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

@trudbot/map

v0.0.4

Published

c++ 风格的map

Downloads

42

Readme

Easy-map

这是一个c++风格的map, 底层使用红黑树实现

Install

npm install @trudbot/map

Usage

import {EasyMap} from '@trudbot/map';

const map = new EasyMap<number, number>();

map.insert(1, 2);
map.insert(2, 3);
console.log(map.get(1)); // 2

map.erase(1);

console.log(map.get(1)); // null

const proxy = map.createProxy(p => {
    if (typeof p === 'number') return p;
    if (typeof p === 'string') {
        const num = parseInt(p);
        return isNaN(num) ? null : num;
    }
    return null;
});

console.log(proxy.get(2)); // 3
proxy[2] = 4;
console.log(proxy.get(2)); // 4
proxy[2] ++;
console.log(proxy.get(2)); // 5

API

EasyMap<K, V>

constructor(config?: {compare?: Compare<K>, defaultValue?: V})

  • compare 比较函数, 需要返回COMPARE_RESULT枚举值
export type Compare<K> = (a: K, b: K) =>
    COMPARE_RESULT.MORE |  // a > b
    COMPARE_RESULT.EQUAL |  // a === b
    COMPARE_RESULT.LESS;  // a < b
  • defaultValue 默认值 该值只会在使用proxy时起作用, 如
const map = new map<number, number>({defaultValue: 0});
const proxy = map.createProxy(p => {
    if (typeof p === 'number') return p;
    if (typeof p === 'string') {
        const num = parseInt(p);
        return isNaN(num) ? null : num;
    }
    return null;
});

console.log(proxy[1]); // 0

insert(key: K, value: V): RBNode<K, V>

插入一个键值对, 返回值为插入的节点

erase(key: K): RBNode<K, V> | null

删除一个键值对, 返回删除的节点

get(key: K): V | null

获取一个键值对, 如果不存在则返回null

clear()

清空所有键值对

keys(): K[]

返回所有的键, 按键升序

values(): V[]

返回所有的值, 按键升序

entries(): {key: K, value:V}[]

返回所有的键值对, 按键升序

size(): number

返回map中键值对的数量

count(key: K): number

返回指定键的数量, 0或1

has(key: K): boolean

返回map中是否存在指定键

createProxy(transform: (key: K) => T): ProxyMap<T>

创建一个map代理, 代理 支持通过中括号表达式存取键值对。

forEach(callback: (value: V, key: K, map: EasyMap<K, V>) => void): void

按键升序遍历map中的键值对

ProxyMap

proxyMap支持通过中括号表达式存取键值对。

transform(p: PropertyKey): V

在createProxy时传入的转换函数, 用于将中括号表达式的键转换为map中的键 PropertyKeystring | number | symbol的联合类型。

const map = new map<number, number>({defaultValue: 0});
const proxy = map.createProxy(p => {
    if (typeof p === 'number') return p;
    if (typeof p === 'string') {
        const num = parseInt(p);
        return isNaN(num) ? null : num;
    }
    return null;
});

proxy[1] // map.get(1)
proxy[1] = 2 // map.insert(1, 2), 此处与insert不完全等价, 因为proxy[1]只会返回true
delete proxy[1] // map.erase(1), 此处与erase不完全等价, 因为delete proxy[1]只会返回true
1 in proxy  // map.has(1)
proxy[1] ++ // map.insert(1, map.get(1))
// ...