reworked ARRAY(..) + docs...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2018-08-19 03:39:06 +03:00
parent c23bbb75d1
commit 8dd3bf064d
2 changed files with 56 additions and 8 deletions

View File

@ -23,6 +23,7 @@
- [Logic patterns](#logic-patterns)
- [String patterns](#string-patterns)
- [Number patterns](#number-patterns)
- [Array patterns](#array-patterns)
- [Patterns (EXPERIMENTAL)](#patterns-experimental)
- [JSON compatibility](#json-compatibility)
- [Extending Diff](#extending-diff)
@ -72,6 +73,11 @@ console.log(diff.diff)
Here's how different `Bill` and `Ted` really are (or how the *diff* looks like):
```javascript
// log out the relevant part...
console.log(diff.diff)
```
And the output is:
```javascript
[
{
"path": ["name"],
@ -363,15 +369,29 @@ Match a string via a nested pattern.
Matches a number
### Array patterns
`ARRAY`
Matches any array.
`ARRAY(length)`
Matches an array of length `length`.
`ARRAY(func)`
Match if `func` returns true when applied to each array item.
`ARRAY(pattern)`
Match if `pattern` matches each array item.
`ARRAY(x, y, ..)`
A combination of the above where `x`, `y` and `..` may be any of *length*, *functions* or *patterns*.
This is a shorthand for: `AND(ARRAY(x), ARRAY(y), ..)`
XXX examples...
## Patterns (EXPERIMENTAL)
`ARRAY`
`ARRAY(length)`
`IN(A)`
`AT(A, K)`

36
diff.js
View File

@ -298,6 +298,7 @@ var makeCIPattern = function(name, check, init){
return object.makeConstructor(name, o, o)
}
// Singleton ANY...
//
// ANY
@ -306,7 +307,8 @@ var makeCIPattern = function(name, check, init){
var ANY =
module.ANY =
makeCIPattern('ANY',
function(obj, cmp){ return true })()
function(){ return true })()
// String pattern...
//
@ -379,15 +381,41 @@ module.NUMBER =
//
// ARRAY
// ARRAY(length)
// ARRAY(func)
// ARRAY(pattern)
// ARRAY(test, ...)
// -> pattern
//
// XXX support length, types, ...
// NOTE: func and pattern if given are applied to each array item and
// the match is made iff for each item the function returns true or
// the pattern matches.
// NOTE: multiple tests (length, func, pattern) can be combined in any
// order, this is a shorthand:
// ARRAY(4, STRING)
// is the same as:
// AND(ARRAY(4), ARRAY(STRING))
// NOTE: order of arguments is not important, but it is possible to add
// a set of conflicting arguments...
var ARRAY =
module.ARRAY =
makeCIPattern('ARRAY',
function(obj, cmp){
// XXX do the .value test....
return obj === ARRAY || obj instanceof Array },
return obj === ARRAY
//|| (obj instanceof Array && this.value.length == 0)
|| (obj instanceof Array
// XXX make this fail on first fail -- currently
// this runs every test on every elem...
&& this.value.filter(function(value){
return (typeof(value) == typeof(123) ?
obj.length == value
// function...
: value instanceof Function ?
obj.filter(value).length == obj.length
// pattern...
: obj.filter(function(e){
return cmp(value, e)
}).length == obj.length)
}).length == this.value.length) },
function(...value){ this.value = value })