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:**
- the two approaches (_function_ vs. `.__call__(..)`) will produce
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
- `.__call__(..)` -> `.prototype` object with a `.__call__(..)` method
- _prototype function_ -> `.prototype` is that function object
- `.__call__(..)` -> `.prototype` is the object with the `.__call__(..)`
method
The instance in both cases is a function wrapper that will proxy the
call to the corresponding implementation.
@ -634,10 +636,11 @@ C(<name>, ..)
### `normalizeIndent(..)`
Align text to shortest leading whitespace
Align _code_ to shortest leading white-space
```
normalizeIndent(<text>)
normalizeIndent(<text>, <tab-size>)
normalizeIndent(<text>, <tab-size>, <keep-tabs>)
-> <text>
```

View File

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

View File

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