reworked patterns + some docs...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2018-08-03 16:38:44 +03:00
parent b77bfb03ee
commit 9fd9844336
2 changed files with 73 additions and 7 deletions

View File

@ -6,6 +6,7 @@ XXX Intro
- [Motivation](#motivation)
- [Goals / Features](#goals--features)
- [General](#general)
- [Installation and loading](#installation-and-loading)
- [Diff](#diff)
- [Diff class API](#diff-class-api)
- [Diff object API](#diff-object-api)
@ -41,6 +42,45 @@ XXX alternatives
## General
```javascript
var {Diff, cmp} = require('object-diff')
var Bill = {
name: 'Bill',
age: 20,
hair: 'black',
skills: [
],
}
var Ted = {
name: 'Ted',
age: 20,
hair: 'blond',
skills: [
],
}
var diff = Diff(Bill, Ted)
// XXX examine the diff...
var PERSON = {
name: STRING,
age: NUMBER,
hair: STRING,
skills: ARRAY(STRING),
}
// we can "type-check" things...
cmp(Bill, PERSON)
```
## Installation and loading
Install the package:
```shell
$ npm install --save object-diff

40
diff.js
View File

@ -286,13 +286,38 @@ object.makeConstructor('LogicType',
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Singleton, will compare to anything as true...
// Singletons...
var makeSingletonPattern = function(name, check){
return new (object.makeConstructor(
name,
Object.assign(new LogicType(), { __cmp__: check })))() }
var ANY =
module.ANY =
new (object.makeConstructor('ANY', Object.assign(new LogicType(), {
__cmp__: function(obj, cmp){
return true },
})))()
makeSingletonPattern('ANY',
function(obj, cmp){ return true })
// XXX should support regexp as arg...
var STRING =
module.STRING =
makeSingletonPattern('STRING',
function(obj, cmp){
return obj === STRING || typeof(obj) == typeof('str') })
// XXX support range checking, ...
var NUMBER =
module.NUMBER =
makeSingletonPattern('NUMBER',
function(obj, cmp){
return obj === NUMBER || typeof(obj) == typeof(123) })
// XXX support length, types, ...
var ARRAY =
module.ARRAY =
makeSingletonPattern('ARRAY',
function(obj, cmp){
return obj === ARRAY || obj instanceof Array })
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Will compare as true to anything but .value...
@ -344,8 +369,9 @@ object.makeConstructor('AND', Object.assign(new LogicType(), {
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Will match a number greater than or equal to min and less than max...
var NUMBER =
module.NUMBER =
// XXX rename...
var _NUMBER =
module._NUMBER =
object.makeConstructor('NUMBER', Object.assign(new LogicType(), {
__cmp__: function(obj, cmp){
if(typeof(obj) == 'number'