simple-placeholder-slot-numberizer-pmb
v0.1.2
Published
Assign number-counting string placeholders for values, remembering the ones already seen.
Downloads
1
Maintainers
Readme
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
: AnArray
used for determining the internal next number, and collecting the encounteredx
values in order of discovery. Default: a new emptyArray
.offset
: Number added to thevalues
array index in order to obtain the pattern-printed slot number. Default:1
dict
: AMap
that maps discoveredx
values to an object with these keysi
(index in thevalues
array) 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
pattern
an object that has ajoin
method. It will be invoked with one single argument, a number primitive.The default
pattern
with defaultoffset
is 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