( obj: InputObject, keysToKeep: string[], )
| 222 | } |
| 223 | |
| 224 | export function filterKeys<InputObject extends any>( |
| 225 | obj: InputObject, |
| 226 | keysToKeep: string[], |
| 227 | ): any { |
| 228 | if (!obj || typeof obj !== "object") return obj; |
| 229 | else if (Array.isArray(obj)) { |
| 230 | return obj.map((ele) => filterKeys(ele, keysToKeep)); |
| 231 | } else { |
| 232 | const output: Record<string, any> = {}; |
| 233 | Object.entries(obj).forEach(([key, value]) => { |
| 234 | // Not array or object |
| 235 | if (!value || typeof value !== "object") { |
| 236 | if (keysToKeep.includes(key)) { |
| 237 | output[key] = value; |
| 238 | } |
| 239 | return; |
| 240 | } |
| 241 | // Array |
| 242 | if (Array.isArray(value)) { |
| 243 | // If the array is empty, don't add it |
| 244 | const val = value.map((ele) => filterKeys(ele, keysToKeep)); |
| 245 | if (val.length > 0) { |
| 246 | output[key] = val; |
| 247 | } |
| 248 | } |
| 249 | // Object |
| 250 | const val = filterKeys(value, keysToKeep); |
| 251 | // If the object is empty, don't add it |
| 252 | if (Object.keys(val).length > 0) { |
| 253 | output[key] = val; |
| 254 | } |
| 255 | }); |
| 256 | |
| 257 | return output; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | export function splitPath(path: string): string[] { |
| 262 | return path.split("/").filter((ele) => ele !== ""); |
no outgoing calls
no test coverage detected