Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
occamsrazor-match
Advanced tools
This is an helper library for writing validators.
A validator is a function. When it runs against an object, it returns true or false.
var isFive = function(o) {
return o === 5;
}
isFive(5); // true
isFive(4); // false
Writing complex validators may be a bit verbose. This library helps you to write short yet expressive validators.
This library contains a main helper called "match" and 3 extra ones: has, isInstanceOf and isPrototypeOf.
var match = require('occamsrazor-match');
var has = require('occamsrazor-match/extra/has');
var isInstanceOf = require('occamsrazor-match/extra/isInstanceOf');
var isPrototypeOf = require('occamsrazor-match/extra/isPrototypeOf');
This is the main helper, and can be used to create a lot of different validators.
These one matches the value in it.
var isFive = match(5);
var isNull = match(null);
var isTrue = match(true);
var isFalse = match(true);
var isHello = match('hello');
For example:
isHello('hello'); // true
isFive(5); // true
Using undefined you create a validator that matches any value:
var isAnything = match(undefined);
isAnything(5);
isAnything('hello');
isAnything({ greeting: 'hello' });
Using a regular expression, the validator will run that, on the value.
var doesMatch = match(/[0-9]+/);
doesMatch('123'); // true
Passing a function, the function itself will be returned:
var isLessThan5 = match(function (n) {
return n < 5;
});
isLessThan5(2); // true
isLessThan5(8); // false
It looks like there is no real reason to do it, this will make more sense in a bit.
"match" can take an array or an object and perform nested validation:
var isPoint = match({ x : undefined, y: undefined });
isPoint({ x: 1, y: 2 }); // true
isPoint({ x: 1, y: 2, z: 3 }); // true
isPoint({ x: 1, z: 3 }); // false
Object matching ensures the object has all the property specified. It is not in the scope of the library find out what an object isn't, but rather what it is. Passing values as undefined indicates that I don't really care about their value.
var isPointOnXaxis = match({ x : 0, y: undefined });
isPointOnXaxis({ x: 1, y: 2 }); // false
isPointOnXaxis({ x: 0, y: 2 }); // true
Values will be interpreted as specified before so I can implement recursive validation:
function isNumber(n) { return typeof n === 'number'; }
var isEmployee = match({
name : /[A-Za-z]+/,
job: {
position: undefined,
salary: isNumber
}
});
Arrays are supported as well:
var startsWith123 = match([1, 2, 3]);
startsWith123([1, 2, 3, 4]); // true
startsWith123([2, 3, 4]); // false
This is a shortcut for a very common match:
var isPoint = has('x', 'y');
is equivalent to:
var isPoint = match({ x : undefined, y: undefined });
Checks if an object has been built with a specific factory function:
var isPoint = isInstanceOf(Point);
Checks if an object is the prototype of another:
var isPoint = isPrototypeOf(Point.prototype);
Being able to use functions, you can mix-up and reuse validators:
var isPoint = match({ x : undefined, y: undefined });
var isSquare = match([isPoint, isPoint, isPoint, isPoint]);
var isTriangle = match([isPoint, isPoint, isPoint]);
var containsSquareAndTriangle = {
triangle: isTriangle,
square: isSquare,
};
Helpers provide always a descriptive name to the generated functions:
match(true).name === 'isTrue'
match(2).name === 'isNumber:2'
match([1,'test']).name === 'isArray:[isNumber:1,isString:test]'
has('test1', 'test2').name === 'isObject:{test1:isAnything,test2:isAnything}'
This can be very helpful for debugging.
FAQs
helper library for writing validators
The npm package occamsrazor-match receives a total of 5 weekly downloads. As such, occamsrazor-match popularity was classified as not popular.
We found that occamsrazor-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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.