Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Apr 30, 2018
1 parent da70db4 commit 8b3540c
Show file tree
Hide file tree
Showing 5 changed files with 283 additions and 232 deletions.
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage/
unist-util-inspect.js
unist-util-inspect.min.js
165 changes: 75 additions & 90 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,222 +1,207 @@
'use strict';
'use strict'

var isEmpty = require('is-empty');
var isEmpty = require('is-empty')

/* Detect color support. */

var color = true;
var color = true

try {
color = 'inspect' in require('util');
color = 'inspect' in require('util')
} catch (err) {
/* istanbul ignore next - browser */
color = false;
color = false
}

module.exports = color ? inspect : /* istanbul ignore next */ noColor;
module.exports = color ? inspect : /* istanbul ignore next */ noColor

inspect.color = inspect;
noColor.color = inspect;
inspect.noColor = noColor;
noColor.noColor = noColor;
inspect.color = inspect
noColor.color = inspect
inspect.noColor = noColor
noColor.noColor = noColor

var dim = ansiColor(2, 22);
var yellow = ansiColor(33, 39);
var green = ansiColor(32, 39);
var dim = ansiColor(2, 22)
var yellow = ansiColor(33, 39)
var green = ansiColor(32, 39)

/* Define ANSII color removal functionality. */
var COLOR_EXPRESSION = new RegExp(
'(?:' +
'(?:\\u001b\\[)|' +
'\\u009b' +
')' +
'(?:' +
')' +
'(?:' +
'(?:[0-9]{1,3})?(?:(?:;[0-9]{0,3})*)?[A-M|f-m]' +
')|' +
'\\u001b[A-M]',
')|' +
'\\u001b[A-M]',
'g'
);

/* Constants. */
var CHAR_HORIZONTAL_LINE = '─';
var CHAR_VERTICAL_LINE = '│';
var CHAR_SPLIT = '└';
var CHAR_CONTINUE_AND_SPLIT = '├';
var CONTINUE = CHAR_CONTINUE_AND_SPLIT + CHAR_HORIZONTAL_LINE + ' ';
var STOP = CHAR_SPLIT + CHAR_HORIZONTAL_LINE + ' ';
)

/* Standard keys defined by unist:
* https://github.com/syntax-tree/unist.
* We don‘t include `data` though. */
var ignore = [
'type',
'value',
'children',
'position'
];
* We don’t ignore `data` though. */
var ignore = ['type', 'value', 'children', 'position']

/* Inspects a node, without using color. */
function noColor(node, pad) {
return stripColor(inspect(node, pad));
return stripColor(inspect(node, pad))
}

/* Inspects a node. */
function inspect(node, pad) {
var result;
var children;
var index;
var length;
var result
var children
var index
var length

if (node && Boolean(node.length) && typeof node !== 'string') {
length = node.length;
index = -1;
result = [];
length = node.length
index = -1
result = []

while (++index < length) {
result[index] = inspect(node[index]);
result[index] = inspect(node[index])
}

return result.join('\n');
return result.join('\n')
}

if (!node || !node.type) {
return String(node);
return String(node)
}

result = [formatNode(node)];
children = node.children;
length = children && children.length;
index = -1;
result = [formatNode(node)]
children = node.children
length = children && children.length
index = -1

if (!length) {
return result[0];
return result[0]
}

if (!pad || typeof pad === 'number') {
pad = '';
pad = ''
}

while (++index < length) {
node = children[index];
node = children[index]

if (index === length - 1) {
result.push(formatNesting(pad + STOP) + inspect(node, pad + ' '));
result.push(formatNesting(pad + '└─ ') + inspect(node, pad + ' '))
} else {
result.push(formatNesting(pad + CONTINUE) + inspect(node, pad + CHAR_VERTICAL_LINE + ' '));
result.push(formatNesting(pad + '├─ ') + inspect(node, pad + '│ '))
}
}

return result.join('\n');
return result.join('\n')
}

/* Colored nesting formatter. */
function formatNesting(value) {
return dim(value);
return dim(value)
}

/* Compile a single position. */
function compile(pos) {
var values = [];
var values = []

if (!pos) {
return null;
return null
}

values = [
[pos.line || 1, pos.column || 1].join(':')
];
values = [[pos.line || 1, pos.column || 1].join(':')]

if ('offset' in pos) {
values.push(String(pos.offset || 0));
values.push(String(pos.offset || 0))
}

return values;
return values
}

