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)
|
- [`parent(..)`](#parent)
|
||||||
- [`parentProperty(..)`](#parentproperty)
|
- [`parentProperty(..)`](#parentproperty)
|
||||||
- [`parentCall(..)`](#parentcall)
|
- [`parentCall(..)`](#parentcall)
|
||||||
|
- [`parentOf(..)` / `childOf(..)` / `related(..)`](#parentof--childof--related)
|
||||||
- [`mixin(..)`](#mixin)
|
- [`mixin(..)`](#mixin)
|
||||||
- [`mixins(..)`](#mixins)
|
- [`mixins(..)`](#mixins)
|
||||||
- [`hasMixin(..)`](#hasmixin)
|
- [`hasMixin(..)`](#hasmixin)
|
||||||
@ -712,6 +713,22 @@ parentCall(<prototype>, '__call__', <this>)
|
|||||||
|
|
||||||
See [`parent(..)`](#parent) and [`sources(..)`](#sources) for more details.
|
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(..)`
|
### `mixin(..)`
|
||||||
|
|
||||||
|
|||||||
25
object.js
25
object.js
@ -510,6 +510,31 @@ function(proto, name, that, ...args){
|
|||||||
: undefined }
|
: 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...
|
// Mixin utils...
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "ig-object",
|
"name": "ig-object",
|
||||||
"version": "5.1.16",
|
"version": "5.2.0",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "object.js",
|
"main": "object.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
23
test.js
23
test.js
@ -150,6 +150,27 @@ module.setups = {
|
|||||||
}), `inherit (gen1)`),
|
}), `inherit (gen1)`),
|
||||||
B: B = assert(object.C('B', A, {
|
B: B = assert(object.C('B', A, {
|
||||||
// XXX constructor methods...
|
// 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(){
|
get prop(){
|
||||||
return 'B.prop' },
|
return 'B.prop' },
|
||||||
@ -737,7 +758,7 @@ object.Constructor('Assert', {
|
|||||||
path
|
path
|
||||||
: [path])
|
: [path])
|
||||||
],
|
],
|
||||||
stats,
|
this.stats,
|
||||||
this.verbose) },
|
this.verbose) },
|
||||||
pop: function(){
|
pop: function(){
|
||||||
return this.constructor(
|
return this.constructor(
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user