js-restructure
Advanced tools
+29
-3
| module.exports = function matcher(obj, flags) { | ||
| "use strict"; | ||
| var props = Object.getOwnPropertyNames(obj); | ||
| var parserArgs = []; | ||
| var re = new RegExp(props.reduce(function(p, c) { | ||
| if(!isNaN(c)) { | ||
| throw new TypeError("Object with numeric keys are not supported"); | ||
| throw new TypeError("Objects with numeric keys are not supported."+ | ||
| "Got object with key: "+c); | ||
| } | ||
| var val = obj[c]; | ||
| if(val.source) val = val.source; // accept RE arguments | ||
| // convert objects that are not matchers or REs to nested parsers | ||
| if (Object(val) === val && !val.re && !val.source) { | ||
| val = matcher(val); | ||
| } | ||
| if(val.re) { | ||
| // accept parser arguments, | ||
| parserArgs.push(val); // add the parser | ||
| val = val._nonCapturingRe; // don't capture for re arguments. | ||
| } else if (val.source) { | ||
| val = val.source; // accept RE arguments | ||
| } else { | ||
| parserArgs.push(null); | ||
| } | ||
| if(c[0] === "_") return p + val; | ||
| else return p + "(" + val + ")"; | ||
| }, ""), flags || ""); | ||
| var _nonCapturingRe = props.reduce( function(prev, cur) { | ||
| return prev + obj[cur]; | ||
| }, ""); | ||
| props = props.filter(function(x) { return x[0] !== "_"; }); | ||
@@ -19,8 +40,13 @@ var parser = function(pattern) { | ||
| for(var i = 0; i < res.length; i++) { | ||
| o[props[i]] = res[i + 1]; | ||
| if(parserArgs[i]) { // got a parser | ||
| o[props[i]] = parserArgs[i](res[i + 1]); // invoke the parser | ||
| } else { | ||
| o[props[i]] = res[i + 1]; | ||
| } | ||
| } | ||
| return o; | ||
| }; | ||
| parser._nonCapturingRe = _nonCapturingRe; // expose raw RE for nesting | ||
| parser.re = re; // expose regexp | ||
| return parser; | ||
| }; |
+1
-1
| { | ||
| "name": "js-restructure", | ||
| "version": "1.0.7", | ||
| "version": "1.1.0", | ||
| "description": "This package provides a nifty way to match against regular expressions\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001bconstruct and match against regular expressions.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
+8
-3
@@ -19,3 +19,3 @@ # js-restructure | ||
| console.log(parts.user); // benji | ||
| console.log(host); // somewhere.com | ||
| console.log(parts.host); // somewhere.com | ||
| ``` | ||
@@ -68,2 +68,7 @@ | ||
| You can nest objects and parsers inside a parser: | ||
| var parser2 = matcher({x : parser}); // can nest parsers to create nested results | ||
| var parser3 = matcher({x : {x: 3}}); // will create a nested object when parsing | ||
| A parser is itself a function that can be passed a string, it returns an object of the type passed when creating the parser or null if the parsing failed: | ||
@@ -98,6 +103,6 @@ | ||
| - <s>tests</s> | ||
| - demos | ||
| - nested match | ||
| - <s>demos</s> | ||
| - <s>nested match</s> | ||
| - measure performance | ||
| - sugar | ||
+41
-2
@@ -80,4 +80,5 @@ var matcher = require("./index.js"); | ||
| it("accepts REs ", function(){ | ||
| var m = matcher({ x: /a/})("a"); | ||
| assert.equal(m.x, "a"); | ||
| var m = matcher({ x: /a/}); | ||
| var p = m("a"); | ||
| assert.equal(p.x, "a"); | ||
| }); | ||
@@ -135,2 +136,40 @@ it("accepts multiple REs ", function(){ | ||
| }); | ||
| }); | ||
| describe("the parser", function() { | ||
| it("should accept nested objects", function() { | ||
| var p = matcher({x: {x:"A"}})("A"); | ||
| assert.equal(p.x.x, "A"); | ||
| }); | ||
| it("should accept multiple nested objects", function() { | ||
| var p = matcher({x: {x:"A"}, y: {y: "B"}})("AB"); | ||
| assert.equal(p.x.x, "A"); | ||
| assert.equal(p.y.y, "B"); | ||
| }); | ||
| it("should accept deep nested objects", function() { | ||
| var p = matcher({x: {x:"A", y: "B"}})("AB"); | ||
| assert.equal(p.x.x, "A"); | ||
| assert.equal(p.x.y, "B"); | ||
| }); | ||
| it("should accept nested matchers", function() { | ||
| var m1 = matcher({x: "A"}); | ||
| var m2 = matcher({x: m1 }); | ||
| var p = m2("A"); | ||
| assert.equal(p.x.x, "A"); | ||
| }); | ||
| it("should accept the same matcher twice", function() { | ||
| var m1 = matcher({x: "A"}); | ||
| var m2 = matcher({x: m1, y:m1 }); | ||
| var p = m2("AA"); | ||
| assert.equal(p.x.x, "A"); | ||
| assert.equal(p.y.x, "A"); | ||
| }); | ||
| it("should accept the same matcher twice", function() { | ||
| var m1 = matcher({x: "A"}); | ||
| var m2 = matcher({x: m1, y:m1 }); | ||
| var p = m2("AA"); | ||
| assert.equal(p.x.x, "A"); | ||
| assert.equal(p.y.x, "A"); | ||
| }); | ||
| }); |
AI-detected potential code anomaly
Supply chain riskAI has identified unusual behaviors that may pose a security risk.
Found 1 instance in 1 package
10847
26.01%216
37.58%106
4.95%2
100%