Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

js-restructure

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

js-restructure - npm Package Compare versions

Comparing version 1.0.7 to 1.1.0

32

index.js
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;
};

2

package.json
{
"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",

@@ -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

@@ -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");
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc