tweaking and refactoring...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-05-08 15:42:32 +03:00
parent 4e5ef568f4
commit 0ae32795f1
3 changed files with 57 additions and 29 deletions

View File

@ -264,10 +264,12 @@ user's responsibility to call `.__call__(..)` method.
**Notes:** **Notes:**
- the two approaches (_function_ vs. `.__call__(..)`) will produce - the two approaches (_function_ vs. `.__call__(..)`) will produce
functionally identical but structurally different constructors/objects, functionally identical but structurally different constructors/objects,
the difference is in `.prototype`: the difference is in `.prototype` -- what is defined as the prototype
_is_ the prototype, so we get:
- _prototype function_ -> `.prototype` is a function object - _prototype function_ -> `.prototype` is that function object
- `.__call__(..)` -> `.prototype` object with a `.__call__(..)` method - `.__call__(..)` -> `.prototype` is the object with the `.__call__(..)`
method
The instance in both cases is a function wrapper that will proxy the The instance in both cases is a function wrapper that will proxy the
call to the corresponding implementation. call to the corresponding implementation.
@ -634,10 +636,11 @@ C(<name>, ..)
### `normalizeIndent(..)` ### `normalizeIndent(..)`
Align text to shortest leading whitespace Align _code_ to shortest leading white-space
``` ```
normalizeIndent(<text>) normalizeIndent(<text>)
normalizeIndent(<text>, <tab-size>) normalizeIndent(<text>, <tab-size>)
normalizeIndent(<text>, <tab-size>, <keep-tabs>)
-> <text> -> <text>
``` ```

View File

@ -15,6 +15,9 @@
var TAB_SIZE = var TAB_SIZE =
module.TAB_SIZE = 4 module.TAB_SIZE = 4
var KEEP_TABS =
module.KEEP_TABS = 1
// Normalize indent... // Normalize indent...
// //
@ -26,28 +29,46 @@ module.TAB_SIZE = 4
// for printing function code of functions that were defined at deep // for printing function code of functions that were defined at deep
// levels of indent. // levels of indent.
// //
// This will ignore the indent of the first line.
//
// If the last line is indented higher or equal to the rest of the text
// we will user keep_tabs (defaults to KEEP_TABS) to indent the rest of
// the text.
// This will indent the following styles correctnly:
//
// |function(a, b){ |function(a, b){
// | return a + b } | return a + b
// | |}
//
// NOTE: this will trim out both leading and trailing white-space. // NOTE: this will trim out both leading and trailing white-space.
// //
// XXX is this the right place for this??? // XXX is this the right place for this???
// ...when moving take care that ImageGrid's core.doc uses this... // ...when moving take care that ImageGrid's core.doc uses this...
var normalizeIndent = var normalizeIndent =
module.normalizeIndent = module.normalizeIndent =
function(text, tab_size){ function(text, tab_size, keep_tabs){
tab_size = tab_size || TAB_SIZE tab_size = tab_size || TAB_SIZE
text = tab_size > 0 ? keep_tabs = (keep_tabs || KEEP_TABS) * tab_size
text.replace(/\t/g, ' '.repeat(tab_size)) tab_size = ' '.repeat(tab_size)
text = tab_size != '' ?
text.replace(/\t/g, tab_size)
: text : text
var lines = text.split(/\n/) var lines = text.trim().split(/\n/)
var l = lines var l = lines
.reduce(function(l, e, i){ .reduce(function(l, e, i){
var indent = e.length - e.trimLeft().length var indent = e.length - e.trimLeft().length
return e.trim().length == 0 return e.trim().length == 0
// ignore 0 indent of first line... // ignore 0 indent of first/last lines...
|| (i == 0 && indent == 0) ? l || (i == 0 && indent == 0) ?
l
// last line -- ...
: i == lines.length-1 && indent > l ?
Math.max(l-keep_tabs, 0)
// initial state...
: l < 0 ? : l < 0 ?
indent indent
: Math.min(l, indent) // min...
}, -1) : Math.min(l, indent) }, -1)
return lines return lines
.map(function(line, i){ .map(function(line, i){
return i == 0 ? return i == 0 ?
@ -449,7 +470,12 @@ function(context, constructor, ...args){
var _mirror_doc = function(func, target){ var _mirror_doc = function(func, target){
Object.defineProperty(func, 'toString', { Object.defineProperty(func, 'toString', {
value: function(...args){ value: function(...args){
return target.toString(...args) }, var f = target.prototype instanceof Function ?
target.prototype
: target.prototype.__call__
return f instanceof Function ?
module.normalizeIndent(f.toString(...args))
: undefined },
enumerable: false, enumerable: false,
}) })
return func } return func }
@ -461,22 +487,21 @@ function(context, constructor, ...args){
// native constructor... // native constructor...
: /\[native code\]/.test(constructor.toString()) ? : /\[native code\]/.test(constructor.toString()) ?
Reflect.construct(constructor, args) Reflect.construct(constructor, args)
// callable instance -- prototype is a function... // callable instance...
// NOTE: we need to isolate the .prototype from instances... // NOTE: we need to isolate the callable from instances...
: constructor.prototype instanceof Function ? : (constructor.prototype instanceof Function
|| constructor.prototype.__call__ instanceof Function) ?
_mirror_doc( _mirror_doc(
function(){ function(){
return constructor.prototype return (
.call(obj, this, ...arguments) }, // .prototype is a function...
constructor.prototype) constructor.prototype instanceof Function ?
// callable instance -- prototype defines .__call__(..)... constructor.prototype
// NOTE: we need to isolate the .__call__ from instances... .call(obj, this, ...arguments)
: constructor.prototype.__call__ instanceof Function ? // .__call__(..)
_mirror_doc( : constructor.prototype.__call__
function(){ .call(obj, this, ...arguments)) },
return constructor.prototype.__call__ constructor)
.call(obj, this, ...arguments) },
constructor.prototype.__call__)
// use parent's constructor... // use parent's constructor...
// XXX do a better test??? // XXX do a better test???
: (constructor.__proto__ instanceof Function : (constructor.__proto__ instanceof Function
@ -723,7 +748,7 @@ function Constructor(name, a, b, c){
.toString() .toString()
.replace(/[^{]*{/, '{') .replace(/[^{]*{/, '{')
: '{ .. }' : '{ .. }'
return `${this.name}(${args})${normalizeIndent(code)}` }, return `${this.name}(${args})${module.normalizeIndent(code)}` },
enumerable: false, enumerable: false,
}) })
// set generic raw instance constructor... // set generic raw instance constructor...

View File

@ -1,6 +1,6 @@
{ {
"name": "ig-object", "name": "ig-object",
"version": "3.3.0", "version": "3.3.1",
"description": "", "description": "",
"main": "object.js", "main": "object.js",
"scripts": { "scripts": {