/* Compile a location. */
function stringify(start, end) {
var values = [];
var positions = [];
var offsets = [];
var values = []
var positions = []
var offsets = []

add(start);
add(end);
add(start)
add(end)

if (positions.length !== 0) {
values.push(positions.join('-'));
values.push(positions.join('-'))
}

if (offsets.length !== 0) {
values.push(offsets.join('-'));
values.push(offsets.join('-'))
}

return values.join(', ');
return values.join(', ')

/* Add a position. */
function add(position) {
var tuple = compile(position);
var tuple = compile(position)

if (tuple) {
positions.push(tuple[0]);
positions.push(tuple[0])

if (tuple[1]) {
offsets.push(tuple[1]);
offsets.push(tuple[1])
}
}
}
}

/* Colored node formatter. */
function formatNode(node) {
var log = node.type;
var location = node.position || {};
var position = stringify(location.start, location.end);
var key;
var values = [];
var value;
var log = node.type
var location = node.position || {}
var position = stringify(location.start, location.end)
var key
var values = []
var value

if (node.children) {
log += dim('[') + yellow(node.children.length) + dim(']');
log += dim('[') + yellow(node.children.length) + dim(']')
} else if (typeof node.value === 'string') {
log += dim(': ') + green(JSON.stringify(node.value));
log += dim(': ') + green(JSON.stringify(node.value))
}

if (position) {
log += ' (' + position + ')';
log += ' (' + position + ')'
}

for (key in node) {
value = node[key];
value = node[key]

if (
ignore.indexOf(key) !== -1 ||
value === null ||
value === undefined ||
(typeof value === 'object' && isEmpty(value))
) {
continue;
continue
}

values.push('[' + key + '=' + JSON.stringify(value) + ']');
values.push('[' + key + '=' + JSON.stringify(value) + ']')
}

if (values.length !== 0) {
log += ' ' + values.join('');
log += ' ' + values.join('')
}

return log;
return log
}

/* Remove ANSI colour from `value`. */
function stripColor(value) {
return value.replace(COLOR_EXPRESSION, '');
return value.replace(COLOR_EXPRESSION, '')
}

/* Factory to wrap values in ANSI colours. */
function ansiColor(open, close) {
return color;
return color

function color(value) {
return '\u001b[' + open + 'm' + value + '\u001b[' + close + 'm';
return '\u001B[' + open + 'm' + value + '\u001B[' + close + 'm'
}
}
18 changes: 13 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"chalk": "^2.3.0",
"esmangle": "^1.0.1",
"nyc": "^11.0.0",
"prettier": "^1.12.1",
"remark-cli": "^5.0.0",
"remark-preset-wooorm": "^4.0.0",
"retext": "^5.0.0",
Expand All @@ -34,17 +35,24 @@
"xo": "^0.20.0"
},
"scripts": {
"build-md": "remark . -qfo",
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix",
"build-bundle": "browserify index.js --bare -s unistUtilInspect > unist-util-inspect.js",
"build-mangle": "esmangle unist-util-inspect.js > unist-util-inspect.min.js",
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
"lint": "xo",
"build": "npm run build-bundle && npm run build-mangle",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test.js",
"test": "npm run build && npm run lint && npm run test-coverage"
"test": "npm run format && npm run build && npm run test-coverage"
},
"prettier": {
"tabWidth": 2,
"useTabs": false,
"singleQuote": true,
"bracketSpacing": false,
"semi": false,
"trailingComma": "none"
},
"xo": {
"space": true,
"prettier": true,
"esnext": false,
"rules": {
"guard-for-in": "off",
Expand Down
12 changes: 7 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ npm install unist-util-inspect
## Usage

```javascript
var unified = require('unified');
var inspect = require('unist-util-inspect');
var parse = require('rehype-parse');
var unified = require('unified')
var inspect = require('unist-util-inspect')
var parse = require('rehype-parse')

var tree = unified().use(parse).parse('<h2>Hello, world!</h2>');
var tree = unified()
.use(parse)
.parse('<h2>Hello, world!</h2>')

console.log(inspect(tree));
console.log(inspect(tree))
```

Yields:
Expand Down

0 comments on commit 8b3540c

Please sign in to comment.