-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
5 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
packages/n4s/src/enforce/compounds/__tests__/isArrayOf.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import enforce from 'enforce' | ||
import isArrayOf from 'isArrayOf' | ||
|
||
describe('Tests isArrayOf rule', () => { | ||
|
||
it('Should return true if all elements are ture for one or more rules', () => { | ||
expect(isArrayOf([3,4,5,'six',7], | ||
enforce.greaterThan(2), | ||
enforce.isString()) | ||
).toBe(true); | ||
|
||
expect(isArrayOf([3,4,5,'six',7], | ||
enforce.greaterThan(2), | ||
enforce.isString()) | ||
).toBe(true); | ||
}); | ||
|
||
describe('Tests for recursive call', ()=>{ | ||
|
||
it('Should return true if all elements are ture for one or more rules', () => { | ||
expect(isArrayOf([3,4,5,['s','i','x'],7], | ||
enforce.greaterThan(2), | ||
enforce.isArrayOf(enforce.isString())) | ||
).toBe(true); | ||
|
||
expect(isArrayOf([[1,0,1,0],[0,0,1,0],[0,1,0,0]], | ||
enforce.isArrayOf(enforce.equals(0),enforce.equals(1)) | ||
)).toBe(true); | ||
}); | ||
|
||
it('Should return false if one element or more fails all rules', () => { | ||
expect( | ||
isArrayOf([3,4,5,['s','i','x'],7], | ||
enforce.greaterThan(2), | ||
enforce.isArrayOf(enforce.isNumber())) | ||
).toBe(false); | ||
}); | ||
|
||
expect(isArrayOf([[1,0,1,0],[0,0,1,'not 0/1'],[0,1,0,0]], | ||
enforce.isArrayOf(enforce.equals(0),enforce.equals(1)) | ||
)).toBe(false); | ||
}); | ||
|
||
describe('as part of enforce', () => { | ||
|
||
it('should return silently when valid', () => { | ||
enforce([1,2,'3']).isArrayOf(enforce.isNumber(), enforce.isString()); | ||
enforce([1,2,'3']).isArrayOf(enforce.isNumeric(), enforce.lessThan(5).greaterThan(0)); | ||
enforce([[0,1,0,1],[1,1,1,1],[0,1,1,0]]).isArrayOf(enforce.isArrayOf(enforce.equals(0),enforce.equals(1)).lengthEquals(4)); | ||
}); | ||
|
||
it('should throw an exception when invalid', () => { | ||
expect(() => enforce([1,2,'3']).isArrayOf(enforce.isNull())).toThrow() | ||
expect(() => enforce([1,2,'3']).isArrayOf(enforce.isNumber(), enforce.greaterThan(5))).toThrow() | ||
expect(() => enforce([[0,1,0,1],[1,'not 0/1',1,1],[0,1,1,0]]).isArrayOf(enforce.isArrayOf(enforce.equals(0),enforce.equals(1))).lengthEquals(4)).toThrow() | ||
expect(() => enforce([[0,1,0,1],[1,1,1],[0,1,1,0]]).isArrayOf(enforce.isArrayOf(enforce.equals(0),enforce.equals(1))).lengthEquals(4)).toThrow() | ||
}); | ||
|
||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import isArrayOf from 'isArrayOf'; | ||
import optional from 'optional'; | ||
import shape from 'shape'; | ||
|
||
export default { | ||
isArrayOf, | ||
optional, | ||
shape, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { isNotArray } from 'isArray' | ||
import runLazyRules from 'runLazyRules'; | ||
|
||
export default function isArrayOf(value, ...ruleChain) { | ||
if (isNotArray(value)) { return false }; | ||
return ( value.every(element => ruleChain.some(rule => runLazyRules(rule, element) ))); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters