UNPKG

1.67 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.removeUndefinedValuesFromObject = exports.getPropertyByPath = exports.emplace = void 0;
4/**
5 * @internal
6 */
7function emplace(map, key, fn) {
8 const cached = map.get(key);
9 if (cached !== undefined) {
10 return cached;
11 }
12 const result = fn();
13 map.set(key, result);
14 return result;
15}
16exports.emplace = emplace;
17// Resolves property names or property paths defined with period-delimited
18// strings or arrays of strings. Property names that are found on the source
19// object are used directly (even if they include a period).
20// Nested property names that include periods, within a path, are only
21// understood in array paths.
22/**
23 * @internal
24 */
25function getPropertyByPath(source, path) {
26 if (typeof path === 'string' &&
27 Object.prototype.hasOwnProperty.call(source, path)) {
28 return source[path];
29 }
30 const parsedPath = typeof path === 'string' ? path.split('.') : path;
31 // eslint-disable-next-line @typescript-eslint/no-explicit-any
32 return parsedPath.reduce((previous, key) => {
33 if (previous === undefined) {
34 return previous;
35 }
36 return previous[key];
37 }, source);
38}
39exports.getPropertyByPath = getPropertyByPath;
40/** @internal */
41function removeUndefinedValuesFromObject(options) {
42 /* istanbul ignore if -- @preserve */
43 if (!options) {
44 return undefined;
45 }
46 return Object.fromEntries(Object.entries(options).filter(([, value]) => value !== undefined));
47}
48exports.removeUndefinedValuesFromObject = removeUndefinedValuesFromObject;
49//# sourceMappingURL=util.js.map
\No newline at end of file