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.
npm install --save short-string
import shortString from 'short-string';
shortString(0)
// ⇒ 'a'
Parameters:
num
- ANumber
identifying the index of the occurrence.
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
.
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()
});
short-string is licensed under the MIT License.