mirror of
				https://github.com/flynx/diff.js.git
				synced 2025-10-29 19:10:11 +00:00 
			
		
		
		
	reworked ARRAY(..) + docs...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
		
							parent
							
								
									c23bbb75d1
								
							
						
					
					
						commit
						8dd3bf064d
					
				
							
								
								
									
										28
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										28
									
								
								README.md
									
									
									
									
									
								
							| @ -23,6 +23,7 @@ | |||||||
| 		- [Logic patterns](#logic-patterns) | 		- [Logic patterns](#logic-patterns) | ||||||
| 		- [String patterns](#string-patterns) | 		- [String patterns](#string-patterns) | ||||||
| 		- [Number patterns](#number-patterns) | 		- [Number patterns](#number-patterns) | ||||||
|  | 		- [Array patterns](#array-patterns) | ||||||
| 	- [Patterns (EXPERIMENTAL)](#patterns-experimental) | 	- [Patterns (EXPERIMENTAL)](#patterns-experimental) | ||||||
| 	- [JSON compatibility](#json-compatibility) | 	- [JSON compatibility](#json-compatibility) | ||||||
| 	- [Extending Diff](#extending-diff) | 	- [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): | Here's how different `Bill` and `Ted` really are (or how the *diff* looks like): | ||||||
| ```javascript | ```javascript | ||||||
|  | // log out the relevant part... | ||||||
|  | console.log(diff.diff) | ||||||
|  | ``` | ||||||
|  | And the output is: | ||||||
|  | ```javascript | ||||||
| [ | [ | ||||||
| 	{ | 	{ | ||||||
| 		"path": ["name"], | 		"path": ["name"], | ||||||
| @ -363,15 +369,29 @@ Match a string via a nested pattern. | |||||||
| Matches a number | 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... | XXX examples... | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| ## Patterns (EXPERIMENTAL) | ## Patterns (EXPERIMENTAL) | ||||||
| 
 | 
 | ||||||
| `ARRAY` |  | ||||||
| `ARRAY(length)` |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| `IN(A)`   | `IN(A)`   | ||||||
| 
 | 
 | ||||||
| `AT(A, K)`   | `AT(A, K)`   | ||||||
|  | |||||||
							
								
								
									
										36
									
								
								diff.js
									
									
									
									
									
								
							
							
						
						
									
										36
									
								
								diff.js
									
									
									
									
									
								
							| @ -298,6 +298,7 @@ var makeCIPattern = function(name, check, init){ | |||||||
| 	return object.makeConstructor(name, o, o)  | 	return object.makeConstructor(name, o, o)  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
| // Singleton ANY...
 | // Singleton ANY...
 | ||||||
| //
 | //
 | ||||||
| // 	ANY
 | // 	ANY
 | ||||||
| @ -306,7 +307,8 @@ var makeCIPattern = function(name, check, init){ | |||||||
| var ANY =  | var ANY =  | ||||||
| module.ANY =  | module.ANY =  | ||||||
| 	makeCIPattern('ANY',  | 	makeCIPattern('ANY',  | ||||||
| 		function(obj, cmp){ return true })() | 		function(){ return true })() | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
| // String pattern...
 | // String pattern...
 | ||||||
| //
 | //
 | ||||||
| @ -379,15 +381,41 @@ module.NUMBER = | |||||||
| //
 | //
 | ||||||
| // 	ARRAY
 | // 	ARRAY
 | ||||||
| // 	ARRAY(length)
 | // 	ARRAY(length)
 | ||||||
|  | // 	ARRAY(func)
 | ||||||
|  | // 	ARRAY(pattern)
 | ||||||
|  | // 	ARRAY(test, ...)
 | ||||||
| // 		-> pattern
 | // 		-> 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 =  | var ARRAY =  | ||||||
| module.ARRAY =  | module.ARRAY =  | ||||||
| 	makeCIPattern('ARRAY',  | 	makeCIPattern('ARRAY',  | ||||||
| 		function(obj, cmp){  | 		function(obj, cmp){  | ||||||
| 			// XXX do the .value test....
 | 			return obj === ARRAY  | ||||||
| 			return obj === ARRAY || obj instanceof 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 })  | 		function(...value){ this.value = value })  | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user