directory-tree 3.1.0 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +27 -0
  2. package/index.d.ts +1 -0
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -166,6 +166,33 @@ photos
166
166
  }
167
167
  ```
168
168
 
169
+ ## Adding custom fields
170
+ You can easily extend a `DirectoryTree` object with custom fields by adding them to the custom field.
171
+ For example add an `id` based on the path of a `DirectoryTree` object for each directory and file like so:
172
+ ```
173
+ import { createHash } from 'crypto';
174
+ import * as directoryTree from 'directory-tree';
175
+ import { DirectoryTree, DirectoryTreeOptions, DirectoryTreeCallback } from 'directory-tree';
176
+
177
+ const callback: DirectoryTreeCallback = (
178
+ item: DirectoryTree,
179
+ path: string
180
+ ) => {
181
+ item.custom.id = createHash('sha1').update(path).digest('base64');
182
+ };
183
+
184
+ const dirTree: DirectoryTree & { id?: string } = directoryTree(
185
+ "<your-directory-path>",
186
+ {},
187
+ callback,
188
+ callback
189
+ );
190
+
191
+ // to explore the object with the new custom fields
192
+ console.log(JSON.stringify(dirTree, null, 2));
193
+
194
+ ```
195
+
169
196
  ## Note
170
197
 
171
198
  Device, FIFO and socket files are ignored.
package/index.d.ts CHANGED
@@ -18,6 +18,7 @@ declare namespace directoryTree {
18
18
  children ? : DirectoryTree[];
19
19
  extension?: string;
20
20
  isSymbolicLink?: boolean;
21
+ custom: { [key: string]: any };
21
22
  }
22
23
  export interface DirectoryTreeOptions {
23
24
  normalizePath ? : boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "directory-tree",
3
- "version": "3.1.0",
3
+ "version": "3.2.0",
4
4
  "description": "Convert a directory tree to a JS object.",
5
5
  "repository": {
6
6
  "type": "git",