Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
pattern-match
Advanced tools
A pattern matching DSL for JavaScript. The module is a function that
takes an arbitrary JavaScript value and tests it against a
pattern. If the match succeeds, the result is a sub-match object,
which consists of the sub-components of the value that matched named
sub-patterns (using the var
pattern). If the match fails, a
MatchError
is thrown.
Here's a simple example of using pattern matching to analyze an AST for a hypothetical language:
var match = require('pattern-match');
match(ast, function(when) {
when({
type: 'FunctionCall',
callee: match.var('callee'),
args: match.var('args')
}, function(vars) {
this.analyzeFunctionCall(vars.callee, vars.args);
}, this);
when({
type: 'Assignment',
lhs: match.var('lhs'),
rhs: match.var('rhs')
}, function(vars) {
this.analyzeAssignment(vars.lhs, vars.rhs);
}, this);
when({
type: 'Return',
arg: match.var('arg')
}, function(vars) {
this.analyzeReturn(vars.arg);
}, this);
}, this);
This will get sweeter in ES6 with destructuring:
var match = require('pattern-match');
match(ast, function(when) {
when({
type: 'FunctionCall',
callee: match.var('callee'),
args: match.var('args')
}, function({ callee, args }) {
this.analyzeFunctionCall(callee, args);
}, this);
when({
type: 'Assignment',
lhs: match.var('lhs'),
rhs: match.var('rhs')
}, function({ lhs, rhs }) {
this.analyzeAssignment(lhs, rhs);
}, this);
when({
type: 'Return',
arg: match.var('arg')
}, function({ arg }) {
this.analyzeReturn(arg);
}, this);
}, this);
And sweeter still with ES6 arrow-functions:
var match = require('pattern-match');
match(ast, (when) => {
when({
type: 'FunctionCall',
callee: match.var('callee'),
args: match.var('args')
}, ({ callee, args }) => {
this.analyzeFunctionCall(callee, args);
});
when({
type: 'Assignment',
lhs: match.var('lhs'),
rhs: match.var('rhs')
}, ({ lhs, rhs }) => {
this.analyzeAssignment(lhs, rhs);
});
when({
type: 'Return',
arg: match.var('arg')
}, ({ arg }) => {
this.analyzeReturn(arg);
});
});
Match x
against a sequence of patterns, returning the result of the
first successful match. The cases are provided by the body
function:
Provides the cases by calling when
in the order the cases should be
tried. The library calls body
with the thisArg
provided to match
as the binding of this
.
Provides the next case, consisting of a pattern an optional
template. If matching the pattern succeeds, the result is passed to
template
with thisArg
bound to this
(defaults to the global
object). If template
is not provided, this case produces the
sub-match object.
Match x
against a single pattern. Returns the result of calling
template
on the sub-match object with thisArg
(or the global
object by default) as the binding of this
. If template
is not
provided, returns the sub-match object.
Array.isArray
matches.null
value.undefined
value.low
, high
).pattern
(defaults to any
) and saves the value in the sub-match object with property name name
.pattern
matches.pattern
matches.pred
returns a truthy value.x1
to xn
matching patterns pattern1
to patternn
, respectively. Only the own properties of the pattern are used.pattern0
to patternn
, respectively.You can create custom patterns by extending the root pattern prototype.
Error
that represents a failed pattern-match.
FAQs
A pattern-matching DSL for JSON-like trees of data.
The npm package pattern-match receives a total of 11 weekly downloads. As such, pattern-match popularity was classified as not popular.
We found that pattern-match demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.