parse-js
Version:
Utility library for object structure conversion.
28 lines (21 loc) • 687 B
JavaScript
;
var _transform = require('../lib/transform');
function MatchTransformer(match) {
if (!(this instanceof MatchTransformer)) {
return this.transform(new MatchTransformer(match));
}
if (typeof match === 'string') {
// Escape string
match = match.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
match = new RegExp(match);
}
this._match = match;
}
MatchTransformer.prototype.parse = function (source) {
var _this = this;
return _transform(source, function (result, value, key) {
if (_this._match.test(key)) result[key] = value;
}, {});
};
MatchTransformer.prototype.reverse = MatchTransformer.prototype.parse;
module.exports = MatchTransformer;