lodash-omitdeep
Version:
lodash omitDeep/omitByDeep object key/value recursively
1 lines • 2.56 kB
Source Map (JSON)
{"version":3,"file":"omitDeep.cjs","sources":["../../../src/omitDeep/omitDeep.ts"],"sourcesContent":["import type { Many, PartialObject, PropertyName } from 'lodash';\n\nimport lodash from 'lodash';\n\nexport const needOmit = (value: any) =>\n !lodash.isNil(value) && (lodash.isPlainObject(value) || Array.isArray(value));\n\ninterface OmitDeep {\n <T extends object, K extends PropertyName[]>(\n object: T | null | undefined,\n ...paths: K\n ): Pick<T, Exclude<keyof T, K[number]>>;\n <T extends object, K extends keyof T>(\n object: T | null | undefined,\n ...paths: Many<K>[]\n ): Omit<T, K>;\n <T extends object>(\n object: T | null | undefined,\n ...paths: Many<PropertyName>[]\n ): PartialObject<T>;\n}\n\n/**\n * The opposite of `_.pick`; this method creates an object composed of the\n * own and inherited enumerable properties of `object` that are not omitted.\n *\n * @category Function\n * @param object The source object.\n * @param [paths] The property names to omit, specified\n * individually or in arrays.\n * @returns Returns the new object.\n * @example\n *\n * const object = { 'a': 1, 'b': 2, 'c': { 'a': 1, 'b': 2 } };\n *\n * omitDeep(object, ['b', 'a']);\n * // => { 'c': {} }\n */\nexport const omitDeep: OmitDeep = (object: any, ...paths: any) => {\n function omitDeepOnOwnProps(object: any) {\n if (!Array.isArray(object) && !lodash.isPlainObject(object)) {\n return object;\n }\n\n if (Array.isArray(object)) {\n return object.map((element) => (needOmit(element) ? omitDeep(element, ...paths) : element));\n }\n\n const temp = {};\n\n for (const [key, value] of Object.entries<{\n [x: string]: object | PropertyName;\n }>(object)) {\n (temp as any)[key] = needOmit(value) ? omitDeep(value, ...paths) : value;\n }\n return lodash.omit(temp, ...paths);\n }\n\n return omitDeepOnOwnProps(object);\n};\n"],"names":["needOmit","value","lodash","omitDeep","object","paths","omitDeepOnOwnProps","element","temp","key"],"mappings":"0GAIaA,EAAYC,GACvB,CAACC,EAAO,MAAMD,CAAK,IAAMC,EAAO,cAAcD,CAAK,GAAK,MAAM,QAAQA,CAAK,GAiChEE,EAAqB,CAACC,KAAgBC,IAAe,CAChE,SAASC,EAAmBF,EAAa,CACvC,GAAI,CAAC,MAAM,QAAQA,CAAM,GAAK,CAACF,EAAO,cAAcE,CAAM,EACxD,OAAOA,EAGT,GAAI,MAAM,QAAQA,CAAM,EACtB,OAAOA,EAAO,IAAKG,GAAaP,EAASO,CAAO,EAAIJ,EAASI,EAAS,GAAGF,CAAK,EAAIE,CAAQ,EAG5F,MAAMC,EAAO,CAAA,EAEb,SAAW,CAACC,EAAKR,CAAK,IAAK,OAAO,QAE/BG,CAAM,EACNI,EAAaC,CAAG,EAAIT,EAASC,CAAK,EAAIE,EAASF,EAAO,GAAGI,CAAK,EAAIJ,EAErE,OAAOC,EAAO,KAAKM,EAAM,GAAGH,CAAK,CACnC,CAEA,OAAOC,EAAmBF,CAAM,CAClC"}