short-string
v0.0.2
Published
Function to create short strings.
Downloads
1
Readme
short-string
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
- ANumber
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
- AString
containing all valid characters for the first char of the generated short string.charactersExtra
- AString
containing all valid characters for the remaining chars of the generated short string. Must start withcharacters
.
Examples
postcss-reduce-idents
This is the configuration of postcss-reduce-idents to use short-string as encoder.
const options = {
encoder: (value, occurrence) => shortString(occurrence)
}
modular-css
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.