string-between
v1.0.2
Published
Get a string between (when compared) two other strings
Readme
string-between
Get a string between (when compared) two other strings
Install
$ yarn add string-betweenUsage
It can, for example, be usefull to represent an ordered list in a database: You keep indexes as string and order the list with them. This way you can add new element at the end or insert between elements as much as you want without editing other indexes. Repeated insertions may increase the length of the rank strings.
const stringBefore = "A";
const stringAfter = "B";
const stringBetween = getStringBetween(stringBefore, stringAfter);
console.log(stringBetween);
//=> "AW"
// "AW" > "A" && "AW" < "B"
// This works indefinitely:
const stringBefore = "A";
const stringAfter = "AW";
const stringBetween = getStringBetween(stringBefore, stringAfter);
console.log(stringBetween);
//=> "AC"
// "AC" > "A" && "AC" < "AW"
// You can also add after:
const stringBefore = "A";
const stringAfter = getStringBetween(stringBefore);
console.log(stringAfter);
//=> "B"
// "B" > "A"
// or before:
const stringAfter = "A";
const stringBefore = getStringBetween(null, stringAfter);
console.log(stringBefore);
//=> "8"
// "8" < "A"
// or even to start with
const string = getStringBetween();
console.log(string);
//=> "W"Parameters
string before (Optional)
string after (Optional)
minimum char code (Optional: default to 48)
maximum char code (Optional: default to 126)
The character codes define an inclusive alphabet of at least two UTF-16 code
units. Both codes must be integers between 0 and 65535, with the minimum
strictly smaller than the maximum. Bounds must use characters from this alphabet.
Ordering and boundaries
The result is strictly between the supplied bounds using JavaScript string
comparison. Reversed bounds are accepted and swapped. Omit a bound with null,
undefined, or an empty string to insert before or after the other bound.
getStringBetween("101", "201"); // "101W"
// "101" < "101W" && "101W" < "201"If no nonempty string fits, the function throws. Equal bounds are one example.
With the default alphabet, "0" is the smallest character, so there is also no
string between "A" and "A0", or before "0".
For repeatedly editable ranks, avoid initial ranks ending in the minimum
character. The generator preserves this property when given such ranks.
Externally supplied ranks ending in the minimum character may still leave a
finite amount of room: between "A" and "A00", the result is "A0", but no
further string fits between "A" and "A0".
