(params: FlattenParams, path: string)
| 13 | * @returns {FlattenParams} |
| 14 | */ |
| 15 | const removePath = (params: FlattenParams, path: string): FlattenParams => { |
| 16 | // by default simply filter out elements from the object |
| 17 | let filtered = filterOutParams(params, path) |
| 18 | |
| 19 | // reverse means that we iterate from the closes parent |
| 20 | const parentPaths = pathToParts(path).reverse() |
| 21 | |
| 22 | // but if one of the parent is an array |
| 23 | parentPaths.find((parentPath, parentIndex) => { |
| 24 | const parent = get(params, parentPath) |
| 25 | if (Array.isArray(parent)) { |
| 26 | // previous element is stringified index like 'property.1' |
| 27 | const previousPaths = parentPaths[parentIndex - 1].split(DELIMITER) |
| 28 | // so this is the index: 1 |
| 29 | const previousPathIndex = previousPaths[previousPaths.length - 1] |
| 30 | parent.splice(+previousPathIndex, 1) |
| 31 | filtered = set(params, parentPath, parent) |
| 32 | // this works just for the firstly found array item, because in case of removing the last one |
| 33 | // it leaves `[]` as a value. |
| 34 | return true |
| 35 | } |
| 36 | return false |
| 37 | }) |
| 38 | |
| 39 | return filtered |
| 40 | } |
| 41 | |
| 42 | export { removePath } |
no test coverage detected
searching dependent graphs…