mirror of
https://github.com/flynx/object.js.git
synced 2025-10-29 10:30:08 +00:00
added parentOf(..) / childOf(..) / related(..) tests...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
0cdab633c7
commit
00c17b3ce8
17
README.md
17
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(<prototype>, '__call__', <this>)
|
||||
|
||||
See [`parent(..)`](#parent) and [`sources(..)`](#sources) for more details.
|
||||
|
||||
### `parentOf(..)` / `childOf(..)` / `related(..)`
|
||||
|
||||
Test if a is parent of b and/or vice-versa.
|
||||
```
|
||||
parentOf(<parent>, <child>)
|
||||
-> <bool>
|
||||
|
||||
childOf(<child>, <parent>)
|
||||
-> <bool>
|
||||
|
||||
related(<a>, <b>)
|
||||
-> <bool>
|
||||
```
|
||||
|
||||
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(..)`
|
||||
|
||||
|
||||
25
object.js
25
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...
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "ig-object",
|
||||
"version": "5.1.16",
|
||||
"version": "5.2.0",
|
||||
"description": "",
|
||||
"main": "object.js",
|
||||
"scripts": {
|
||||
|
||||
23
test.js
23
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(
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user