Download admin-bro/src/utils/flat/remove-path.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.44 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/admin-bro/src/utils/flat/remove-path.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/admin-bro/src/utils/flat/remove-path.ts
-
curl -L -o remove-path.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/admin-bro/src/utils/flat/remove-path.ts
1.44 kB
| import { filterOutParams } from './filter-out-params.js' | |
| import { FlattenParams } from './flat.types.js' | |
| import { get } from './get.js' | |
| import { set } from './set.js' | |
| import { pathToParts } from './path-to-parts.js' | |
| import { DELIMITER } from './constants.js' | |
| /** | |
| * @load ./remove-path.doc.md | |
| * @memberof module:flat | |
| * @param {FlattenParams} params | |
| * @param {...string} properties | |
| * @returns {FlattenParams} | |
| */ | |
| const removePath = (params: FlattenParams, path: string): FlattenParams => { | |
| // by default simply filter out elements from the object | |
| let filtered = filterOutParams(params, path) | |
| // reverse means that we iterate from the closes parent | |
| const parentPaths = pathToParts(path).reverse() | |
| // but if one of the parent is an array | |
| parentPaths.find((parentPath, parentIndex) => { | |
| const parent = get(params, parentPath) | |
| if (Array.isArray(parent)) { | |
| // previous element is stringified index like 'property.1' | |
| const previousPaths = parentPaths[parentIndex - 1].split(DELIMITER) | |
| // so this is the index: 1 | |
| const previousPathIndex = previousPaths[previousPaths.length - 1] | |
| parent.splice(+previousPathIndex, 1) | |
| filtered = set(params, parentPath, parent) | |
| // this works just for the firstly found array item, because in case of removing the last one | |
| // it leaves `[]` as a value. | |
| return true | |
| } | |
| return false | |
| }) | |
| return filtered | |
| } | |
| export { removePath } | |