Skip to content

Commit 2e89215

Browse files
author
lukas
committed
adopted original formatting, added readme entry for how to append custom fields.
1 parent af10d08 commit 2e89215

File tree

2 files changed

+49
-26
lines changed

2 files changed

+49
-26
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,33 @@ photos
164164
}
165165
```
166166

167+
## Adding custom fields
168+
You can easily extend a `DirectoryTree` object with custom fields by adding them to the custom field.
169+
For example add an `id` based on the path of a `DirectoryTree` object for each directory and file like so:
170+
```
171+
import { createHash } from 'crypto';
172+
import * as directoryTree from 'directory-tree';
173+
import { DirectoryTree, DirectoryTreeOptions, DirectoryTreeCallback } from 'directory-tree';
174+
175+
const callback: DirectoryTreeCallback = (
176+
item: DirectoryTree,
177+
path: string
178+
) => {
179+
item.custom.id = createHash('sha1').update(path).digest('base64');
180+
};
181+
182+
const dirTree: DirectoryTree & { id?: string } = directoryTree(
183+
"<your-directory-path>",
184+
{},
185+
callback,
186+
callback
187+
);
188+
189+
// to explore the object with the new custom fields
190+
console.log(JSON.stringify(dirTree, null, 2));
191+
192+
```
193+
167194
## Note
168195

169196
Device, FIFO and socket files are ignored.

index.d.ts

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,33 @@
11
import { Stats } from 'fs';
22

33
declare function directoryTree(
4-
path: string,
5-
options?: directoryTree.DirectoryTreeOptions,
6-
onEachFile?: directoryTree.DirectoryTreeCallback,
7-
onEachDirectory?: directoryTree.DirectoryTreeCallback
4+
path: string,
5+
options ? : directoryTree.DirectoryTreeOptions,
6+
onEachFile ? : directoryTree.DirectoryTreeCallback,
7+
onEachDirectory ? : directoryTree.DirectoryTreeCallback,
88
): directoryTree.DirectoryTree;
99

1010
export as namespace directoryTree;
1111

1212
declare namespace directoryTree {
13-
export interface DirectoryTree {
14-
path: string;
15-
name: string;
16-
size: number;
17-
type: 'directory' | 'file';
18-
children?: DirectoryTree[];
19-
extension?: string;
20-
isSymbolicLink?: boolean;
21-
custom: { [key: string]: any };
22-
}
23-
export interface DirectoryTreeOptions {
24-
normalizePath?: boolean;
25-
exclude?: RegExp | RegExp[];
26-
attributes?: (keyof Stats | 'type' | 'extension')[];
27-
extensions?: RegExp;
28-
followSymlink?: boolean;
29-
}
30-
export type DirectoryTreeCallback = (
31-
item: DirectoryTree,
32-
path: string,
33-
stats: Stats
34-
) => void;
13+
export interface DirectoryTree {
14+
path: string;
15+
name: string;
16+
size: number;
17+
type: "directory" | "file";
18+
children ? : DirectoryTree[];
19+
extension?: string;
20+
isSymbolicLink?: boolean;
21+
custom: { [key: string]: any };
22+
}
23+
export interface DirectoryTreeOptions {
24+
normalizePath ? : boolean;
25+
exclude ? : RegExp | RegExp[];
26+
attributes ? : (keyof Stats | "type" | "extension")[];
27+
extensions ? : RegExp;
28+
followSymlink ? : boolean;
29+
}
30+
export type DirectoryTreeCallback = (item: DirectoryTree, path: string, stats: Stats) => void;
3531
}
3632

3733
export = directoryTree;

0 commit comments

Comments
 (0)