mirror of
https://github.com/flynx/diff.js.git
synced 2025-10-29 19:10:11 +00:00
reworked patterns + some docs...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
b77bfb03ee
commit
9fd9844336
40
README.md
40
README.md
@ -6,6 +6,7 @@ XXX Intro
|
|||||||
- [Motivation](#motivation)
|
- [Motivation](#motivation)
|
||||||
- [Goals / Features](#goals--features)
|
- [Goals / Features](#goals--features)
|
||||||
- [General](#general)
|
- [General](#general)
|
||||||
|
- [Installation and loading](#installation-and-loading)
|
||||||
- [Diff](#diff)
|
- [Diff](#diff)
|
||||||
- [Diff class API](#diff-class-api)
|
- [Diff class API](#diff-class-api)
|
||||||
- [Diff object API](#diff-object-api)
|
- [Diff object API](#diff-object-api)
|
||||||
@ -41,6 +42,45 @@ XXX alternatives
|
|||||||
|
|
||||||
## General
|
## 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:
|
Install the package:
|
||||||
```shell
|
```shell
|
||||||
$ npm install --save object-diff
|
$ npm install --save object-diff
|
||||||
|
|||||||
40
diff.js
40
diff.js
@ -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 =
|
var ANY =
|
||||||
module.ANY =
|
module.ANY =
|
||||||
new (object.makeConstructor('ANY', Object.assign(new LogicType(), {
|
makeSingletonPattern('ANY',
|
||||||
__cmp__: function(obj, cmp){
|
function(obj, cmp){ return true })
|
||||||
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...
|
// 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...
|
// Will match a number greater than or equal to min and less than max...
|
||||||
var NUMBER =
|
// XXX rename...
|
||||||
module.NUMBER =
|
var _NUMBER =
|
||||||
|
module._NUMBER =
|
||||||
object.makeConstructor('NUMBER', Object.assign(new LogicType(), {
|
object.makeConstructor('NUMBER', Object.assign(new LogicType(), {
|
||||||
__cmp__: function(obj, cmp){
|
__cmp__: function(obj, cmp){
|
||||||
if(typeof(obj) == 'number'
|
if(typeof(obj) == 'number'
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user