From 9fd9844336c77ec9f15ea3ce076a01b3666128e1 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Fri, 3 Aug 2018 16:38:44 +0300 Subject: [PATCH] reworked patterns + some docs... Signed-off-by: Alex A. Naanou --- README.md | 40 ++++++++++++++++++++++++++++++++++++++++ diff.js | 40 +++++++++++++++++++++++++++++++++------- 2 files changed, 73 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index dc9fc81..b17101a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/diff.js b/diff.js index b95c00a..3a1ebfb 100644 --- a/diff.js +++ b/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 = 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'