occamsrazor-match
Advanced tools
Comparing version 2.1.1 to 2.2.0
{ | ||
"name": "occamsrazor-match", | ||
"version": "2.1.1", | ||
"version": "2.2.0", | ||
"description": "helper library for writing validators", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -32,2 +32,4 @@ occamsrazor-match | ||
var and = require('occamsrazor-match/extra/and'); | ||
var some = require('occamsrazor-match/extra/some'); | ||
var every = require('occamsrazor-match/extra/every'); | ||
``` | ||
@@ -170,2 +172,19 @@ | ||
some/every | ||
========== | ||
This couple of validators can be used to validate arrays. It takes a validator as argument (it uses "match" behind the scene) and checks the array items against that validator. In the case of "some", at least a check should pass to return true. In the case of "every", all of them should pass. | ||
```js | ||
var atLeastOne5 = some(5); | ||
atLeastOne5([1, 2, 5]); // true | ||
atLeastOne5([5, 5, 2]); // true | ||
atLeastOne5([1, 2, 3]); // false | ||
``` | ||
```js | ||
var allNegatives = every(function isNegative(n) { return n < 0; }); | ||
allNegatives([-1, -2, -5]); // true | ||
allNegatives([-5, 5, -2]); // false | ||
``` | ||
Custom validator | ||
@@ -172,0 +191,0 @@ ================ |
@@ -8,2 +8,5 @@ var assert = require('chai').assert; | ||
var or = require('../extra/or'); | ||
var arrayOf = require('../extra/arrayOf'); | ||
var every = require('../extra/every'); | ||
var some = require('../extra/some'); | ||
@@ -113,1 +116,77 @@ describe('isPrototype/isInstanceOf', function () { | ||
}); | ||
describe('arrayOf', function () { | ||
var areFive; | ||
before(function () { | ||
areFive = arrayOf(5); | ||
}); | ||
it('must match', function () { | ||
assert.isTrue(areFive([5])); | ||
}); | ||
it('must match (2)', function () { | ||
assert.isTrue(areFive([5, 5])); | ||
}); | ||
it('must not match', function () { | ||
assert.isFalse(areFive([3])); | ||
}); | ||
it('must not match (2)', function () { | ||
assert.isFalse(areFive([5, 3])); | ||
}); | ||
}); | ||
describe('every', function () { | ||
var areFive; | ||
before(function () { | ||
areFive = every(5); | ||
}); | ||
it('must match', function () { | ||
assert.isTrue(areFive([5])); | ||
}); | ||
it('must match (2)', function () { | ||
assert.isTrue(areFive([5, 5])); | ||
}); | ||
it('must not match', function () { | ||
assert.isFalse(areFive([3])); | ||
}); | ||
it('must not match (2)', function () { | ||
assert.isFalse(areFive([5, 3])); | ||
}); | ||
}); | ||
describe('some', function () { | ||
var someFive; | ||
before(function () { | ||
someFive = some(5); | ||
}); | ||
it('must match', function () { | ||
assert.isTrue(someFive([5])); | ||
}); | ||
it('must match (2)', function () { | ||
assert.isTrue(someFive([5, 5])); | ||
}); | ||
it('must not match', function () { | ||
assert.isFalse(someFive([3])); | ||
}); | ||
it('must not match (2)', function () { | ||
assert.isTrue(someFive([5, 3])); | ||
}); | ||
it('must not match (3)', function () { | ||
assert.isTrue(someFive([3, 5])); | ||
}); | ||
}); |
@@ -7,2 +7,3 @@ var assert = require('chai').assert; | ||
var or = require('../extra/or'); | ||
var arrayOf = require('../extra/arrayOf'); | ||
@@ -200,2 +201,26 @@ var isPrototypeOf = require('../extra/isPrototypeOf'); | ||
describe('arrayOf', function () { | ||
it('must log arrayOf', function () { | ||
var result = validationResult(true); | ||
var validator = arrayOf(5); | ||
validator([5, 5], result); | ||
assert.deepEqual(result(), [ | ||
{ path: '', name: 'isArray', result: true, value: [5, 5] }, | ||
{ path: '[0]', name: 'isNumber:5', result: true, value: 5 }, | ||
{ path: '[1]', name: 'isNumber:5', result: true, value: 5 }, | ||
]); | ||
}); | ||
it('must log arrayOf, one fails', function () { | ||
var result = validationResult(true); | ||
var validator = arrayOf(5); | ||
validator([2, 5], result); | ||
assert.deepEqual(result(), [ | ||
{ path: '', name: 'isArray', result: true, value: [2, 5] }, | ||
{ path: '[0]', name: 'isNumber:5', result: false, value: 2 }, | ||
{ path: '[1]', name: 'isNumber:5', result: true, value: 5 }, | ||
]); | ||
}); | ||
}); | ||
describe('match - composed object', function () { | ||
@@ -202,0 +227,0 @@ it('must log composed object', function () { |
@@ -9,2 +9,3 @@ var assert = require('chai').assert; | ||
var or = require('../extra/or'); | ||
var arrayOf = require('../extra/arrayOf'); | ||
@@ -77,3 +78,8 @@ describe('validator name', function () { | ||
}); | ||
describe('arrayOf', function () { | ||
it('must return name', function () { | ||
assert.equal(arrayOf(5).name, 'every(isNumber:5)'); | ||
}); | ||
}); | ||
}); |
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
38729
24
845
270