npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ts-map

v1.0.3

Published

A typescript Map structure like ES6 Map

Downloads

1,795,386

Readme

What is ts-map?

it is a Map structure like ES6 Map. Map is similar to the object, but also a set of key-value pairs, but the "key" range is not limited to strings, various types of values (including objects) can be used as a key.

Installation

npm install ts-map

Usage

use in typescript file

import TsMap from 'ts-map'
const map = new TsMap()
const k1: number = 1
const k2: number[] = [2] 
const k3: boolean = true
map.set(1, "hello")
map.set(k2, "ts").set(k3, "map")

map.get(1) // "hello"
map.get(k2) // "world"
map.size // 3
map.keys() // [1, [2], true]
map.values() // ["hello", "ts", "map"]

map.forEach((value, key, map) => {
  console.log(key, ':', value)
})
// 1 ':' 'hello'
// [ 2 ] ':' 'ts'
// true ':' 'map'

Getting started

Constructor with parameter

You can pass in the default parameters in the constructor:

const map = new TsMap([
  [1, "ok"],
  [2, "fail"]
])

console.log(map.get(1)) // ok

Class generic

support define generic for ts-map

interface Coder {
  name: string
}

const map = new TsMap<number, Coder>([
  [1, {name: 'lavyun'}]
])

map.set(2, {name: "tom"}) // work

map.set(3, "jack") // sorry, error

If you do not define generics, but in the constructor passed in the parameters, you also need follow the generic rules.If you do not use generics, you can set any type of key-value pairs for the map.

API

size: number

return the Map's size

const map = new TsMap<number, Coder>([
  [1, {name: 'lavyun'}]
])

map.set(2, {name: "tom"})
map.size // 2

set(k: K, v: V): TsMapInter<K, V>

set a key-value to Map, support chain called.

map.set(true, "1")
map.set(1, "hello").set(2, "world")

Notice: Only the reference to the same object, Map structure will be regarded as the same key.

const k = ["1"]
map.set(k, "hello")
map.get(k)  // hello
map.get(["1"])  // undefind

If the same key is assigned multiple times, the following value will overwrite the previous value.

map.set(1, "111").set(1, "222")
map.get(1) // 222

get(k: K): V | undefined

Return the value of the corresponding key,if dosn't include, return undefind.

map.set(1, "111")
map.get(1) // 111
map.get(2) // undefind

has(k: K): boolean

Determine if a key is included.

map.set(1, "111")
map.has(1) // true
map.has(2) // false

delete(k: k): boolean

Delete all the corresponding keys and its value, if detele success, return true. else return false.

map.set(1, "111")
map.set(2, "222")
map.delete(1)
map.has(1) //false
mao.size // 1

clear(): void

Delete all key-value from the Map.

map.set(1, "111")
map.set(2, "222")
map.size // 2
map.clear()
map.size // 0

keys(): K[]

return all Map's key.

map.set(1, 2)
map.set(true, false)
map.set(["1"], {name: 'lavyun'})
map.keys() // [1, true, ["1"]]

values(): V[]

return all Map's value.

map.set(1, 2)
map.set(true, false)
map.set(["1"], {name: 'lavyun'})
map.values() // [2, false, 'lavyun']

entries(): Array<[K, V]>

return all Map's key-value.

map.set(1, 2)
map.set(true, false)
map.set(["1"], {name: 'lavyun'})
map.entries()
/* 
[
  [1, 2],
  [true, false],
  [["1"], {name: 'lavyun'}]
]
*/

forEach(cb, context?: any): void

Traversal the Map.Accept two parameters, first is a callback, second is a optional context.

callback function accepts 3 optional params,first is value, second is key, last is the map.

map.set(1, "111").set(2. "222")
map.forEach((value, key, map) => {
  console.log(key, '-', value)
})
// 1 - '111'
// 2 - '222'

You can pass the second param to set the callback's context

const person = {
  name: 'lavyun'
}

map.set(1, "111").set(2. "222")
map.forEach((value, key, map) => {
  console.log(key, '-', value, '-', this.name)
}, person)
// 1 - '111' - 'lavyun'
// 2 - '222' - 'lavyun'

Licence

MIT LICENCE