1 line
7.6 KiB
Text
1 line
7.6 KiB
Text
|
|
{"version":3,"file":"omitBy.cjs","names":["purry","out: Partial<Record<string, unknown>>"],"sources":["../src/omitBy.ts"],"sourcesContent":["import type { IsNever, Or, Simplify } from \"type-fest\";\nimport type { EnumerableStringKeyOf } from \"./internal/types/EnumerableStringKeyOf\";\nimport type { EnumerableStringKeyedValueOf } from \"./internal/types/EnumerableStringKeyedValueOf\";\nimport type { IsBoundedRecord } from \"./internal/types/IsBoundedRecord\";\nimport { purry } from \"./purry\";\n\n// Symbols are not passed to the predicate (because they can't be enumerated\n// with the `Object.entries` function) and the output object is built from a\n// shallow copy of the input; meaning symbols would just be passed through as-\n// is.\ntype PickSymbolKeys<T extends object> = {\n -readonly [P in keyof T as P extends symbol ? P : never]: T[P];\n};\n\n// When we don't use a type-predicate we can't say anything about what props\n// would be omitted from the output, so we need to assume any of them could be\n// filtered out. This means that we effectively make all (enumerable) keys\n// optional.\ntype PartialEnumerableKeys<T extends object> =\n // `extends unknown` is always going to be the case and is used to convert any\n // union into a [distributive conditional type](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types).\n T extends unknown\n ? Simplify<\n IsBoundedRecord<T> extends true\n ? PickSymbolKeys<T> & {\n -readonly [P in keyof T as P extends symbol\n ? never\n : P]?: Required<T>[P];\n }\n : // This is the type you'd get from doing:\n // `Object.fromEntries(Object.entries(x))`.\n Record<EnumerableStringKeyOf<T>, EnumerableStringKeyedValueOf<T>>\n >\n : never;\n\n// When the predicate is a type-guard we have more information to work with when\n// constructing the type of the output object. We can safely remove any property\n// which value would always be true for the predicate, AND we can also\n// assume that properties that are rejected by the predicate perfectly would\n// **always** show up in the output object. Hence to build the output object we\n// need to build and merge 2 output objects: One for the properties which have a\n// value of at type that would always yield a `false` result from the predicate,\n// these are the \"matches\", which would not change the \"optionality\" of the\n// input object's props, and one for partial matches which would also make the\n// props optional (as they could have a value that would be filtered out).\ntype PartialEnumerableKeysNarrowed<T extends object, S> = Simplify<\n ExactProps<T, S> & PartialProps<T, S> & PickSymbolKeys<T>\n>;\n\n// The exact case, props here would always be part of the output object\ntype ExactProps<T, S> = {\n -readonly [P in keyof T as IsExactProp<T, P, S> extends true\n ? P\n : never]: Exclude<T[P], S>;\n};\n\n// The partial case, props here might be part of the output object, but might\n// not be, hence they are optional.\ntype PartialProps<T, S> = {\n -readonly [P in keyof T as IsPartialProp<T, P, S> extends true\n ? P\n : never]?: Exclude<T[P], S>;\n};\n\n// If the input object's value type extends itself when the type-guard is\n// excluded from it we can safely assume that the predicate would always return\n// `false` for any value of that property.\ntype IsExactProp<T, P extends keyof T, S> = P extends symbol\n ? // Symbols are passed through via the PickSymbolKeys type\n false\n : T[P] extends Exclude<T[P], S>\n ? S extends T[P]\n ? // If S extends the T[P] it means the type the predicate is narrowing to\n // can't narrow the rejected value any further, so we can't say what\n // would happen for a concrete value in runtime (e.g. if T[P] is\n // `number` and S is `1`: `Exclude<number, 1> === number`.\n false\n : true\n : false;\n\n// ...and if the input object's value type isn't an exact match, but still ha
|