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.2 to 1.0.3

4

index.js

@@ -9,3 +9,3 @@ module.exports = function matcher(obj) {

props = props.filter(function(x) { return !x.startsWith("_"); });
return function(pattern) {
var parser = function(pattern) {
var o = {};

@@ -19,2 +19,4 @@ var res = re.exec(pattern);

};
parser.re = re; // expose regexp
return parser;
};
{
"name": "js-restructure",
"version": "1.0.2",
"version": "1.0.3",
"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
## Installation
This package allows you to express regular expressions by defining an object, and then capture matched sub-expressions into object's fields.
npm install js-restructure
Here is a very simple email address parser:
## Match regular expressions into object fields
Example email matcher
```js
matcher({
var parser = matcher({
_ : "^",

@@ -18,11 +15,73 @@ user : "\\w+",

_3 : "$"
})("benji@tipranks.com")
});
var parts = parser("benji@somewhere.com");
console.log(parts.user); // benji
console.log(host); // somewhere.com
```
Example primitive URL parser:
```js
var m = matcher({
protocol : "http|https",
_1 : "://",
host : "[^/]+",
_2 : "/",
path : ".+",
_3 : "$"
})("http://www.google.com/search?foo=bar");
m.protocol; // http
m.host; // www.google.com
m.path; // search?foo=bar
```
Note: In "real code" use the built in parsing capabilities of browsers/node to parse real URLs.
## Installation
npm install js-restructure
Or in the browser
<script src='https://wzrd.in/standalone/js-restructure@latest'></script>
Usage in script tag:
var matcher = window.jsRestructure(...);
## API
The basic fundamental unit of js-restructure is a parser. A parser is created by passing an object to the matcher function (the one you `require`, in the browser this is `window.jsRestructure`).
Creating a parser:
matcher(T object) -> Parser
For example:
var parser = matcher({x : A });
**Note:** The parser ignores properties that start with `_`, if you have properties that you do not want to capture but need to specify parts of the RE, start them with `_`.
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:
Parser:: (string) => T?
For example:
var parser = matcher({x : A });
parser("A").x; // A
parser("B"); // null, no match
## Todo
- put in module
- npm package
- cdn etc.
- tests
- <s>put in module</s>
- <s>npm package</s>
- <s>cdn etc.</s>
- <s>tests</s>
- demos
- nested match
- measure performance
- sugar

@@ -36,2 +36,39 @@ var matcher = require("./index.js");

});
it("matches primitive http/s urls", function(){
var m = matcher({
protocol : "http|https",
_1 : "://",
host : "[^/]+",
_2 : "/",
path : ".+",
_3 : "$"
})("http://www.google.com/search?foo=bar")
assert.equal(m.protocol, "http");
assert.equal(m.host, "www.google.com");
assert.equal(m.path, "search?foo=bar");
});
it("parses 'primitive' CSV", function(){
var p = matcher({
first : "\\w+?",
_1 : ",",
last : "\\w+?",
_2 : ",",
age : "\\d+?",
_3 : "$"
});
var m = p("Benjamin,Gruenbaum,27")
assert.equal(m.first, "Benjamin");
assert.equal(m.last, "Gruenbaum");
assert.equal(m.age, "27");
});
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");
});
});
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