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.
- package/README.md +27 -0
- package/index.d.ts +1 -0
- 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