|
| 1 | +// @ts-check |
| 2 | + |
| 3 | +// @deno-types="is-plain-obj/index.d.ts" |
| 4 | +import isPlainObject from "is-plain-obj"; |
| 5 | + |
| 6 | +/** @typedef {import("./isExtractableFile.mjs").default} isExtractableFile */ |
| 7 | + |
| 8 | +/** |
| 9 | + * Recursively extracts files and their {@link ObjectPath object paths} within a |
| 10 | + * value, replacing them with `null` in a deep clone without mutating the |
| 11 | + * original value. |
| 12 | + * [`FileList`](https://developer.mozilla.org/en-US/docs/Web/API/Filelist) |
| 13 | + * instances are treated as |
| 14 | + * [`File`](https://developer.mozilla.org/en-US/docs/Web/API/File) instance |
| 15 | + * arrays. |
| 16 | + * @template Extractable Extractable file type. |
| 17 | + * @param {unknown} value Value to extract files from. Typically an object tree. |
| 18 | + * @param {(value: unknown) => value is Extractable} isExtractable Matches extractable files. Typically |
| 19 | + * {@linkcode isExtractableFile}. |
| 20 | + * @param {ObjectPath} [path] Prefix for object paths for extracted files. |
| 21 | + * Defaults to `""`. |
| 22 | + * @returns {Extraction<Extractable>} Extraction result. |
| 23 | + * @example |
| 24 | + * Extracting files from an object. |
| 25 | + * |
| 26 | + * For the following: |
| 27 | + * |
| 28 | + * ```js |
| 29 | + * import extractFiles from "extract-files/extractFiles.mjs"; |
| 30 | + * import isExtractableFile from "extract-files/isExtractableFile.mjs"; |
| 31 | + * |
| 32 | + * const file1 = new File(["1"], "1.txt", { type: "text/plain" }); |
| 33 | + * const file2 = new File(["2"], "2.txt", { type: "text/plain" }); |
| 34 | + * const value = { |
| 35 | + * a: file1, |
| 36 | + * b: [file1, file2], |
| 37 | + * }; |
| 38 | + * |
| 39 | + * const { clone, files } = extractFiles(value, isExtractableFile, "prefix"); |
| 40 | + * ``` |
| 41 | + * |
| 42 | + * `value` remains the same. |
| 43 | + * |
| 44 | + * `clone` is: |
| 45 | + * |
| 46 | + * ```json |
| 47 | + * { |
| 48 | + * "a": null, |
| 49 | + * "b": [null, null] |
| 50 | + * } |
| 51 | + * ``` |
| 52 | + * |
| 53 | + * `files` is a |
| 54 | + * [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) |
| 55 | + * instance containing: |
| 56 | + * |
| 57 | + * | Key | Value | |
| 58 | + * | :------ | :--------------------------- | |
| 59 | + * | `file1` | `["prefix.a", "prefix.b.0"]` | |
| 60 | + * | `file2` | `["prefix.b.1"]` | |
| 61 | + */ |
| 62 | +export default function extractFiles(value, isExtractable, path = "") { |
| 63 | + if (!arguments.length) throw new TypeError("Argument 1 `value` is required."); |
| 64 | + |
| 65 | + if (typeof isExtractable !== "function") |
| 66 | + throw new TypeError("Argument 2 `isExtractable` must be a function."); |
| 67 | + |
| 68 | + if (typeof path !== "string") |
| 69 | + throw new TypeError("Argument 3 `path` must be a string."); |
| 70 | + |
| 71 | + /** |
| 72 | + * Deeply clonable value. |
| 73 | + * @typedef {Array<unknown> | FileList | Record<PropertyKey, unknown>} Cloneable |
| 74 | + */ |
| 75 | + |
| 76 | + /** |
| 77 | + * Clone of a {@link Cloneable deeply cloneable value}. |
| 78 | + * @typedef {Exclude<Cloneable, FileList>} Clone |
| 79 | + */ |
| 80 | + |
| 81 | + /** |
| 82 | + * Map of values recursed within the input value and their clones, for reusing |
| 83 | + * clones of values that are referenced multiple times within the input value. |
| 84 | + * @type {Map<Cloneable, Clone>} |
| 85 | + */ |
| 86 | + const clones = new Map(); |
| 87 | + |
| 88 | + /** |
| 89 | + * Extracted files and their object paths within the input value. |
| 90 | + * @type {Extraction<Extractable>["files"]} |
| 91 | + */ |
| 92 | + const files = new Map(); |
| 93 | + |
| 94 | + /** |
| 95 | + * Recursively clones the value, extracting files. |
| 96 | + * @param {unknown} value Value to extract files from. |
| 97 | + * @param {ObjectPath} path Prefix for object paths for extracted files. |
| 98 | + * @param {Set<Cloneable>} recursed Recursed values for avoiding infinite |
| 99 | + * recursion of circular references within the input value. |
| 100 | + * @returns {unknown} Clone of the value with files replaced with `null`. |
| 101 | + */ |
| 102 | + function recurse(value, path, recursed) { |
| 103 | + if (isExtractable(value)) { |
| 104 | + const filePaths = files.get(value); |
| 105 | + |
| 106 | + filePaths ? filePaths.push(path) : files.set(value, [path]); |
| 107 | + |
| 108 | + return null; |
| 109 | + } |
| 110 | + |
| 111 | + const valueIsList = |
| 112 | + Array.isArray(value) || |
| 113 | + (typeof FileList !== "undefined" && value instanceof FileList); |
| 114 | + const valueIsPlainObject = isPlainObject(value); |
| 115 | + |
| 116 | + if (valueIsList || valueIsPlainObject) { |
| 117 | + let clone = clones.get(value); |
| 118 | + |
| 119 | + const uncloned = !clone; |
| 120 | + |
| 121 | + if (uncloned) { |
| 122 | + clone = valueIsList |
| 123 | + ? [] |
| 124 | + : // Replicate if the plain object is an `Object` instance. |
| 125 | + value instanceof /** @type {any} */ (Object) |
| 126 | + ? {} |
| 127 | + : Object.create(null); |
| 128 | + |
| 129 | + clones.set(value, /** @type {Clone} */ (clone)); |
| 130 | + } |
| 131 | + |
| 132 | + if (!recursed.has(value)) { |
| 133 | + const pathPrefix = path ? `${path}.` : ""; |
| 134 | + const recursedDeeper = new Set(recursed).add(value); |
| 135 | + |
| 136 | + if (valueIsList) { |
| 137 | + let index = 0; |
| 138 | + |
| 139 | + for (const item of value) { |
| 140 | + const itemClone = recurse( |
| 141 | + item, |
| 142 | + pathPrefix + index++, |
| 143 | + recursedDeeper |
| 144 | + ); |
| 145 | + |
| 146 | + if (uncloned) /** @type {Array<unknown>} */ (clone).push(itemClone); |
| 147 | + } |
| 148 | + } else |
| 149 | + for (const key in value) { |
| 150 | + const propertyClone = recurse( |
| 151 | + value[key], |
| 152 | + pathPrefix + key, |
| 153 | + recursedDeeper |
| 154 | + ); |
| 155 | + |
| 156 | + if (uncloned) |
| 157 | + /** @type {Record<PropertyKey, unknown>} */ (clone)[key] = |
| 158 | + propertyClone; |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + return clone; |
| 163 | + } |
| 164 | + |
| 165 | + return value; |
| 166 | + } |
| 167 | + |
| 168 | + return { |
| 169 | + clone: recurse(value, path, new Set()), |
| 170 | + files, |
| 171 | + }; |
| 172 | +} |
| 173 | + |
| 174 | +/** |
| 175 | + * An extraction result. |
| 176 | + * @template [Extractable=unknown] Extractable file type. |
| 177 | + * @typedef {object} Extraction |
| 178 | + * @prop {unknown} clone Clone of the original value with extracted files |
| 179 | + * recursively replaced with `null`. |
| 180 | + * @prop {Map<Extractable, Array<ObjectPath>>} files Extracted files and their |
| 181 | + * object paths within the original value. |
| 182 | + */ |
| 183 | + |
| 184 | +/** |
| 185 | + * String notation for the path to a node in an object tree. |
| 186 | + * @typedef {string} ObjectPath |
| 187 | + * @see [`object-path` on npm](https://npm.im/object-path). |
| 188 | + * @example |
| 189 | + * An object path for object property `a`, array index `0`, object property `b`: |
| 190 | + * |
| 191 | + * ``` |
| 192 | + * a.0.b |
| 193 | + * ``` |
| 194 | + */ |
0 commit comments