@v-c/util 1.3.1 → 1.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/pickDefined.d.ts +15 -0
- package/dist/pickDefined.js +27 -0
- package/package.json +1 -1
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copy `obj` keeping only the keys whose value is not `undefined`, optionally
|
|
3
|
+
* dropping `omitKeys` as well.
|
|
4
|
+
*
|
|
5
|
+
* Vue materialises every declared prop on `props` (unset ones as `undefined`),
|
|
6
|
+
* so spreading `props` into a child forwards dozens of empty keys that the
|
|
7
|
+
* child has to normalise in `initProps` / `setFullProps` on every mount, and
|
|
8
|
+
* that every further spread / omit copies again. Boolean props in @v-c default
|
|
9
|
+
* to `undefined` rather than `false`, so leaving an unset key out behaves the
|
|
10
|
+
* same as forwarding it as `undefined`.
|
|
11
|
+
*
|
|
12
|
+
* Reads each key exactly once: `obj` is usually a reactive proxy and every read
|
|
13
|
+
* goes through the `get` trap and dependency tracking.
|
|
14
|
+
*/
|
|
15
|
+
export default function pickDefined<T extends object, K extends keyof T = never>(obj: T, omitKeys?: readonly K[]): Omit<T, K>;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { toRaw } from "vue";
|
|
2
|
+
//#region src/pickDefined.ts
|
|
3
|
+
/**
|
|
4
|
+
* Copy `obj` keeping only the keys whose value is not `undefined`, optionally
|
|
5
|
+
* dropping `omitKeys` as well.
|
|
6
|
+
*
|
|
7
|
+
* Vue materialises every declared prop on `props` (unset ones as `undefined`),
|
|
8
|
+
* so spreading `props` into a child forwards dozens of empty keys that the
|
|
9
|
+
* child has to normalise in `initProps` / `setFullProps` on every mount, and
|
|
10
|
+
* that every further spread / omit copies again. Boolean props in @v-c default
|
|
11
|
+
* to `undefined` rather than `false`, so leaving an unset key out behaves the
|
|
12
|
+
* same as forwarding it as `undefined`.
|
|
13
|
+
*
|
|
14
|
+
* Reads each key exactly once: `obj` is usually a reactive proxy and every read
|
|
15
|
+
* goes through the `get` trap and dependency tracking.
|
|
16
|
+
*/
|
|
17
|
+
function pickDefined(obj, omitKeys) {
|
|
18
|
+
const result = {};
|
|
19
|
+
const raw = toRaw(obj);
|
|
20
|
+
for (const key in raw) {
|
|
21
|
+
const value = obj[key];
|
|
22
|
+
if (value !== void 0 && (!omitKeys || !omitKeys.includes(key))) result[key] = value;
|
|
23
|
+
}
|
|
24
|
+
return result;
|
|
25
|
+
}
|
|
26
|
+
//#endregion
|
|
27
|
+
export { pickDefined as default };
|