From 8dd3bf064d8be60cce23397a137a82b31c4dc68c Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Sun, 19 Aug 2018 03:39:06 +0300 Subject: [PATCH] reworked ARRAY(..) + docs... Signed-off-by: Alex A. Naanou --- README.md | 28 ++++++++++++++++++++++++---- diff.js | 36 ++++++++++++++++++++++++++++++++---- 2 files changed, 56 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 1cb651a..35f7390 100644 --- a/README.md +++ b/README.md @@ -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)` diff --git a/diff.js b/diff.js index 60652ae..324d122 100644 --- a/diff.js +++ b/diff.js @@ -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 })