From 00c17b3ce8b4c63adc696ecf97ce3ecf4434af23 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Sun, 9 Aug 2020 14:05:30 +0300 Subject: [PATCH] added parentOf(..) / childOf(..) / related(..) tests... Signed-off-by: Alex A. Naanou --- README.md | 17 +++++++++++++++++ object.js | 25 +++++++++++++++++++++++++ package.json | 2 +- test.js | 23 ++++++++++++++++++++++- 4 files changed, 65 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index cf3e50f..491e813 100755 --- a/README.md +++ b/README.md @@ -136,6 +136,7 @@ class B extends A { - [`parent(..)`](#parent) - [`parentProperty(..)`](#parentproperty) - [`parentCall(..)`](#parentcall) + - [`parentOf(..)` / `childOf(..)` / `related(..)`](#parentof--childof--related) - [`mixin(..)`](#mixin) - [`mixins(..)`](#mixins) - [`hasMixin(..)`](#hasmixin) @@ -712,6 +713,22 @@ parentCall(, '__call__', ) See [`parent(..)`](#parent) and [`sources(..)`](#sources) for more details. +### `parentOf(..)` / `childOf(..)` / `related(..)` + +Test if a is parent of b and/or vice-versa. +``` +parentOf(, ) + -> + +childOf(, ) + -> + +related(, ) + -> +``` + +These are similar to `instanceof` but will test if the two objects are in the +same prototype chain and in case of `parentOf(..)`/`childOf(..)` in what order. ### `mixin(..)` diff --git a/object.js b/object.js index c34c9e5..e0f3416 100755 --- a/object.js +++ b/object.js @@ -510,6 +510,31 @@ function(proto, name, that, ...args){ : undefined } +// Test if child is related to parent... +// +// parentOf(parent, child) +// -> bool +// +// +// NOTE: this is like a instanceof b but within the prototype chain +var parentOf = +module.parentOf = +function(parent, child){ + return new Set(sources(child)).has(parent) } + +// Reverse of parentOf(..) +var childOf = +module.childOf = +function(child, parent){ + return parentOf(parent, child) } + +var related = +module.related = +function(a, b){ + return parentOf(a, b) + || parentOf(b, a) } + + //--------------------------------------------------------------------- // Mixin utils... diff --git a/package.json b/package.json index aed8a87..cc81954 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ig-object", - "version": "5.1.16", + "version": "5.2.0", "description": "", "main": "object.js", "scripts": { diff --git a/test.js b/test.js index 09aff37..89a7af6 100755 --- a/test.js +++ b/test.js @@ -150,6 +150,27 @@ module.setups = { }), `inherit (gen1)`), B: B = assert(object.C('B', A, { // XXX constructor methods... + testRelations: function(){ + assert(object.parentOf(A, this), + 'parentOf(A, B): expected to be true') + assert(object.childOf(this, A), + 'childOf(B, A): expected to be true') + + assert(!object.parentOf(X, this), + 'parentOf(X, B): expected to be false') + + assert(object.parentOf(A, E), + 'parentOf(A, E): expected to be true') + assert(object.childOf(E, A), + 'childOf(E, A): expected to be true') + + assert(object.related(A, E) && object.related(E, A), + 'related(A, E) and related(E, A): expected to be true') + + assert(!object.related(X, E) + && !object.related(E, X), + 'related(X, E) and related(E, X): expected to be flase') + }, }, { get prop(){ return 'B.prop' }, @@ -737,7 +758,7 @@ object.Constructor('Assert', { path : [path]) ], - stats, + this.stats, this.verbose) }, pop: function(){ return this.constructor(