0.1.2 • Published 2 years ago
simple-placeholder-slot-numberizer-pmb v0.1.2
simple-placeholder-slot-numberizer-pmb
Assign number-counting string placeholders for values, remembering the ones already seen.
API
This module exports one function:
makeNumberizer(opt)
Return a function numb(x) that will assign distinct numbers for each
distinct x and return a string based on that number.
opt is an optional options object that supports these optional keys:
pattern: An array of string parts that will be glued together with the counter number. Default:['$', '']values: AnArrayused for determining the internal next number, and collecting the encounteredxvalues in order of discovery. Default: a new emptyArray.offset: Number added to thevaluesarray index in order to obtain the pattern-printed slot number. Default:1dict: AMapthat maps discoveredxvalues to an object with these keysi(index in thevaluesarray) ands(the string resulting from applyingpattern). Default: new emptyMap.
The numb function will carry all the keys that are supported in opt,
and modifying them will modify behavior for future encounters.
In addition, the numb function will carry these other properties:
tag: A renderer function that willnumb()all slot values in a tagged string.
Bonus knowledge
- For arbitrarily fancy render functions, make
patternan object that has ajoinmethod. It will be invoked with one single argument, a number primitive. - The default
patternwith defaultoffsetis useful for…- up to 9 JS RegExp capture groups.
- up to 9 bash function arguments.
(
bash -c 'f() { echo "a$42z"; }; f b{1..100}y'→ab4y2z)- If you want more, use
pattern: ['${', '}'](bash -c 'f() { echo "a${42}z"; }; f b{1..100}y'→ab42yz)
- If you want more, use
- PostgreSQL positional parameters up to more than 32k
(haven't tested though).
- However, function arguments are limited to 100. You can use arrays though.
Usage
See test/usage.mjs.
Known issues
- Needs more/better tests and docs.
License
ISC