diff --git a/README.md b/README.md index 6c6f765..3f80543 100755 --- a/README.md +++ b/README.md @@ -634,7 +634,7 @@ C(, ..) ## Utilities -### `normalizeIndent(..)` +### `normalizeIndent(..)` / `normalizeTextIndent(..)` Align _code_ to shortest leading white-space ``` @@ -647,6 +647,13 @@ normalizeIndent(, , ) This is used to format `.toString(..)` return values for nested functions 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(..)` diff --git a/object.js b/object.js index 11ced79..66e6bbb 100755 --- a/object.js +++ b/object.js @@ -19,7 +19,7 @@ var KEEP_TABS = module.KEEP_TABS = 1 -// Normalize indent... +// Normalize code indent... // // normalizeIndent(text) // -> text @@ -47,12 +47,18 @@ module.KEEP_TABS = 1 var normalizeIndent = module.normalizeIndent = function(text, tab_size, keep_tabs){ - tab_size = tab_size || TAB_SIZE - keep_tabs = (keep_tabs || KEEP_TABS) * tab_size + tab_size = tab_size == null ? + TAB_SIZE + : tab_size + keep_tabs = (keep_tabs == null ? + KEEP_TABS + : keep_tabs) + * tab_size tab_size = ' '.repeat(tab_size) text = tab_size != '' ? text.replace(/\t/g, tab_size) : text + var lines = text.trim().split(/\n/) var l = lines .reduce(function(l, e, i){ @@ -69,6 +75,7 @@ function(text, tab_size, keep_tabs){ indent // min... : Math.min(l, indent) }, -1) + return lines .map(function(line, i){ return i == 0 ? @@ -78,6 +85,13 @@ function(text, tab_size, keep_tabs){ .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(a, b) diff --git a/package.json b/package.json index 6cf9c44..2ec69d8 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ig-object", - "version": "3.3.1", + "version": "3.3.2", "description": "", "main": "object.js", "scripts": {