composerx
v0.0.16
Published
[](https://codecov.io/gh/fedeghe/composerx)
Downloads
239
Maintainers
Readme
composerx 0.0.16
Never write the same regexp again, ...ok, ...almost!
In less than 1KB composerx aims to help to:
- define a specific RegExp (once)
- reuse defined ones to define another RegExp
- go back to 1 or 2
const crx = require('composerx');
crx.add('1-31', /^(([1-9])|([1-2]\d)|(3[01]))$/);
crx.add('3letters', /^([a-z]{3})$/i);
crx.compose(
'myComposedRx',
'cx(1-31)|cx(3letters)',
);
const res1 = crx.match('myComposedRx', '3'),
res2 = crx.match('myComposedRx', 'abf');
console.log(crx.get('myComposedRx').source)
console.log({
res1,
// ['3', '3', '3', '3', undefined, ...]
res2
// ['abf', undefined, undefined, undefined, undefined, 'abf', ....]
myComposedRx: crx.get('myComposedRx').source
// (^(([1-9])|([1-2]\d)|(3[01]))$|^([a-z]{3})$)
});API
All methods are static, thus no need for instances.
All chainable methods show a 🔗
composerx.add(String name, RegExp rx)
Adds a named element to the set of the reusable ones.
- throws
whennameis not a truthy string
whenrxis not a RegExp - overrides
in case an element with that name exists already
returns: 🔗
composerx.get(String name)
Retrives an element when existing, null otherwise.
- throws
whennameis not a truthy string.
returns:
the named element if it exists, undefined otherwise
composerx.remove(String name)
Removes an existing element
- throws
whennameis not a string or is an empty string. - no effect
when the element is not found.
returns: 🔗
composerx.compose(String name, String tpl)
Creates a new the elements named name (or overrides an existing one) using the template passed to create the new RegExp using previously added elements.
To use an existing element add for it a placeholder inside the tpl parameter (see the example above).
The placeholder for element named myRx1 is cx(myRx1).
- throws
whennameortplis not a string or is empty string.
returns: 🔗
composerx.match(String name, String search, {definedOnly = false})
Run a match of the search against the elements named name
throws
whennameis not a string or is empty string.
whensearchis not string.options.definedOnly: when passed astrueallundefinedwill be filtered out from the resulting array
returns:
the rx match output or undefined when the element does not exists
composerx.test(String name, String search, {definedOnly = false})
Run a test of the search against the elements named name
- throws
whennameis not a string or is empty string.
whensearchis not string.
returns: Boolean
composerx.clear()
Removes all stored elements.
returns: 🔗
