darthmaim / short-string

Function to create short strings

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

short-string

version license Travis Coverage

Function to create short strings based on given alphabets for the first char and the remaining ones. Default strings are valid CSS identifiers.

Can for example be used to generate short CSS identifiers as part of a custom encoder for postcss-reduce-idents or a namer for modular-css.

Installation

npm install --save short-string

Usage

shortString(num)

import shortString from 'short-string';

shortString(0)
// ⇒ 'a'

Parameters:

  • num - A Number identifying the index of the occurrence.

shortStringFactory(characters, charactersExtra)

import shortStringFactory from 'short-string/lib/factory';

const customShortString = shortStringFactory('ab', 'abc');

customShortString(5);
// ⇒ 'ba'

Parameters:

  • characters - A String containing all valid characters for the first char of the generated short string.
  • charactersExtra - A String containing all valid characters for the remaining chars of the generated short string. Must start with characters.

Examples

This is the configuration of postcss-reduce-idents to use short-string as encoder.

const options = {
    encoder: (value, occurrence) => shortString(occurrence)
}

This is an example namer function for modular-css using short-string to generate short selector names.

function shortStringNamer() {
    const known = {};
    let nextIndex = 0;

    return function(file, selector) {
        const id = file + selector;

        return shortString(known[id] || (known[id] = nextIndex++));
    }
}

new Processor({
    namer: shortStringNamer()
});

License

short-string is licensed under the MIT License.

About

Function to create short strings

License:MIT License


Languages

Language:JavaScript 100.0%