Skip to content

Commit

Permalink
feat: added nodeSeparator option
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Siemon committed Jul 25, 2022
1 parent a74f2b5 commit 065e6d5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,37 +84,44 @@ By default a single string is returned. Can be set to `false` to instead return
#### spacerNoNeighbour

Type: `string`<br>
Default: ` `
Default: `   `

Prefix for depth level when no further neighbour is present.

#### spacerNeighbour

Type: `string`<br>
Default: ` `
Default: `  `

Prefix for depth level when a further neighbour is present.

#### keyNoNeighbour

Type: `string`<br>
Default: `└─ `
Default: `└─ `

Prefix for key when no further neighbour is present.

#### keyNeighbour

Type: `string`<br>
Default: `├─ `
Default: `├─ `

Prefix for key when a further neighbour is present.

#### nodeSeparator

Type: `string`<br>
Default: `: `

Used to separate node key from node value.

#### renderFn

Type: `function`<br>
Default: ``(node) => (['boolean', 'string', 'number'].includes(typeof node) ? `: ${node}` : '')``
Default: `(node) => (['boolean', 'string', 'number'].includes(typeof node) ? node : undefined)`

Can be used to overwrite the node rendering logic.
Can be used to overwrite the node rendering logic. Node is rendered if result is not equal `undefined`.

#### sortFn

Expand Down
14 changes: 11 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ const buildCtx = (opts) => {
spacerNeighbour: '│ ',
keyNoNeighbour: '└─ ',
keyNeighbour: '├─ ',
renderFn: (node) => (['boolean', 'string', 'number'].includes(typeof node) ? `: ${node}` : ''),
nodeSeparator: ': ',
renderFn: (node) => (['boolean', 'string', 'number'].includes(typeof node) ? node : undefined),
sortFn: null,
breakCircularWith: ' (circular ref.)',
...opts
};
assert(Object.keys(ctx).length === 8, 'Unexpected Option(s) provided');
assert(Object.keys(ctx).length === 9, 'Unexpected Option(s) provided');
assert(typeof ctx.joined === 'boolean', 'Option "joined" has invalid format');
assert(typeof ctx.spacerNoNeighbour === 'string', 'Option "spacerNoNeighbour" has invalid format');
assert(typeof ctx.spacerNeighbour === 'string', 'Option "spacerNeighbour" has invalid format');
assert(typeof ctx.keyNoNeighbour === 'string', 'Option "keyNoNeighbour" has invalid format');
assert(typeof ctx.keyNeighbour === 'string', 'Option "keyNeighbour" has invalid format');
assert(typeof ctx.nodeSeparator === 'string', 'Option "nodeSeparator" has invalid format');
assert(typeof ctx.renderFn === 'function', 'Option "renderFn" has invalid format');
assert(typeof ctx.sortFn === 'function' || ctx.sortFn === null, 'Option "sortFn" has invalid format');
assert(
Expand All @@ -31,6 +33,11 @@ export default (tree, opts = {}) => {
const ctx = buildCtx(opts);
const result = [];

const rootRendered = ctx.renderFn(tree);
if (rootRendered !== undefined) {
result.push(String(rootRendered));
}

const sort = (input) => (ctx.sortFn === null ? input.reverse() : input.sort((a, b) => ctx.sortFn(b, a)));

const neighbours = [];
Expand All @@ -42,11 +49,12 @@ export default (tree, opts = {}) => {
const isCircular = ctx.breakCircularWith !== null && lookup.includes(node);

neighbours[key.length - 1] = keys.length !== 0 && keys[keys.length - 1].length === key.length;
const nodeRendered = ctx.renderFn(node);
result.push([
neighbours.slice(0, key.length - 1).map((n) => (n ? ctx.spacerNeighbour : ctx.spacerNoNeighbour)).join(''),
neighbours[key.length - 1] ? ctx.keyNeighbour : ctx.keyNoNeighbour,
key[key.length - 1],
ctx.renderFn(node),
nodeRendered === undefined ? '' : `${ctx.nodeSeparator}${node}`,
isCircular ? ctx.breakCircularWith : ''
].join(''));

Expand Down
3 changes: 2 additions & 1 deletion test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,9 @@ describe('Testing Treeify', () => {
9: null
}
}, {
renderFn: (node) => `: ${node}`
renderFn: (node) => node
})).to.deep.equal([
'[object Object]',
'├─ 1: [object Object]',
'│ ├─ 2: [object Object]',
'│ │ └─ 3: null',
Expand Down

0 comments on commit 065e6d5

Please sign in to comment.