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

short-key-generator

v1.2.0

Published

Generate sequence of short keys based on specific alphabet and provide a keymap

Downloads

6

Readme

Short key generator

Build Status Coverage Status

Why?

To reduce size of JSON with a large data set that is transmitted over the network. I've seen this in quite a few projects - always done from scratch and usually not very well.

Problem

var toSend = {
  data: [
    {
      selfExplanatoryKey1: 11,
      selfExplanatoryKey2: 22,
      ...
      selfExplanatoryKeyN: 997
    },
    {
      selfExplanatoryKey1: 66,
      selfExplanatoryKey2: 77,
      ...
    },
  ]
};

The stringified JSON gets very large because of the long self-explanatory keys.

Solution

var toSend = {
  data: [
    {
      A: 11,
      B: 22,
      ...
      Cm: 997
    },
    {
      A: 66,
      B: 77,
      ...
    },
  ],
  map: {
    selfExplanatoryKey1: 'A',
    selfExplanatoryKey2: 'B',
    ....
    selfExplanatoryKeyN: 'Cm'
  }
};

Basic Usage

var keyMapFactory = require('short-key-generator');
var keyMap = keyMapFactory();
keyMap.getOrCreate('selfExplanatoryKey1'); // 'A'
keyMap.getOrCreate('selfExplanatoryKey2'); // 'B'
keyMap.getOrCreate('selfExplanatoryKey1'); // 'A'

// map object keys and keep the values unchanged
keyMap.mapObjectKeys({
  selfExplanatoryKey2: 1,
  selfExplanatoryKey3: {selfExplanatoryKey4: 2}
}); // {'B': 1, 'C': {selfExplanatoryKey4: 2}}

keyMap.getMap();
// returns
//  {
//    selfExplanatoryKey1: 'A',
//    selfExplanatoryKey2: 'B',
//    selfExplanatoryKey3: 'C'
//  }

// if you need inverted map like {A: 'selfExplanatoryKey1'}, use
keyMap.getIvertedMap();

API Reference

The default export of this module is ~keyMap(). That and probably ~characterRange() are the only things you need. The other module members (classes) are exported just in case you feel like you want to extend them and it is not worth a pull request.

~~KeyMap

Kind: inner class of short-key-generator

keyMap.getOrCreate(longKey) ⇒ string

Get an existing short key for provided long key. If it doesn't exist, new short key is created

Kind: instance method of KeyMap

| Param | Type | | --- | --- | | longKey | string |

keyMap.getKey(longKey) ⇒ string | undefined

Get an existing short key for provided long key

Kind: instance method of KeyMap
Returns: string | undefined - undefined if there is no entry for longKey

| Param | Type | | --- | --- | | longKey | string |

keyMap.mapObjectKeys(obj) ⇒ Object

Utility method to rename object keys

Kind: instance method of KeyMap
Returns: Object - new object with original values

| Param | Type | | --- | --- | | obj | Object |

keyMap.getMap() ⇒ Object

Kind: instance method of KeyMap
Returns: Object - map of all entries: {longKey: shortKey}

keyMap.getInvertedMap() ⇒ Object

Kind: instance method of KeyMap
Returns: Object - map of all entries: {shortKey: longKey}

~~SequentialKeyGen

Kind: inner class of short-key-generator

sequentialKeyGen.getNextKey() ⇒ string

Generate new key

Kind: instance method of SequentialKeyGen

~~characterRange(firstChar, lastChar, optStep) ⇒ Array.<string>

Utility function to create a range/array of characters

Kind: inner method of short-key-generator
Returns: Array.<string> - array of characters

| Param | Type | Description | | --- | --- | --- | | firstChar | string | first character of the desired range - first character of the provided string is used - non-strings are coverted to string - if no character (empty string) is provided, empty array is returned | | lastChar | string | last character of the desired range; same behavior as firstChar | | optStep | string | step - default is one, decimals are floored, 0 converted to 1 |

~~keyMap() ⇒ KeyMap

Function creating new instance of KeyMap

Kind: inner method of short-key-generator

| Param | Type | Default | Description | | --- | --- | --- | --- | | [options.initInvertedMap] | Object.<string, string> | Object() | initialize map, e.g. {shortKey: 'longKey'} | | [options.alphabet] | Array.<string> | ['A'-'Z'] | strings used to generate new keys | | [options.initCounter] | number | 0 | use if you want to skip a few keys at the beginning | | [options.glueFn] | function | fragments => fragments.join('') | keys are created with one or more strings from the alphabet. By default, they are combined with .join('') |

~~sequentialKeyGen() ⇒ SequentialKeyGen

Function creating new instance of SequentialKeyGen Exported as member sequentialKeyGen

Kind: inner method of short-key-generator

| Param | Type | Default | Description | | --- | --- | --- | --- | | [options.alphabet] | Array.<string> | ['A'-'Z'] | strings used to generate new keys | | [options.initCounter] | number | 0 | use if you want to skip a few keys at the beginning | | [options.glueFn] | function | fragments => fragments.join('') | keys are created with one or more strings from the alphabet. By default, they are combined with .join('') |

Contributing

Feel free to suggest new features or create pull requests (PR). With PR, keep the test coverage at 100% ;)

Clone the repo and develop new feature or bug fix

$ git clone https://github.com/stropho/short-key-generator.git
$ cd short-key-generator
$ npm i
$ npm run test:unit:watch

Tests autamatically run on change in src/ or test/ folder.

To run other tasks like linter, coverage or build, simply run

$ npm run all

Basic concept

Script Tasks

build

Build the code from ./src, store in ./dist

$ npm run build

Test

## Unit tests
$ npm run test:unit

## All tests and linter for test files
$ npm run test

Test coverage

$ npm run coverage

Linting

## Lint `./src`
$ npm run lint

## Lint './test`
$ npm run lint:test