complex-matcher
Advanced tools
Comparing version 0.3.0 to 0.4.0
"use strict"; | ||
exports.__esModule = true; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
@@ -5,0 +7,0 @@ |
"use strict"; | ||
exports.__esModule = true; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.ast = exports.pattern = void 0; | ||
@@ -10,6 +12,6 @@ | ||
var pattern = 'foo !"\\\\ \\"" name:|(wonderwoman batman) hasCape? age:32'; | ||
var pattern = 'foo !"\\\\ \\"" name:|(wonderwoman batman) hasCape? age:32 chi*go /^foo\\/bar\\./i'; | ||
exports.pattern = pattern; | ||
var ast = new CM.And([new CM.String('foo'), new CM.Not(new CM.String('\\ "')), new CM.Property('name', new CM.Or([new CM.String('wonderwoman'), new CM.String('batman')])), new CM.TruthyProperty('hasCape'), new CM.Property('age', new CM.Number(32))]); | ||
var ast = new CM.And([new CM.String('foo'), new CM.Not(new CM.String('\\ "')), new CM.Property('name', new CM.Or([new CM.String('wonderwoman'), new CM.String('batman')])), new CM.TruthyProperty('hasCape'), new CM.Property('age', new CM.Number(32)), new CM.GlobPattern('chi*go'), new CM.RegExp('^foo/bar\\.', 'i')]); | ||
exports.ast = ast; | ||
//# sourceMappingURL=index.fixtures.js.map |
"use strict"; | ||
exports.__esModule = true; | ||
exports.setPropertyClause = exports.getPropertyClausesStrings = exports.parse = exports.TruthyProperty = exports.String = exports.StringNode = exports.Property = exports.Number = exports.NumberNode = exports.Not = exports.Or = exports.Comparison = exports.And = exports.Null = void 0; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.setPropertyClause = exports.getPropertyClausesStrings = exports.parse = exports.TruthyProperty = exports.String = exports.StringNode = exports.RegExp = exports.RegExpNode = exports.GlobPattern = exports.Property = exports.Number = exports.NumberNode = exports.Not = exports.Or = exports.Comparison = exports.And = exports.Null = void 0; | ||
@@ -10,12 +12,34 @@ var _some2 = _interopRequireDefault(require("lodash/some")); | ||
var _escapeRegExp2 = _interopRequireDefault(require("lodash/escapeRegExp")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); } | ||
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } | ||
function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } | ||
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } | ||
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } | ||
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } | ||
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; } | ||
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } | ||
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
var RAW_STRING_CHARS = function () { | ||
@@ -26,7 +50,4 @@ var chars = { | ||
var add = function add(a, b) { | ||
if (b === void 0) { | ||
b = a; | ||
} | ||
var add = function add(a) { | ||
var b = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : a; | ||
var i = a.charCodeAt(0); | ||
@@ -63,14 +84,17 @@ var j = b.charCodeAt(0); | ||
var Node = function () { | ||
function Node() {} | ||
function Node() { | ||
_classCallCheck(this, Node); | ||
} | ||
var _proto = Node.prototype; | ||
_createClass(Node, [{ | ||
key: "createPredicate", | ||
value: function createPredicate() { | ||
var _this = this; | ||
_proto.createPredicate = function createPredicate() { | ||
var _this = this; | ||
return function (value) { | ||
return _this.match(value); | ||
}; | ||
} | ||
}]); | ||
return function (value) { | ||
return _this.match(value); | ||
}; | ||
}; | ||
return Node; | ||
@@ -80,18 +104,22 @@ }(); | ||
var Null = function (_Node) { | ||
_inheritsLoose(Null, _Node); | ||
_inherits(Null, _Node); | ||
function Null() { | ||
return _Node.apply(this, arguments) || this; | ||
_classCallCheck(this, Null); | ||
return _possibleConstructorReturn(this, _getPrototypeOf(Null).apply(this, arguments)); | ||
} | ||
var _proto2 = Null.prototype; | ||
_createClass(Null, [{ | ||
key: "match", | ||
value: function match() { | ||
return true; | ||
} | ||
}, { | ||
key: "toString", | ||
value: function toString() { | ||
return ''; | ||
} | ||
}]); | ||
_proto2.match = function match() { | ||
return true; | ||
}; | ||
_proto2.toString = function toString() { | ||
return ''; | ||
}; | ||
return Null; | ||
@@ -109,3 +137,3 @@ }(Node); | ||
var And = function (_Node2) { | ||
_inheritsLoose(And, _Node2); | ||
_inherits(And, _Node2); | ||
@@ -115,6 +143,8 @@ function And(children) { | ||
_this2 = _Node2.call(this) || this; | ||
_classCallCheck(this, And); | ||
_this2 = _possibleConstructorReturn(this, _getPrototypeOf(And).call(this)); | ||
if (children.length === 1) { | ||
return children[0] || _assertThisInitialized(_this2); | ||
return _possibleConstructorReturn(_this2, children[0]); | ||
} | ||
@@ -126,15 +156,17 @@ | ||
var _proto3 = And.prototype; | ||
_createClass(And, [{ | ||
key: "match", | ||
value: function match(value) { | ||
return this.children.every(function (child) { | ||
return child.match(value); | ||
}); | ||
} | ||
}, { | ||
key: "toString", | ||
value: function toString(isNested) { | ||
var terms = formatTerms(this.children); | ||
return isNested ? "(".concat(terms, ")") : terms; | ||
} | ||
}]); | ||
_proto3.match = function match(value) { | ||
return this.children.every(function (child) { | ||
return child.match(value); | ||
}); | ||
}; | ||
_proto3.toString = function toString(isNested) { | ||
var terms = formatTerms(this.children); | ||
return isNested ? "(" + terms + ")" : terms; | ||
}; | ||
return And; | ||
@@ -146,3 +178,3 @@ }(Node); | ||
var Comparison = function (_Node3) { | ||
_inheritsLoose(Comparison, _Node3); | ||
_inherits(Comparison, _Node3); | ||
@@ -152,3 +184,5 @@ function Comparison(operator, value) { | ||
_this3 = _Node3.call(this) || this; | ||
_classCallCheck(this, Comparison); | ||
_this3 = _possibleConstructorReturn(this, _getPrototypeOf(Comparison).call(this)); | ||
_this3._comparator = Comparison.comparators[operator]; | ||
@@ -160,12 +194,14 @@ _this3._operator = operator; | ||
var _proto4 = Comparison.prototype; | ||
_createClass(Comparison, [{ | ||
key: "match", | ||
value: function match(value) { | ||
return typeof value === 'number' && this._comparator(value, this._value); | ||
} | ||
}, { | ||
key: "toString", | ||
value: function toString() { | ||
return this._operator + String(this._value); | ||
} | ||
}]); | ||
_proto4.match = function match(value) { | ||
return typeof value === 'number' && this._comparator(value, this._value); | ||
}; | ||
_proto4.toString = function toString() { | ||
return this._operator + String(this._value); | ||
}; | ||
return Comparison; | ||
@@ -191,3 +227,3 @@ }(Node); | ||
var Or = function (_Node4) { | ||
_inheritsLoose(Or, _Node4); | ||
_inherits(Or, _Node4); | ||
@@ -197,6 +233,8 @@ function Or(children) { | ||
_this4 = _Node4.call(this) || this; | ||
_classCallCheck(this, Or); | ||
_this4 = _possibleConstructorReturn(this, _getPrototypeOf(Or).call(this)); | ||
if (children.length === 1) { | ||
return children[0] || _assertThisInitialized(_this4); | ||
return _possibleConstructorReturn(_this4, children[0]); | ||
} | ||
@@ -208,14 +246,16 @@ | ||
var _proto5 = Or.prototype; | ||
_createClass(Or, [{ | ||
key: "match", | ||
value: function match(value) { | ||
return this.children.some(function (child) { | ||
return child.match(value); | ||
}); | ||
} | ||
}, { | ||
key: "toString", | ||
value: function toString() { | ||
return "|(".concat(formatTerms(this.children), ")"); | ||
} | ||
}]); | ||
_proto5.match = function match(value) { | ||
return this.children.some(function (child) { | ||
return child.match(value); | ||
}); | ||
}; | ||
_proto5.toString = function toString() { | ||
return "|(" + formatTerms(this.children) + ")"; | ||
}; | ||
return Or; | ||
@@ -227,3 +267,3 @@ }(Node); | ||
var Not = function (_Node5) { | ||
_inheritsLoose(Not, _Node5); | ||
_inherits(Not, _Node5); | ||
@@ -233,3 +273,5 @@ function Not(child) { | ||
_this5 = _Node5.call(this) || this; | ||
_classCallCheck(this, Not); | ||
_this5 = _possibleConstructorReturn(this, _getPrototypeOf(Not).call(this)); | ||
_this5.child = child; | ||
@@ -239,12 +281,14 @@ return _this5; | ||
var _proto6 = Not.prototype; | ||
_createClass(Not, [{ | ||
key: "match", | ||
value: function match(value) { | ||
return !this.child.match(value); | ||
} | ||
}, { | ||
key: "toString", | ||
value: function toString() { | ||
return '!' + this.child.toString(true); | ||
} | ||
}]); | ||
_proto6.match = function match(value) { | ||
return !this.child.match(value); | ||
}; | ||
_proto6.toString = function toString() { | ||
return '!' + this.child.toString(true); | ||
}; | ||
return Not; | ||
@@ -256,3 +300,3 @@ }(Node); | ||
var NumberNode = function (_Node6) { | ||
_inheritsLoose(NumberNode, _Node6); | ||
_inherits(NumberNode, _Node6); | ||
@@ -262,6 +306,8 @@ function NumberNode(value) { | ||
_this6 = _Node6.call(this) || this; | ||
_classCallCheck(this, NumberNode); | ||
_this6 = _possibleConstructorReturn(this, _getPrototypeOf(NumberNode).call(this)); | ||
_this6.value = value; | ||
Object.defineProperty(_assertThisInitialized(_this6), 'match', { | ||
value: _this6.match.bind(_assertThisInitialized(_this6)) | ||
Object.defineProperty(_assertThisInitialized(_assertThisInitialized(_this6)), 'match', { | ||
value: _this6.match.bind(_assertThisInitialized(_assertThisInitialized(_this6))) | ||
}); | ||
@@ -271,12 +317,14 @@ return _this6; | ||
var _proto7 = NumberNode.prototype; | ||
_createClass(NumberNode, [{ | ||
key: "match", | ||
value: function match(value) { | ||
return value === this.value || value !== null && _typeof(value) === 'object' && (0, _some2.default)(value, this.match); | ||
} | ||
}, { | ||
key: "toString", | ||
value: function toString() { | ||
return String(this.value); | ||
} | ||
}]); | ||
_proto7.match = function match(value) { | ||
return value === this.value || value !== null && typeof value === 'object' && (0, _some2.default)(value, this.match); | ||
}; | ||
_proto7.toString = function toString() { | ||
return String(this.value); | ||
}; | ||
return NumberNode; | ||
@@ -288,3 +336,3 @@ }(Node); | ||
var Property = function (_Node7) { | ||
_inheritsLoose(Property, _Node7); | ||
_inherits(Property, _Node7); | ||
@@ -294,3 +342,5 @@ function Property(name, child) { | ||
_this7 = _Node7.call(this) || this; | ||
_classCallCheck(this, Property); | ||
_this7 = _possibleConstructorReturn(this, _getPrototypeOf(Property).call(this)); | ||
_this7.name = name; | ||
@@ -301,12 +351,14 @@ _this7.child = child; | ||
var _proto8 = Property.prototype; | ||
_createClass(Property, [{ | ||
key: "match", | ||
value: function match(value) { | ||
return value != null && this.child.match(value[this.name]); | ||
} | ||
}, { | ||
key: "toString", | ||
value: function toString() { | ||
return "".concat(formatString(this.name), ":").concat(this.child.toString(true)); | ||
} | ||
}]); | ||
_proto8.match = function match(value) { | ||
return value != null && this.child.match(value[this.name]); | ||
}; | ||
_proto8.toString = function toString() { | ||
return formatString(this.name) + ":" + this.child.toString(true); | ||
}; | ||
return Property; | ||
@@ -322,16 +374,21 @@ }(Node); | ||
var formatString = function formatString(value) { | ||
return Number.isNaN(+value) ? isRawString(value) ? value : "\"" + value.replace(/\\|"/g, escapeChar) + "\"" : "\"" + value + "\""; | ||
return Number.isNaN(+value) ? isRawString(value) ? value : "\"".concat(value.replace(/\\|"/g, escapeChar), "\"") : "\"".concat(value, "\""); | ||
}; | ||
var StringNode = function (_Node8) { | ||
_inheritsLoose(StringNode, _Node8); | ||
var GlobPattern = function (_Node8) { | ||
_inherits(GlobPattern, _Node8); | ||
function StringNode(value) { | ||
function GlobPattern(value) { | ||
var _this8; | ||
_this8 = _Node8.call(this) || this; | ||
_this8.lcValue = value.toLowerCase(); | ||
_classCallCheck(this, GlobPattern); | ||
if (value.indexOf('*') === -1) { | ||
return _possibleConstructorReturn(_this8, new StringNode(value)); | ||
} | ||
_this8 = _possibleConstructorReturn(this, _getPrototypeOf(GlobPattern).call(this)); | ||
_this8.value = value; | ||
Object.defineProperty(_assertThisInitialized(_this8), 'match', { | ||
value: _this8.match.bind(_assertThisInitialized(_this8)) | ||
Object.defineProperty(_assertThisInitialized(_assertThisInitialized(_this8)), 'match', { | ||
value: _this8.match.bind(_assertThisInitialized(_assertThisInitialized(_this8)), new RegExp(value.split('*').map(_escapeRegExp2.default).join('.*'), 'i')) | ||
}); | ||
@@ -341,20 +398,104 @@ return _this8; | ||
var _proto9 = StringNode.prototype; | ||
_createClass(GlobPattern, [{ | ||
key: "match", | ||
value: function match(re, value) { | ||
if (typeof value === 'string') { | ||
return re.test(value); | ||
} | ||
_proto9.match = function match(value) { | ||
if (typeof value === 'string') { | ||
return value.toLowerCase().indexOf(this.lcValue) !== -1; | ||
if (Array.isArray(value) || (0, _isPlainObject2.default)(value)) { | ||
return (0, _some2.default)(value, this.match); | ||
} | ||
return false; | ||
} | ||
}, { | ||
key: "toString", | ||
value: function toString() { | ||
return this.value; | ||
} | ||
}]); | ||
if (Array.isArray(value) || (0, _isPlainObject2.default)(value)) { | ||
return (0, _some2.default)(value, this.match); | ||
return GlobPattern; | ||
}(Node); | ||
exports.GlobPattern = GlobPattern; | ||
var RegExpNode = function (_Node9) { | ||
_inherits(RegExpNode, _Node9); | ||
function RegExpNode(pattern, flags) { | ||
var _this9; | ||
_classCallCheck(this, RegExpNode); | ||
_this9 = _possibleConstructorReturn(this, _getPrototypeOf(RegExpNode).call(this)); | ||
_this9.re = new RegExp(pattern, flags); | ||
Object.defineProperty(_assertThisInitialized(_assertThisInitialized(_this9)), 'match', { | ||
value: _this9.match.bind(_assertThisInitialized(_assertThisInitialized(_this9))) | ||
}); | ||
return _this9; | ||
} | ||
_createClass(RegExpNode, [{ | ||
key: "match", | ||
value: function match(value) { | ||
if (typeof value === 'string') { | ||
return this.re.test(value); | ||
} | ||
if (Array.isArray(value) || (0, _isPlainObject2.default)(value)) { | ||
return (0, _some2.default)(value, this.match); | ||
} | ||
return false; | ||
} | ||
}, { | ||
key: "toString", | ||
value: function toString() { | ||
return this.re.toString(); | ||
} | ||
}]); | ||
return false; | ||
}; | ||
return RegExpNode; | ||
}(Node); | ||
_proto9.toString = function toString() { | ||
return formatString(this.value); | ||
}; | ||
exports.RegExp = exports.RegExpNode = RegExpNode; | ||
var StringNode = function (_Node10) { | ||
_inherits(StringNode, _Node10); | ||
function StringNode(value) { | ||
var _this10; | ||
_classCallCheck(this, StringNode); | ||
_this10 = _possibleConstructorReturn(this, _getPrototypeOf(StringNode).call(this)); | ||
_this10.value = value; | ||
Object.defineProperty(_assertThisInitialized(_assertThisInitialized(_this10)), 'match', { | ||
value: _this10.match.bind(_assertThisInitialized(_assertThisInitialized(_this10)), value.toLowerCase()) | ||
}); | ||
return _this10; | ||
} | ||
_createClass(StringNode, [{ | ||
key: "match", | ||
value: function match(lcValue, value) { | ||
if (typeof value === 'string') { | ||
return value.toLowerCase().indexOf(lcValue) !== -1; | ||
} | ||
if (Array.isArray(value) || (0, _isPlainObject2.default)(value)) { | ||
return (0, _some2.default)(value, this.match); | ||
} | ||
return false; | ||
} | ||
}, { | ||
key: "toString", | ||
value: function toString() { | ||
return formatString(this.value); | ||
} | ||
}]); | ||
return StringNode; | ||
@@ -365,23 +506,27 @@ }(Node); | ||
var TruthyProperty = function (_Node9) { | ||
_inheritsLoose(TruthyProperty, _Node9); | ||
var TruthyProperty = function (_Node11) { | ||
_inherits(TruthyProperty, _Node11); | ||
function TruthyProperty(name) { | ||
var _this9; | ||
var _this11; | ||
_this9 = _Node9.call(this) || this; | ||
_this9.name = name; | ||
return _this9; | ||
_classCallCheck(this, TruthyProperty); | ||
_this11 = _possibleConstructorReturn(this, _getPrototypeOf(TruthyProperty).call(this)); | ||
_this11.name = name; | ||
return _this11; | ||
} | ||
var _proto10 = TruthyProperty.prototype; | ||
_createClass(TruthyProperty, [{ | ||
key: "match", | ||
value: function match(value) { | ||
return value != null && !!value[this.name]; | ||
} | ||
}, { | ||
key: "toString", | ||
value: function toString() { | ||
return formatString(this.name) + '?'; | ||
} | ||
}]); | ||
_proto10.match = function match(value) { | ||
return value != null && !!value[this.name]; | ||
}; | ||
_proto10.toString = function toString() { | ||
return formatString(this.name) + '?'; | ||
}; | ||
return TruthyProperty; | ||
@@ -394,2 +539,4 @@ }(Node); | ||
function Failure(pos, expected) { | ||
_classCallCheck(this, Failure); | ||
this.expected = expected; | ||
@@ -402,3 +549,3 @@ this.pos = pos; | ||
get: function get() { | ||
throw new Error("parse error: expected " + this.expected + " at position " + this.pos); | ||
throw new Error("parse error: expected ".concat(this.expected, " at position ").concat(this.pos)); | ||
} | ||
@@ -411,2 +558,4 @@ }]); | ||
var Success = function Success(pos, value) { | ||
_classCallCheck(this, Success); | ||
this.pos = pos; | ||
@@ -417,131 +566,156 @@ this.value = value; | ||
var P = function () { | ||
P.alt = function alt() { | ||
for (var _len = arguments.length, parsers = new Array(_len), _key = 0; _key < _len; _key++) { | ||
parsers[_key] = arguments[_key]; | ||
} | ||
_createClass(P, null, [{ | ||
key: "alt", | ||
value: function alt() { | ||
for (var _len = arguments.length, parsers = new Array(_len), _key = 0; _key < _len; _key++) { | ||
parsers[_key] = arguments[_key]; | ||
} | ||
var length = parsers.length; | ||
return new P(function (input, pos, end) { | ||
for (var i = 0; i < length; ++i) { | ||
var result = parsers[i]._parse(input, pos, end); | ||
var length = parsers.length; | ||
return new P(function (input, pos, end) { | ||
for (var i = 0; i < length; ++i) { | ||
var result = parsers[i]._parse(input, pos, end); | ||
if (result instanceof Success) { | ||
return result; | ||
if (result instanceof Success) { | ||
return result; | ||
} | ||
} | ||
} | ||
return new Failure(pos, 'alt'); | ||
}); | ||
}; | ||
return new Failure(pos, 'alt'); | ||
}); | ||
} | ||
}, { | ||
key: "grammar", | ||
value: function grammar(rules) { | ||
var grammar = {}; | ||
Object.keys(rules).forEach(function (k) { | ||
var rule = rules[k]; | ||
grammar[k] = rule instanceof P ? rule : P.lazy(rule, grammar); | ||
}); | ||
return grammar; | ||
} | ||
}, { | ||
key: "lazy", | ||
value: function lazy(parserCreator, arg) { | ||
var parser = new P(function (input, pos, end) { | ||
return (parser._parse = parserCreator(arg)._parse)(input, pos, end); | ||
}); | ||
return parser; | ||
} | ||
}, { | ||
key: "regex", | ||
value: function regex(_regex) { | ||
_regex = new RegExp(_regex.source, 'y'); | ||
return new P(function (input, pos) { | ||
_regex.lastIndex = pos; | ||
P.grammar = function grammar(rules) { | ||
var grammar = {}; | ||
Object.keys(rules).forEach(function (k) { | ||
var rule = rules[k]; | ||
grammar[k] = rule instanceof P ? rule : P.lazy(rule, grammar); | ||
}); | ||
return grammar; | ||
}; | ||
var matches = _regex.exec(input); | ||
P.lazy = function lazy(parserCreator, arg) { | ||
var parser = new P(function (input, pos, end) { | ||
return (parser._parse = parserCreator(arg)._parse)(input, pos, end); | ||
}); | ||
return parser; | ||
}; | ||
return matches !== null ? new Success(_regex.lastIndex, matches[0]) : new Failure(pos, _regex); | ||
}); | ||
} | ||
}, { | ||
key: "seq", | ||
value: function seq() { | ||
for (var _len2 = arguments.length, parsers = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
parsers[_key2] = arguments[_key2]; | ||
} | ||
P.regex = function regex(_regex) { | ||
_regex = new RegExp(_regex.source, 'y'); | ||
return new P(function (input, pos) { | ||
_regex.lastIndex = pos; | ||
var length = parsers.length; | ||
return new P(function (input, pos, end) { | ||
var values = new Array(length); | ||
var matches = _regex.exec(input); | ||
for (var i = 0; i < length; ++i) { | ||
var result = parsers[i]._parse(input, pos, end); | ||
return matches !== null ? new Success(_regex.lastIndex, matches[0]) : new Failure(pos, _regex); | ||
}); | ||
}; | ||
if (result instanceof Failure) { | ||
return result; | ||
} | ||
P.seq = function seq() { | ||
for (var _len2 = arguments.length, parsers = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
parsers[_key2] = arguments[_key2]; | ||
} | ||
var length = parsers.length; | ||
return new P(function (input, pos, end) { | ||
var values = new Array(length); | ||
for (var i = 0; i < length; ++i) { | ||
var result = parsers[i]._parse(input, pos, end); | ||
if (result instanceof Failure) { | ||
return result; | ||
pos = result.pos; | ||
values[i] = result.value; | ||
} | ||
pos = result.pos; | ||
values[i] = result.value; | ||
} | ||
return new Success(pos, values); | ||
}); | ||
} | ||
}, { | ||
key: "text", | ||
value: function text(_text) { | ||
var length = _text.length; | ||
return new P(function (input, pos) { | ||
return input.startsWith(_text, pos) ? new Success(pos + length, _text) : new Failure(pos, "'".concat(_text, "'")); | ||
}); | ||
} | ||
}]); | ||
return new Success(pos, values); | ||
}); | ||
}; | ||
function P(parse) { | ||
_classCallCheck(this, P); | ||
P.text = function text(_text) { | ||
var length = _text.length; | ||
return new P(function (input, pos) { | ||
return input.startsWith(_text, pos) ? new Success(pos + length, _text) : new Failure(pos, "'" + _text + "'"); | ||
}); | ||
}; | ||
function P(parse) { | ||
this._parse = parse; | ||
} | ||
var _proto11 = P.prototype; | ||
_createClass(P, [{ | ||
key: "map", | ||
value: function map(fn) { | ||
var _this12 = this; | ||
_proto11.map = function map(fn) { | ||
var _this10 = this; | ||
return new P(function (input, pos, end) { | ||
var result = _this12._parse(input, pos, end); | ||
return new P(function (input, pos, end) { | ||
var result = _this10._parse(input, pos, end); | ||
if (result instanceof Success) { | ||
result.value = fn(result.value); | ||
} | ||
if (result instanceof Success) { | ||
result.value = fn(result.value); | ||
} | ||
return result; | ||
}); | ||
} | ||
}, { | ||
key: "parse", | ||
value: function parse(input) { | ||
var pos = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; | ||
var end = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : input.length; | ||
return this._parse(input, pos, end).value; | ||
} | ||
}, { | ||
key: "repeat", | ||
value: function repeat() { | ||
var _this13 = this; | ||
return result; | ||
}); | ||
}; | ||
var min = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; | ||
var max = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Infinity; | ||
return new P(function (input, pos, end) { | ||
var value = []; | ||
var result; | ||
var i = 0; | ||
_proto11.parse = function parse(input, pos, end) { | ||
if (pos === void 0) { | ||
pos = 0; | ||
} | ||
while (i < min) { | ||
++i; | ||
result = _this13._parse(input, pos, end); | ||
if (end === void 0) { | ||
end = input.length; | ||
} | ||
if (result instanceof Failure) { | ||
return result; | ||
} | ||
return this._parse(input, pos, end).value; | ||
}; | ||
value.push(result.value); | ||
pos = result.pos; | ||
} | ||
_proto11.repeat = function repeat(min, max) { | ||
var _this11 = this; | ||
while (i < max && (result = _this13._parse(input, pos, end)) instanceof Success) { | ||
++i; | ||
value.push(result.value); | ||
pos = result.pos; | ||
} | ||
if (min === void 0) { | ||
min = 0; | ||
return new Success(pos, value); | ||
}); | ||
} | ||
}, { | ||
key: "skip", | ||
value: function skip(otherParser) { | ||
var _this14 = this; | ||
if (max === void 0) { | ||
max = Infinity; | ||
} | ||
return new P(function (input, pos, end) { | ||
var result = _this14._parse(input, pos, end); | ||
return new P(function (input, pos, end) { | ||
var value = []; | ||
var result; | ||
var i = 0; | ||
while (i < min) { | ||
++i; | ||
result = _this11._parse(input, pos, end); | ||
if (result instanceof Failure) { | ||
@@ -551,37 +725,14 @@ return result; | ||
value.push(result.value); | ||
pos = result.pos; | ||
} | ||
var otherResult = otherParser._parse(input, result.pos, end); | ||
while (i < max && (result = _this11._parse(input, pos, end)) instanceof Success) { | ||
++i; | ||
value.push(result.value); | ||
pos = result.pos; | ||
} | ||
if (otherResult instanceof Failure) { | ||
return otherResult; | ||
} | ||
return new Success(pos, value); | ||
}); | ||
}; | ||
_proto11.skip = function skip(otherParser) { | ||
var _this12 = this; | ||
return new P(function (input, pos, end) { | ||
var result = _this12._parse(input, pos, end); | ||
if (result instanceof Failure) { | ||
result.pos = otherResult.pos; | ||
return result; | ||
} | ||
}); | ||
} | ||
}]); | ||
var otherResult = otherParser._parse(input, result.pos, end); | ||
if (otherResult instanceof Failure) { | ||
return otherResult; | ||
} | ||
result.pos = otherResult.pos; | ||
return result; | ||
}); | ||
}; | ||
return P; | ||
@@ -596,6 +747,19 @@ }(); | ||
return P.seq(r.ws, r.term.repeat(), P.eof).map(function (_ref) { | ||
var terms = _ref[1]; | ||
var _ref2 = _slicedToArray(_ref, 2), | ||
terms = _ref2[1]; | ||
return terms.length === 0 ? new Null() : new And(terms); | ||
}); | ||
}, | ||
globPattern: new P(function (input, pos, end) { | ||
var value = ''; | ||
var c; | ||
while (pos < end && ((c = input[pos]) === '*' || c in RAW_STRING_CHARS)) { | ||
++pos; | ||
value += c; | ||
} | ||
return value.length === 0 ? new Failure(pos, 'a raw string') : new Success(pos, value); | ||
}), | ||
quotedString: new P(function (input, pos, end) { | ||
@@ -620,2 +784,5 @@ if (input[pos] !== '"') { | ||
}), | ||
property: function property(r) { | ||
return P.alt(r.quotedString, r.rawString); | ||
}, | ||
rawString: new P(function (input, pos, end) { | ||
@@ -632,5 +799,30 @@ var value = ''; | ||
}), | ||
string: function string(r) { | ||
return P.alt(r.quotedString, r.rawString); | ||
}, | ||
regex: new P(function (input, pos, end) { | ||
if (input[pos] !== '/') { | ||
return new Failure(pos, '/'); | ||
} | ||
++pos; | ||
var c; | ||
var pattern = ''; | ||
var escaped = false; | ||
while (pos < end && ((c = input[pos++]) !== '/' || escaped)) { | ||
escaped = c === '\\'; | ||
pattern += c; | ||
} | ||
if (c !== '/') { | ||
return new Failure(pos, '/'); | ||
} | ||
var flags = ''; | ||
if (pos < end && (c = input[pos]) === 'i') { | ||
++pos; | ||
flags += c; | ||
} | ||
return new Success(pos, new RegExpNode(pattern, flags)); | ||
}), | ||
term: function term(r) { | ||
@@ -643,5 +835,7 @@ return P.alt(P.seq(P.text('('), r.ws, r.term.repeat(1), P.text(')')).map(function (_) { | ||
return new Not(_[2]); | ||
}), P.seq(P.regex(/[<>]=?/), r.rawString).map(function (_ref2) { | ||
var op = _ref2[0], | ||
val = _ref2[1]; | ||
}), P.seq(P.regex(/[<>]=?/), r.rawString).map(function (_ref3) { | ||
var _ref4 = _slicedToArray(_ref3, 2), | ||
op = _ref4[0], | ||
val = _ref4[1]; | ||
val = +val; | ||
@@ -654,12 +848,15 @@ | ||
return new Comparison(op, val); | ||
}), P.seq(r.string, r.ws, P.text(':'), r.ws, r.term).map(function (_) { | ||
}), P.seq(r.property, r.ws, P.text(':'), r.ws, r.term).map(function (_) { | ||
return new Property(_[0], _[4]); | ||
}), P.seq(r.string, P.text('?')).map(function (_) { | ||
}), P.seq(r.property, P.text('?')).map(function (_) { | ||
return new TruthyProperty(_[0]); | ||
}), P.alt(r.quotedString.map(function (_) { | ||
}), r.value).skip(r.ws); | ||
}, | ||
value: function value(r) { | ||
return P.alt(r.quotedString.map(function (_) { | ||
return new StringNode(_); | ||
}), r.rawString.map(function (str) { | ||
}), r.regex, r.globPattern.map(function (str) { | ||
var asNum = +str; | ||
return Number.isNaN(asNum) ? new StringNode(str) : new NumberNode(asNum); | ||
}))).skip(r.ws); | ||
return Number.isNaN(asNum) ? new GlobPattern(str) : new NumberNode(asNum); | ||
})); | ||
}, | ||
@@ -671,4 +868,4 @@ ws: P.regex(/\s*/) | ||
var _getPropertyClauseStrings = function _getPropertyClauseStrings(_ref3) { | ||
var child = _ref3.child; | ||
var _getPropertyClauseStrings = function _getPropertyClauseStrings(_ref5) { | ||
var child = _ref5.child; | ||
@@ -698,5 +895,3 @@ if (child instanceof Or) { | ||
if (node instanceof Property) { | ||
var _ref4; | ||
return _ref4 = {}, _ref4[node.name] = _getPropertyClauseStrings(node), _ref4; | ||
return _defineProperty({}, node.name, _getPropertyClauseStrings(node)); | ||
} | ||
@@ -703,0 +898,0 @@ |
{ | ||
"name": "complex-matcher", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"license": "ISC", | ||
@@ -33,5 +33,5 @@ "description": "", | ||
"devDependencies": { | ||
"@babel/cli": "7.0.0-beta.40", | ||
"@babel/core": "7.0.0-beta.40", | ||
"@babel/preset-env": "7.0.0-beta.40", | ||
"@babel/cli": "7.0.0-beta.54", | ||
"@babel/core": "7.0.0-beta.54", | ||
"@babel/preset-env": "7.0.0-beta.54", | ||
"babel-plugin-lodash": "^3.3.2", | ||
@@ -38,0 +38,0 @@ "cross-env": "^5.1.1", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
61967
747
1