diff --git a/object.js b/object.js index 7eac49a..554e055 100755 --- a/object.js +++ b/object.js @@ -292,6 +292,7 @@ function(obj){ throw new Error(`create(..): invalid name: "${name}"`) } } // calable... if(typeof(obj) == 'function'){ + /* c8 ignore next 9 */ var func = function(){ return '__call__' in func ? func.__call__(this, ...arguments) @@ -710,9 +711,12 @@ function(func){ : '__call__' in this ? this.__call__ : this.__proto__) + return module.normalizeIndent(f.toString(...args)) }, + /*/ return typeof(f) == 'function' ? module.normalizeIndent(f.toString(...args)) : undefined }, + //*/ enumerable: false, }) return func } diff --git a/package.json b/package.json index fca2b9f..7bf863c 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ig-object", - "version": "5.5.5", + "version": "5.5.6", "description": "", "main": "object.js", "scripts": { diff --git a/test.js b/test.js index 720b16b..11e200d 100755 --- a/test.js +++ b/test.js @@ -619,14 +619,18 @@ var tests = test.Tests({ var cases = test.Cases({ 'edge-cases': function(assert){ + // bad names... + assert.error('Constructor(..): malformed name', + function(){ + var X = object.Constructor('bad name', {}) }) + assert.error('create(..): malformed name', + function(){ + object.create('bad name', function(){}) }) assert.error('double __extends__ fail', function(){ - var X = object.C('X', { - __extends__: Object, - }, { - __extends__: Function, - }) - }) + var X = object.C('X', + { __extends__: Object }, + { __extends__: Function }) }) // native constructor... assert.array( @@ -674,6 +678,7 @@ var cases = test.Cases({ object.values(obj, 'x', function(){ return object.STOP(555) }, true)[0] == 555, // XXX assert ignores the order here -- this should fail... '.values(.., func, true) with explicit stop value') + }, deepKeys: function(assert){ var a = { @@ -813,6 +818,61 @@ var cases = test.Cases({ assert.array(b(), ['A', 'B'], 'call') assert.array(c(), ['A', 'B', 'C'], 'call') }, + + 'docs': function(assert){ + + var func = function(){ + console.log('some string...') } + var func_w_tostring = Object.assign( + function(){ + console.log('some string...') }, + { toString: function(){ + return 'func_w_tostring(){ .. }' } }) + + assert( + object.create(func).toString() == object.normalizeIndent(func.toString()), + 'create(..): function .toString() proxy (builtin).') + assert( + object.create(func_w_tostring).toString() == func_w_tostring.toString(), + 'create(..): function .toString() proxy (custom).') + + var callable_a = + object.Constructor('callable_a', + function(){ + console.log(this.constructor.name) })() + var callable_b = + object.Constructor('callable_b', { + __call__: function(){ + console.log(this.constructor.name) }, + })() + var callable_c = + object.Constructor('callable_c', { + __call__: function(){ + console.log(this.constructor.name) }, + toString: function(){ + return this.constructor.name + '.toString()' }, + })() + + assert( + callable_b.toString() == object.normalizeIndent(callable_b.__call__.toString()), + 'toString(..) proxy to .__call__(..)') + assert( + callable_b.toString() != object.normalizeIndent(callable_b.__proto__.toString()), + 'toString(..) proxy not to .__proto__.toString(..)') + assert( + callable_c.toString() == callable_c.__proto__.toString(), + 'toString(..) proxy to .toString(..)') + + assert( + object.create(callable_a).toString() == callable_a.toString(), + 'create(..): callable .toString() proxy (func).') + assert( + object.create(callable_b).toString() == callable_b.toString(), + 'create(..): callable .toString() proxy (__call__).') + assert( + object.create(callable_c).toString() == callable_c.toString(), + 'create(..): callable .toString() proxy (__call__ w. custom toString).') + }, })