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

@hecom/specials

v2.1.1

Published

A tool to manage general or special handle.

Downloads

7

Readme

specials

npm version Build Status

A tool to manage general or special handle.

Install

Install by Yarn:

yarn add @hecom/specials

Install by NPM:

npm install --save @hecom/specials

Usage

Import the module in file:

import * as Specials from '@hecom/specials';

Storage

We use a root object to store all information. In each node, we can store a default handle and an array of special handles.

  • Specials.DEFAULT_HANDLE: Key for default handle.
  • Specials.SPECIAL_PART: Key for part of special handle.

Here is an example of a root object:

{
    [DEFAULT_HANDLE]: function f0 {.....},
    display: {
        text: {
            [DEFAULT_HANDLE]: function f1 {.....},
            phone: {
                [DEFAULT_HANDLE]: function f4 {.....},
            },
        },
        [SPECIAL_PART]: [
            {
                [kSpecial]: state => state === 'user',
                [kHandle]: function f2 {...},
                [kId]: 1,
                [kPriority]: 0,
            },
            {
                [kSpecial]: state => state === 'org',
                [kHandle]: function f3 {...},
                [kId]: 2,
                [kPriority]: 1,
            }
        ],
    },
    edit: {
        ...
    },
}

Each special part consists of four keys:

  • Specials.kId: Identifier of handle. Used for unregister the handle.
  • Specials.kSpecial: Special judging function. Returns a boolean.
  • Specials.kHandle: Handle function when special function is valid.
  • Specials.kPriority: Prority of handle when multi items are valid. Larger one will be used first. You can use Specials.PRIORITY.

API

  • register: (obj, path, special, handle, priority) => number | void: Register a default or special handle with priority. Return handleId if special is not null.
  • unregister: (obj, path, handleId) => boolean: Unregister a default or special handle. Return true when succeed.
  • get: (obj, path, state, params) => any: Get handle with a state. Then uses handle function with params or just returns the handle object.

Example

First, initialize a root object:

const rootNode = {};

Register different handle:

Specials.register(rootNode, [], undefined, y => y);
Specials.register(rootNode, 'display', undefined, y => y + 1);
Specials.register(rootNode, ['display', 'text'], undefined, y => y + 2);
Specials.register(rootNode, 'display', x => x === 1, y => y - 1); // return: 1
Specials.register(rootNode, ['display', 'text'], x => x === 2, 'P1'); // return: 1
Specials.register(rootNode, ['display', 'text'], x => x === 2, 'P2', Specials.PRIORITY.HIGH); // return: 2

The root object will be:

{
    [DEFAULT_HANDLE]: y => y,
    display: {
        [DEFAULT_HANDLE]: y => y + 1,
        text: {
            [DEFAULT_HANDLE]: y => y + 2,
            [SPECIAL_PART]: [
                {
                    [kSpecial]: x => x === 2,
                    [kHandle]: 'P1',
                    [kId]: 1,
                    [kPriority]: 0,
                },
                {
                    [kSpecial]: x => x === 2,
                    [kHandle]: 'P2',
                    [kId]: 2,
                    [kPriority]: 255,
                },
            ],
        },
        [SPECIAL_PART]: [
            {
                [kSpecial]: x => x === 1,
                [kHandle]: y = y - 1,
                [kId]: 1,
                [kPriority]: 0,
            },
        ],
    },
}

Then use get to get handle:

Specials.get(rootNode, [], undefined, undefined); // function: y => y
Specials.get(rootNode, [], undefined, 100); // y=>y result: 100
Specials.get(rootNode, ['display'], undefined, 100); // y=>y+1 result: 101
Specials.get(rootNode, ['parse'], undefined, 100); // y=>y result: 100
Specials.get(rootNode, ['display'], 1, 100); // y=>y-1 result: 99
Specials.get(rootNode, ['display'], 2, 100); // y=>y+1 result: 101
Specials.get(rootNode, ['display', 'text'], 1, 100); // y=y-1 result: 99
Specials.get(rootNode, ['display', 'text'], 2, 100); // 'P2' result: 'P2'

Finally we can unregister default or special handle:

Specials.unregister(rootNode, []);
Specials.unregister(rootNode, 'display');
Specials.unregister(rootNode, ['display', 'text']);
Specials.unregister(rootNode, 'display', 1); // return: true
Specials.unregister(rootNode, ['display', 'text'], 1); // return: true
Specials.unregister(rootNode, ['display', 'text'], 2); // return: true
Specials.unregister(rootNode, ['display', 'text'], 3); // return: false