js-restructure
Advanced tools
Comparing version 1.0.3 to 1.0.4
12
index.js
@@ -1,8 +0,10 @@ | ||
module.exports = function matcher(obj) { | ||
module.exports = function matcher(obj, flags) { | ||
"use strict"; | ||
var props = Object.getOwnPropertyNames(obj); | ||
var re = new RegExp(props.reduce(function(p, c) { | ||
if(c.indexOf("_") === 0) return p + obj[c]; | ||
else return p + "(" + obj[c] + ")"; | ||
}, "")); | ||
var val = obj[c]; | ||
if(val.source) val = val.source; // accept RE arguments | ||
if(c.indexOf("_") === 0) return p + val; | ||
else return p + "(" + val + ")"; | ||
}, ""), flags || ""); | ||
props = props.filter(function(x) { return !x.startsWith("_"); }); | ||
@@ -20,2 +22,2 @@ var parser = function(pattern) { | ||
return parser; | ||
}; | ||
}; |
{ | ||
"name": "js-restructure", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"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", |
# js-restructure | ||
JavaScript clone of https://github.com/alexflint/go-restructure | ||
Based on https://github.com/alexflint/go-restructure | ||
This package allows you to express regular expressions by defining an object, and then capture matched sub-expressions into object's fields. | ||
@@ -11,3 +12,3 @@ | ||
_ : "^", | ||
user : "\\w+", | ||
user : "\\w+", // can also pass a JS RegExp here | ||
_2 : "@", | ||
@@ -27,3 +28,3 @@ host : "[^@]+", | ||
protocol : "http|https", | ||
_1 : "://", | ||
_1 : "://", // can also pass a RegExp here | ||
host : "[^/]+", | ||
@@ -70,3 +71,3 @@ _2 : "/", | ||
Parser:: (string) => T? | ||
Parser:: (string[, flags]) => T? | ||
@@ -78,3 +79,6 @@ For example: | ||
parser("B"); // null, no match | ||
The second flags parameter is optional and allows setting RE flags like "i" for case insensitivity. | ||
The parser also exposes a `.re` property so the resulting RegExp can be checked and reused. | ||
## Todo | ||
@@ -81,0 +85,0 @@ |
92
tests.js
@@ -27,8 +27,8 @@ var matcher = require("./index.js"); | ||
var m = matcher({ | ||
_ : "^", | ||
user : "\\w+", | ||
_2 : "@", | ||
host : "[^@]+", | ||
_3 : "$" | ||
})("benji@nono.com") | ||
_ : "^", | ||
user : "\\w+", | ||
_2 : "@", | ||
host : "[^@]+", | ||
_3 : "$" | ||
})("benji@nono.com") | ||
assert.equal(m.user, "benji"); | ||
@@ -39,9 +39,9 @@ assert.equal(m.host, "nono.com"); | ||
var m = matcher({ | ||
protocol : "http|https", | ||
_1 : "://", | ||
host : "[^/]+", | ||
_2 : "/", | ||
path : ".+", | ||
_3 : "$" | ||
})("http://www.google.com/search?foo=bar") | ||
protocol : "http|https", | ||
_1 : "://", | ||
host : "[^/]+", | ||
_2 : "/", | ||
path : ".+", | ||
_3 : "$" | ||
})("http://www.google.com/search?foo=bar") | ||
assert.equal(m.protocol, "http"); | ||
@@ -54,10 +54,10 @@ assert.equal(m.host, "www.google.com"); | ||
var p = matcher({ | ||
first : "\\w+?", | ||
_1 : ",", | ||
last : "\\w+?", | ||
_2 : ",", | ||
age : "\\d+?", | ||
_3 : "$" | ||
}); | ||
var m = p("Benjamin,Gruenbaum,27") | ||
first : "\\w+?", | ||
_1 : ",", | ||
last : "\\w+?", | ||
_2 : ",", | ||
age : "\\d+?", | ||
_3 : "$" | ||
}); | ||
var m = p("Benjamin,Gruenbaum,27") | ||
assert.equal(m.first, "Benjamin"); | ||
@@ -68,10 +68,46 @@ assert.equal(m.last, "Gruenbaum"); | ||
it("parses HTML", function(){ | ||
var he = matcher({ | ||
_1 : "<a href='", | ||
url : ".+?", | ||
_2 : "'.*", | ||
}); | ||
var comes = he("<a href='http://www.google.com'>Google!</a>"); | ||
assert.equal(comes.url, "http://www.google.com"); | ||
var he = matcher({ | ||
_1 : "<a href='", | ||
url : ".+?", | ||
_2 : "'.*", | ||
}); | ||
var comes = he("<a href='http://www.google.com'>Google!</a>"); | ||
assert.equal(comes.url, "http://www.google.com"); | ||
}); | ||
it("accepts REs ", function(){ | ||
var m = matcher({ x: /a/})("a"); | ||
assert.equal(m.x, "a"); | ||
}); | ||
it("accepts multiple REs ", function(){ | ||
var m = matcher({ | ||
x: /a/, | ||
y: /A+/ | ||
})("aAAAAA"); | ||
assert.equal(m.x, "a"); | ||
assert.equal(m.y, "AAAAA"); | ||
}); | ||
it("matches emails with RE syntax", function(){ | ||
var m = matcher({ | ||
_ : /^/, | ||
user : /\w+/, | ||
_2 : /@/, | ||
host : /[^@]+/, | ||
_3 : /$/ | ||
})("benji@nono.com") | ||
assert.equal(m.user, "benji"); | ||
assert.equal(m.host, "nono.com"); | ||
}); | ||
it("accepts flags as a second parameter", function(){ | ||
var m = matcher({ | ||
x: "a+", | ||
}, "i")("aAAAAA"); | ||
assert.equal(m.x, "aAAAAA"); | ||
}); | ||
it("behaves like native RE on invalid flags", function(){ | ||
assert.throws(function() { | ||
var m = matcher({ | ||
x: "a+", | ||
}, "13qwshfd")("aAAAAA"); | ||
); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7470
128
91