Skip to content

Commit 99d97ad

Browse files
committed
Added support for deno
1 parent 44ccec1 commit 99d97ad

File tree

5 files changed

+149
-106
lines changed

5 files changed

+149
-106
lines changed

.npmignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.vscode/
2-
src/
2+
docs/
33
test/
44
types/
55
.editorconfig

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
# Format Tree
2-
This library allows complete formatting of trees with guides and configurability that look similar to how npm and yarn dispay trees. The functions expect the predefined structure as defined in [index.ts](src/index.ts), and the full documentation for that format is also described there. However, some examples are show here (which are taken from the [test file](test/test.ts))
2+
This library allows complete formatting of trees with guides and configurability that look similar to how npm and yarn dispay trees. The functions expect the predefined structure as defined in [mod.ts](lib/mod.ts), and the full documentation for that format is also described there. However, some examples are shown here (which are taken from the [test file](test/test.ts))
33

4-
## Node-based
4+
This library is compatible with [deno](https://github.com/denoland/deno) via the following direct import:
5+
6+
`https://raw.githubusercontent.com/luvies/format-tree/{tag}/lib/mod.ts`
7+
8+
or by using the slightly nicer:
9+
10+
`https://denopkg.com/luvies/format-tree@{tag}/lib/mod.ts`
11+
12+
In both cases, the `{tag}` identifier is the release version you are targeting. I would recommend against using master directly (as, in general, the master branch is not guaranteed to be stable).
13+
14+
## With first item
515
Source:
616

717
```ts
@@ -66,7 +76,7 @@ Output:
6676

6777
![node-stype output](docs/img/node-style.png)
6878

69-
## List-style
79+
## Without first item
7080
Source:
7181

7282
```ts

lib/mod.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
1-
import stringLength from 'string-length';
1+
// These snippets are taken from the source to bypass the NPM system, and allow Deno support.
22

3+
// Taken from https://github.com/chalk/strip-ansi/blob/master/index.js
4+
const ansiPatterns = [
5+
'[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\\u0007)',
6+
'(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))',
7+
].join('|');
8+
9+
const ansiRegex = new RegExp(ansiPatterns, 'g');
10+
11+
// Taken from https://github.com/kevva/astral-regex/blob/master/index.js
12+
const astralRegex = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g;
13+
14+
// Taken from https://github.com/sindresorhus/string-length/blob/master/index.js
15+
function stringLength(str: string): number {
16+
return str.replace(ansiRegex, '').replace(astralRegex, ' ').length;
17+
}
18+
19+
/**
20+
* The options to configure the formatting of the tree.
21+
*/
322
export interface Options {
423
/**
524
* The function used to format the guides. Mainly used for adding
@@ -98,13 +117,13 @@ export function formatTree(tree: TreeNode | TreeNode[], options: Options = {}):
98117
let shouldFirstCap = true;
99118
const inset = options.inset || 0;
100119

101-
// process nodes function
120+
// Process nodes function.
102121
const processNodes = (nodes: TreeNode[], prefix: string) => {
103122
for (let i = 0; i < nodes.length; i++) {
104-
// shorthands
123+
// Shorthands.
105124
const node = nodes[i];
106125

107-
// set up guide for current node
126+
// Set up guide for current node.
108127
let guide: string;
109128
const last = i === nodes.length - 1;
110129
const hasChildren = node.children && node.children.length;
@@ -134,18 +153,18 @@ export function formatTree(tree: TreeNode | TreeNode[], options: Options = {}):
134153
}
135154
guide += ' ';
136155

137-
// apply format function
156+
// Apply format function.
138157
if (options.guideFormat) {
139158
guide = options.guideFormat(guide);
140159
}
141160

142-
// build current line
161+
// Build current line.
143162
toBuild.push({
144163
line: prefix + guide + node.text,
145164
extra: node.extra,
146165
});
147166

148-
// build children
167+
// Build children.
149168
if (hasChildren) {
150169
let nprefix = `${prefix}${last ? ' ' : '│'} ${inset === 1 ? ' ' : ''}`;
151170
if (options.guideFormat) {
@@ -159,7 +178,7 @@ export function formatTree(tree: TreeNode | TreeNode[], options: Options = {}):
159178
}
160179
};
161180

162-
// start tree formatting
181+
// Start tree formatting.
163182
let tr: TreeNode[] | undefined;
164183
if (Array.isArray(tree)) {
165184
tr = tree;
@@ -175,13 +194,13 @@ export function formatTree(tree: TreeNode | TreeNode[], options: Options = {}):
175194
processNodes(tr, '');
176195
}
177196

178-
// get the longest name so we can format the extra text occordingly
197+
// Get the longest name so we can format the extra text occordingly.
179198
let maxLen = 0;
180199
for (const item of toBuild) {
181200
maxLen = Math.max(maxLen, stringLength(item.line));
182201
}
183202

184-
// add extra text and build full output
203+
// Add extra text and build full output.
185204
const output: string[] = [];
186205
const extraSplit = typeof options.extraSplit === 'undefined' ? ' | ' : options.extraSplit;
187206
for (const item of toBuild) {

test/deno_test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// @ts-ignore
2+
import { formatTreeString } from '../lib/mod.ts';
3+
4+
// tslint:disable-next-line:no-console
5+
console.log(formatTreeString({
6+
text: 'first',
7+
extra: 'extra',
8+
children: [
9+
{
10+
text: 'second',
11+
extra: 'another',
12+
},
13+
],
14+
}));

0 commit comments

Comments
 (0)