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

occamsrazor-match

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

occamsrazor-match - npm Package Compare versions

Comparing version 3.0.0 to 4.0.0

extra/greaterThan.js

19

extra/has.js

@@ -1,13 +0,16 @@

var match = require('../lib/match');
var setName = require('../lib/setName');
var logger = require('../lib/logger');
module.exports = function has(args) {
if (!Array.isArray(args)) throw new Error('"has": requires an array');
var i, out = {};
for (i = 0, len = args.length; i < len; i++) {
if (typeof args[i] !== 'string') {
throw new Error('Occamsrazor (has): The arguments can only be strings');
var newfunc = function (o) {
for (var i = 0, len = args.length; i < len; i++) {
if (!(args[i] in o)) {
return false;
}
}
out[args[i]] = undefined;
}
return match(out);
return true;
};
return setName(newfunc, 'has(' + args.join(',') + ')');
return logger(newfunc);
};

@@ -99,3 +99,3 @@ var setName = require('./setName');

currentPath = path ? path + '.' + k : k;
if (!getHasAttr(k)(obj, callback, currentPath) || !functions[k](obj[k], callback, currentPath)) {
if (!functions[k](obj[k], callback, currentPath)) {
if (!callback) {

@@ -102,0 +102,0 @@ return false;

{
"name": "occamsrazor-match",
"version": "3.0.0",
"version": "4.0.0",
"description": "helper library for writing validators",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -37,2 +37,9 @@ occamsrazor-match

var every = require('occamsrazor-match/extra/every');
var greaterThan = require('occamsrazor-match/extra/greaterThan');
var lessThan = require('occamsrazor-match/extra/lessThan');
var isUndefined = require('occamsrazor-match/extra/isDefined');
var isDefined = require('occamsrazor-match/extra/isUndefined');
var isNumber = require('occamsrazor-match/extra/isNumber');
var isString = require('occamsrazor-match/extra/isString');
```

@@ -59,3 +66,3 @@

Using undefined you create a validator that matches any value:
Using undefined you create a validator that matches any value.
```js

@@ -67,2 +74,3 @@ var isAnything = match(undefined);

```
To match **undefined** there is an helper (isUndefined), explained below. Also if you wish to use a more explicit style, you can use the isAnything helper.

@@ -90,3 +98,3 @@ Using a regular expression, the validator will run that, on the value.

```js
var isPoint = match({ x : undefined, y: undefined });
var isPoint = match({ x : isNumber, y: isNumber });
isPoint({ x: 1, y: 2 }); // true

@@ -96,4 +104,3 @@ isPoint({ x: 1, y: 2, z: 3 }); // true

```
Object matching ensures the object has all the properties 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.
Passing values as undefined indicates that I don't really care about their value. Or if they are defined
```js

@@ -103,6 +110,7 @@ var isPointOnXaxis = match({ x : 0, y: undefined });

isPointOnXaxis({ x: 0, y: 2 }); // true
isPointOnXaxis({ x: 0 }); // true
```
Values will be interpreted as specified before so I can implement recursive validation:
```js
function isNumber(n) { return typeof n === 'number'; }
function isSalary(n) { return typeof n === 'number' && n > 0; }

@@ -113,3 +121,3 @@ var isEmployee = match({

position: undefined,
salary: isNumber
salary: isSalary
}

@@ -125,13 +133,15 @@ });

```
In Arrays the behaviour of undefined comes useful:
```js
var thirdArgIsNumber = match([undefined, undefined, isNumber]);
thirdArgIsNumber(['a', 'b', 3, 4]); // true
thirdArgIsNumber(['a', 'b', 'c']); // false
```
has
===
This is a shortcut for a very common match:
This checks for the existence of a list of attributes:
```js
var isPoint = has(['x', 'y']);
```
is equivalent to:
```js
var isPoint = match({ x : undefined, y: undefined });
```

@@ -198,3 +208,34 @@ isInstanceOf

greaterThan, lessThan
=====================
They are be useful to validate if a number is in a specific range.
Example:
```js
var isGreaterThan0 = greaterThan(0);
isGreaterThan0(1); // true
isGreaterThan0(-1); // false
var isValidDiscount = and(greaterThan(0), lessThan(100));
```
Common validators
=================
The library includes a series of very common validators:
* isUndefined
* isDefined
* isString
* isNumber
* isAnything (always returns true)
Example:
```js
var isEmployee = match({
id: isDefined,
name: isString,
salary: isNumber,
title: or(isUndefined, 'mr', 'ms')
});
```
Custom validator

@@ -283,1 +324,28 @@ ================

```
Validate decorator
==================
A very common use case for validation is checking if the arguments of a function match a certain criteria. Because of this I have included a function decorator that checks the arguments of the decorated function and throws an error if they don't match:
```js
var validate = require('occamsrazor-match/validate-decorators');
var sum = validate(isNumber, isNumber, isNumber)(function sum(a, b, c) {
return a + b + c;
});
```
if supported you can use the succint es7 syntax:
```js
@validate(isNumber, isNumber, isNumber)
function sum(a, b, c) {
return a + b + c;
}
```
The error object contains a description of all the things went wrong:
```js
try {
var result = sum('1', 2, 3);
} catch (e) {
console.log(e.message); // Function called with wrong arguments: array:[isNumber,isNumber,isNumber]
console.log(e.errors); // [{ path: [0], name: 'isNumber', value: '1' }]
}
```

@@ -11,2 +11,5 @@ var assert = require('chai').assert;

var some = require('../extra/some');
var isUndefined = require('../extra/isUndefined');
var greaterThan = require('../extra/greaterThan');
var lessThan = require('../extra/lessThan');

@@ -192,1 +195,31 @@ describe('isPrototype/isInstanceOf', function () {

});
describe('isUndefined', function () {
it('must match', function () {
assert.isTrue(isUndefined(undefined));
});
it('must not match', function () {
assert.isFalse(isUndefined('defined'));
});
});
describe('number', function () {
it('must be a number', function () {
var isNumber = greaterThan();
assert.isTrue(isNumber(1));
assert.isFalse(isNumber('1'));
});
it('must be greater than', function () {
var greaterThan0 = greaterThan(0);
assert.isTrue(greaterThan0(1));
assert.isFalse(greaterThan0(-1));
});
it('must be greater than', function () {
var lessThan0 = lessThan(0);
assert.isTrue(lessThan0(-1));
assert.isFalse(lessThan0(1));
});
});

@@ -65,11 +65,2 @@ var assert = require('chai').assert;

var validator = match({ key1: 1, key2: 2 });
validator({ key1: 1 }, errors);
assert.deepEqual(errors(), [
{ path: 'key2', name: 'hasAttribute', value: { key1: 1 } }]);
});
it('must log an object, validate false', function () {
var errors = validationErrors();
var validator = match({ key1: 1, key2: 2 });
validator({ key1: 3, key2: 2 }, errors);

@@ -76,0 +67,0 @@

@@ -30,4 +30,4 @@ var assert = require('chai').assert;

assert.isFalse(hasWidth({}));
assert.isFalse(hasHeight_hasWidth({ height: 2 }));
assert.isTrue(hasWidth({}));
assert.isTrue(hasHeight_hasWidth({ height: 2 }));
});

@@ -34,0 +34,0 @@

@@ -49,3 +49,3 @@ var assert = require('chai').assert;

it('must return name', function () {
assert.equal(has(['test1', 'test2']).name, 'object:{test1:isAnything,test2:isAnything}');
assert.equal(has(['test1', 'test2']).name, 'has(test1,test2)');
});

@@ -52,0 +52,0 @@ });

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