Join and shorten strings with priorities
This package was designed to shorten concatenated strings for use as IDs. It allows the specification of priorities for determining which items to remove from the string when shortening to fit a desired maximum length.
Simply install it as a dependency using npm:
npm install join-and-shorten --save
Usage is quite simple - to simply join items, you could use the following:
const join = require("join-and-shorten");
join(["one", "two", "three"]); // "one_two_three"
Notice that underscores are the default joiner of strings.
You can also customise the joining character and maximum length:
join(["one", "two", "three"], "~", 9); // "one~two"
Or you can give priorities to the function so that it knows which items to strip:
join([
["one", 2],
["two", 1],
["three", 3]
], "_", 11); // "one_three"
join
also supports a strip-mode parameter, to allow for shortening by character instead of by item:
join([
["abcdef", 2],
["123456", 3]
], ":", 10, STRIP_MODE_REMOVE_CHARACTER); // "abc:123456"
Run the tests by executing npm test
.