regexp-tokenize
v1.0.0
Published
RegExp.prototype.exec sugar
Downloads
4
Maintainers
Readme
regexp-tokenize
RegExp.prototype.exec sugar to preserve callback/promise chains
Instead of:
var tokens = /(.+)=(.+)/.exec('key1=value1');
if (tokens) {
console.log('key:', tokens[1], 'value:', tokens[2]);
} else {
console.error('No match');
}
You can do:
(a) Callback
Get captured tokens as named arguments instead of dealing with obscure indices:
tokenize(/(.+)=(.+)/, 'key1=value1', function (match, key, value) {
console.log('key:', key, 'value:', value);
});
(b) Promise
Get captured tokens with a resolved promise:
tokenize(/(.+)=(.+)/, 'key1=value1')
.then(function (tokens) {
console.log('key:', tokens[1], 'value:', tokens[2]);
})
.catch(function () {
console.error('No match');
});
(c) Promise with token mapping
tokenize(/(.+)=(.+)/, 'key1=value1', { key: 1, value: 2 })
.then(function (pair) {
console.log('key:', pair.key, 'value:', pair.value);
})
.catch(function () {
console.error('No match');
});
Usage
npm install --save regexp-tokenize
var tokenize = require('regexp-tokenize');
tokenize(/.../, '...')