source: node_modules/recharts/es6/util/resolveDefaultProps.js

Last change on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 5.1 KB
RevLine 
[a762898]1function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
2function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
3function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
4function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
5function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
6/**
7 * This function mimics the behavior of the `defaultProps` static property in React.
8 * Functional components do not have a defaultProps property, so this function is useful to resolve default props.
9 *
10 * The common recommendation is to use ES6 destructuring with default values in the function signature,
11 * but you need to be careful there and make sure you destructure all the individual properties
12 * and not the whole object. See the test file for example.
13 *
14 * And because destructuring all properties one by one is a faff, and it's easy to miss one property,
15 * this function exists.
16 *
17 * @param realProps - the props object passed to the component by the user
18 * @param defaultProps - the default props object defined in the component by Recharts
19 * @returns - the props object with all the default props resolved. All `undefined` values are replaced with the default value.
20 */
21export function resolveDefaultProps(realProps, defaultProps) {
22 /*
23 * To avoid mutating the original `realProps` object passed to the function, create a shallow copy of it.
24 * `resolvedProps` will be modified directly with the defaults.
25 */
26 var resolvedProps = _objectSpread({}, realProps);
27 /*
28 * Since the function guarantees `D extends Partial<T>`, this assignment is safe.
29 * It allows TypeScript to work with the well-defined `Partial<T>` type inside the loop,
30 * making subsequent type inference (especially for `dp[key]`) much more straightforward for the compiler.
31 * This is a key step to improve type safety *without* value assertions later.
32 */
33 var dp = defaultProps;
34 /*
35 * `Object.keys` doesn't preserve strong key types - it always returns Array<string>.
36 * However, due to the `D extends Partial<T>` constraint,
37 * we know these keys *must* also be valid keys of `T`.
38 * This assertion informs TypeScript of this relationship, avoiding type errors when using `key` to index `acc` (type T).
39 *
40 * Type assertions are not sound but in this case it's necessary
41 * as `Object.keys` does not do what we want it to do.
42 */
43 var keys = Object.keys(defaultProps);
44 var withDefaults = keys.reduce((acc, key) => {
45 if (acc[key] === undefined && dp[key] !== undefined) {
46 acc[key] = dp[key];
47 }
48 return acc;
49 }, resolvedProps);
50 /*
51 * And again type assertions are not safe but here we have done the runtime work
52 * so let's bypass the lack of static type safety and tell the compiler what happened.
53 */
54 return withDefaults;
55}
56
57/**
58 * Helper type to extract the keys of T that are required.
59 * It iterates through each key K in T. If Pick<T, K> cannot be assigned an empty object {},
60 * it means K is required, so we keep K; otherwise, we discard it (never).
61 * [keyof T] at the end creates a union of the kept keys.
62 */
63
64/**
65 * Helper type to extract the keys of T that are optional.
66 * It iterates through each key K in T. If Pick<T, K> can be assigned an empty object {},
67 * it means K is optional (or potentially missing), so we keep K; otherwise, we discard it (never).
68 * [keyof T] at the end creates a union of the kept keys.
69 */
70
71/**
72 * Helper type to ensure keys of D exist in T.
73 * For each key K in D, if K is also a key of T, keep the type D[K].
74 * If K is NOT a key of T, map it to type `never`.
75 * An object cannot have a property of type `never`, effectively disallowing extra keys.
76 */
77
78/**
79 * This type will take a source type `Props` and a default type `Defaults` and will return a new type
80 * where all properties that are optional in `Props` but required in `Defaults` are made required in the result.
81 * Properties that are required in `Props` and optional in `Defaults` will remain required.
82 * Properties that are optional in both `Props` and `Defaults` will remain optional.
83 *
84 * This is useful for creating a type that represents the resolved props of a component with default props.
85 */
Note: See TracBrowser for help on using the repository browser.