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:53:24 +03:00
parent 0ae32795f1
commit 13fd19abf2
3 changed files with 26 additions and 5 deletions

View File

@ -634,7 +634,7 @@ C(<name>, ..)
## Utilities ## Utilities
### `normalizeIndent(..)` ### `normalizeIndent(..)` / `normalizeTextIndent(..)`
Align _code_ to shortest leading white-space Align _code_ to shortest leading white-space
``` ```
@ -647,6 +647,13 @@ normalizeIndent(<text>, <tab-size>, <keep-tabs>)
This is used to format `.toString(..)` return values for nested functions This is used to format `.toString(..)` return values for nested functions
to make source printing in console more pleasant to read. to make source printing in console more pleasant to read.
`tab_size` defaults to `object.TAB_SIZE`
`keep_tabs` defaults to `object.KEEP_TABS`
`normalizeTextIndent(..)` is a shorthand optimized for text rather than
code -- ignores `object.KEEP_TABS` and `keep_tabs` is 0 by default.
### `match(..)` ### `match(..)`

View File

@ -19,7 +19,7 @@ var KEEP_TABS =
module.KEEP_TABS = 1 module.KEEP_TABS = 1
// Normalize indent... // Normalize code indent...
// //
// normalizeIndent(text) // normalizeIndent(text)
// -> text // -> text
@ -47,12 +47,18 @@ module.KEEP_TABS = 1
var normalizeIndent = var normalizeIndent =
module.normalizeIndent = module.normalizeIndent =
function(text, tab_size, keep_tabs){ function(text, tab_size, keep_tabs){
tab_size = tab_size || TAB_SIZE tab_size = tab_size == null ?
keep_tabs = (keep_tabs || KEEP_TABS) * tab_size TAB_SIZE
: tab_size
keep_tabs = (keep_tabs == null ?
KEEP_TABS
: keep_tabs)
* tab_size
tab_size = ' '.repeat(tab_size) tab_size = ' '.repeat(tab_size)
text = tab_size != '' ? text = tab_size != '' ?
text.replace(/\t/g, tab_size) text.replace(/\t/g, tab_size)
: text : text
var lines = text.trim().split(/\n/) var lines = text.trim().split(/\n/)
var l = lines var l = lines
.reduce(function(l, e, i){ .reduce(function(l, e, i){
@ -69,6 +75,7 @@ function(text, tab_size, keep_tabs){
indent indent
// min... // min...
: Math.min(l, indent) }, -1) : Math.min(l, indent) }, -1)
return lines return lines
.map(function(line, i){ .map(function(line, i){
return i == 0 ? return i == 0 ?
@ -78,6 +85,13 @@ function(text, tab_size, keep_tabs){
.trim() } .trim() }
// shorthand more suted for text...
var normalizeTextIndent =
module.normalizeTextIndent =
function(text, tab_size, keep_tabs){
return module.normalizeIndent(text, tab_size, keep_tabs || 0) }
// Match two objects... // Match two objects...
// //
// match(a, b) // match(a, b)

View File

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