From 3eabbdbbef0bca15c2c55695dfc52d7cc934cd6a Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Wed, 29 Apr 2020 21:15:46 +0300 Subject: [PATCH 1/8] cleanup... Signed-off-by: Alex A. Naanou --- object.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/object.js b/object.js index be5f5cc..01aa8c3 100755 --- a/object.js +++ b/object.js @@ -6,8 +6,8 @@ * ...if yes then it would also be logical to move Object.run(..) here * **********************************************************************/ -((typeof define)[0]=='u'?function(f){module.exports=f(require)}:define)( -function(require){ var module={} // makes module AMD/node compatible... +((typeof define)[0]=='u'?function(f){module.exports=f(require)}:define) +(function(require){ var module={} // make module AMD/node compatible... /*********************************************************************/ // Helpers... @@ -496,8 +496,7 @@ function Constructor(name, a, b){ enumerable: false, }) - return _constructor -} + return _constructor } From 9fed68370be95c8aa0acef7aae7f9f2059af8667 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Thu, 30 Apr 2020 02:01:00 +0300 Subject: [PATCH 2/8] removed redundant args to parent(..) + some cleanup... Signed-off-by: Alex A. Naanou --- README.md | 10 +++++----- object.js | 46 ++++++++++++++++++++++++---------------------- package.json | 2 +- 3 files changed, 30 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 8e309ea..4498963 100755 --- a/README.md +++ b/README.md @@ -201,14 +201,14 @@ sources(, , ) -> ``` -Get parent method +Get parent attribute value or method ``` -parent(, ) - -> +parent(, ) + -> -> undefined -parent(, , ) - -> +parent(, ) + -> -> undefined ``` diff --git a/object.js b/object.js index 01aa8c3..dd56018 100755 --- a/object.js +++ b/object.js @@ -75,13 +75,14 @@ function(obj, name, callback){ var res = [] do { if(obj.hasOwnProperty(name)){ - res.push(obj) + res.push(obj) // handle callback... stop = callback && callback(obj) // stop requested by callback... - if(stop === false || stop == 'stop'){ - return obj } } + if(stop === true || stop == 'stop'){ + return res } + } obj = obj.__proto__ } while(obj !== null) return res } @@ -90,7 +91,7 @@ function(obj, name, callback){ // Find the next parent attribute in the prototype chain. // // Get parent attribute value... -// parent(value, name, this) +// parent(proto, name) // -> value> // -> undefined // @@ -108,7 +109,7 @@ function(obj, name, callback){ // // method: function(){ // // get attribute... -// var a = object.parent(X.prototype.attr, 'attr', this) +// var a = object.parent(X.prototype, 'attr') // // // get method... // var ret = object.parent(X.prototype.method, this).call(this, ...arguments) @@ -130,25 +131,26 @@ function(obj, name, callback){ // loops, for example, this.method deep in a chain will resolve to // the first .method value visible from 'this', i.e. the top most // value and not the value visible from that particular level... -var parent = +// +// XXX need to add a way to work with props... +var parent = module.parent = -function(proto, name, that){ +function(proto, name){ // special case: method... - if(arguments.length == 2){ - var method = proto - proto = that = name - name = method.name - // skip until we get to the current method... - while(proto.__proto__ && proto[name] !== method){ - proto = proto.__proto__ - } - } - // skip till next name occurrence... - while(proto.__proto__ && !proto.hasOwnProperty(name)){ - proto = proto.__proto__ - } - // get next value... - return proto.__proto__[name] } + if(typeof(name) != typeof('str')){ + that = name + name = proto.name + // get first matching source... + proto = sources(that, name, + function(obj){ return obj[name] === proto }) + .pop() } + // get first source... + var res = sources(proto, name, + function(obj){ return 'stop' }) + .pop() + return res ? + res.__proto__[name] + : undefined } // Find the next parent method and call it... diff --git a/package.json b/package.json index ed13d5d..b69e643 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ig-object", - "version": "2.4.6", + "version": "2.5.0", "description": "", "main": "object.js", "scripts": { From aea235552021062e78e3756eaba6403442f72a2a Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Thu, 30 Apr 2020 02:10:01 +0300 Subject: [PATCH 3/8] more cleanup... Signed-off-by: Alex A. Naanou --- README.md | 2 +- object.js | 18 ++++++------------ package.json | 2 +- 3 files changed, 8 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 4498963..6dc952f 100755 --- a/README.md +++ b/README.md @@ -214,8 +214,8 @@ parent(, ) Get parent method and call it ``` -parentCall(, ) parentCall(, , ) +parentCall(, ) -> -> undefined ``` diff --git a/object.js b/object.js index dd56018..5f3273c 100755 --- a/object.js +++ b/object.js @@ -132,7 +132,7 @@ function(obj, name, callback){ // the first .method value visible from 'this', i.e. the top most // value and not the value visible from that particular level... // -// XXX need to add a way to work with props... +// XXX need to add a way to work with props... (???) var parent = module.parent = function(proto, name){ @@ -155,8 +155,8 @@ function(proto, name){ // Find the next parent method and call it... // -// parentCall(meth, this, ...) // parentCall(proto, name, this, ...) +// parentCall(meth, this, ...) // -> res // -> undefined // @@ -170,20 +170,14 @@ function(proto, name){ // or: // parent(method, this).call(this, ...) // NOTE: for more docs see parent(..) -// -// XXX should we rename this to parent.call(..) ??? -// ...this does not care about context so there is no reason to keep -// the default call, but this lowers discoverability and might be -// confusing... var parentCall = module.parentCall = function(proto, name, that, ...args){ - var [p, c] = typeof(name) == typeof('str') ? - [ [proto, name, that], [...arguments].slice(2)] - : [ [proto, name], [...arguments].slice(1)] - var meth = parent(...p) + var meth = parent(proto, name) return meth instanceof Function ? - meth.call(...c) + meth.call(...( typeof(name) == typeof('str') ? + [...arguments].slice(2) + : [...arguments].slice(1) )) : undefined } diff --git a/package.json b/package.json index b69e643..ccf4f28 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ig-object", - "version": "2.5.0", + "version": "2.5.1", "description": "", "main": "object.js", "scripts": { From 64d3de1fd2e9bdcc94e9405dbcfc25ef720b0e76 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Thu, 30 Apr 2020 02:20:29 +0300 Subject: [PATCH 4/8] updated docs... Signed-off-by: Alex A. Naanou --- README.md | 10 ++++++++++ object.js | 4 +++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6dc952f..fa66e8e 100755 --- a/README.md +++ b/README.md @@ -212,9 +212,19 @@ parent(, ) -> undefined ``` +_Edge case: The `parent(, ..)` has one flaw -- it can't distinguish +between two references to the same method in a chain and will always +return the first, so the first case is preferable. This issue can't be +resolved as this case has no information on the prototype containing the +reference, but these cases should not be very common._ + + Get parent method and call it ``` parentCall(, , ) + -> + -> undefined + parentCall(, ) -> -> undefined diff --git a/object.js b/object.js index 5f3273c..ce6a2a6 100755 --- a/object.js +++ b/object.js @@ -149,6 +149,7 @@ function(proto, name){ function(obj){ return 'stop' }) .pop() return res ? + // get next value... res.__proto__[name] : undefined } @@ -162,7 +163,8 @@ function(proto, name){ // // // This also gracefully handles the case when no higher level definition -// is found, i.e. the corresponding parent(..) call will return undefined. +// is found, i.e. the corresponding parent(..) call will return undefined +// or a non-callable. // // NOTE: this is just like parent(..) but will call the retrieved method, // essentially this is a shorthand to: From 3d36d986028af26bcb8375a5a3e20f8a88e1e1ec Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Thu, 30 Apr 2020 02:26:47 +0300 Subject: [PATCH 5/8] updated docs... Signed-off-by: Alex A. Naanou --- README.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index fa66e8e..2a84b53 100755 --- a/README.md +++ b/README.md @@ -212,11 +212,10 @@ parent(, ) -> undefined ``` -_Edge case: The `parent(, ..)` has one flaw -- it can't distinguish -between two references to the same method in a chain and will always -return the first, so the first case is preferable. This issue can't be -resolved as this case has no information on the prototype containing the -reference, but these cases should not be very common._ +_Edge case: The `parent(, ..)` has one flaw -- in the rare case +where a prototype chain contains two or more references to the same +method under the same name, `parent(..)` can't distinguish between these +references and will always return the first one._ Get parent method and call it From 80ac1dd717e0720ba08c1133f07ed762f45f526d Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Thu, 30 Apr 2020 02:28:08 +0300 Subject: [PATCH 6/8] updated docs... Signed-off-by: Alex A. Naanou --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 2a84b53..81d9338 100755 --- a/README.md +++ b/README.md @@ -212,10 +212,10 @@ parent(, ) -> undefined ``` -_Edge case: The `parent(, ..)` has one flaw -- in the rare case -where a prototype chain contains two or more references to the same -method under the same name, `parent(..)` can't distinguish between these -references and will always return the first one._ +_Edge case: The `parent(, ..)` has one potential pitfall -- in +the rare case where a prototype chain contains two or more references +to the same method under the same name, `parent(..)` can't distinguish +between these references and will always return the first one._ Get parent method and call it From 6d74f6a509133222e9f152a8555da54ca2a7d1a4 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Thu, 30 Apr 2020 02:29:15 +0300 Subject: [PATCH 7/8] fixed docs... Signed-off-by: Alex A. Naanou --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 81d9338..9df5bf9 100755 --- a/README.md +++ b/README.md @@ -215,7 +215,7 @@ parent(, ) _Edge case: The `parent(, ..)` has one potential pitfall -- in the rare case where a prototype chain contains two or more references to the same method under the same name, `parent(..)` can't distinguish -between these references and will always return the first one._ +between these references and will always return the second one._ Get parent method and call it From 92da56ed78fbe8196335994c1ed712fa706b1fe4 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Thu, 30 Apr 2020 16:41:19 +0300 Subject: [PATCH 8/8] added parentProperty(..)... Signed-off-by: Alex A. Naanou --- README.md | 14 ++++++++++++++ object.js | 30 ++++++++++++++++++++++++++++-- package.json | 2 +- 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9df5bf9..6dce949 100755 --- a/README.md +++ b/README.md @@ -201,6 +201,7 @@ sources(, , ) -> ``` + Get parent attribute value or method ``` parent(, ) @@ -218,6 +219,15 @@ to the same method under the same name, `parent(..)` can't distinguish between these references and will always return the second one._ +Get parent property descriptor + +``` +parentProperty(, ) + -> + -> undefined +``` + + Get parent method and call it ``` parentCall(, , ) @@ -236,6 +246,7 @@ mixin(, , ...) -> ``` + Mixin contents of objects into one ``` mixinFlat(, , ...) @@ -244,6 +255,7 @@ mixinFlat(, , ...) This is like `Object.assign(..)` but copies property objects rather than property values. + Make a raw (un-initialized) instance ``` makeRawInstance(, , ...) @@ -252,6 +264,7 @@ makeRawInstance(, , ...) _EXPERIMENTAL: a shorthand to this is defined as `Constructor.__rawinstance__(context, ..)`_ + Define an object constructor ``` Constructor(, ) @@ -259,6 +272,7 @@ Constructor(, , ) -> ``` + Shorthand to `Constructor(..)` ``` C(, ..) diff --git a/object.js b/object.js index ce6a2a6..92e2288 100755 --- a/object.js +++ b/object.js @@ -66,6 +66,8 @@ function(text, tab_size){ // -> list // // +// NOTE: this will not trigger any props... +// // XXX should the callback(..) be used to break (current) or filter/map??? // XXX revise name... var sources = @@ -131,8 +133,10 @@ function(obj, name, callback){ // loops, for example, this.method deep in a chain will resolve to // the first .method value visible from 'this', i.e. the top most // value and not the value visible from that particular level... -// -// XXX need to add a way to work with props... (???) +// NOTE: in the general case this will get the value of the returned +// property/attribute, the rest of the way passive to props. +// The method case will get the value of every method from 'this' +// and to the method after the match. var parent = module.parent = function(proto, name){ @@ -154,6 +158,28 @@ function(proto, name){ : undefined } +// Find the next parent property descriptor in the prototype chain... +// +// parentProperty(proto, name) +// -> prop-descriptor +// +// +// This is like parent(..) but will get a property descriptor... +// +var parentProperty = +module.parentProperty = +function(proto, name){ + // get second source... + var c = 0 + var res = sources(proto, name, + function(obj){ return c++ == 1 }) + .pop() + return res ? + // get next value... + Object.getOwnPropertyDescriptor(res, name) + : undefined } + + // Find the next parent method and call it... // // parentCall(proto, name, this, ...) diff --git a/package.json b/package.json index ccf4f28..20b96ed 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ig-object", - "version": "2.5.1", + "version": "2.6.0", "description": "", "main": "object.js", "scripts": {