-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-value.ts
63 lines (56 loc) · 2.04 KB
/
get-value.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* Please refer to the terms of the license agreement in the root of the project
*
* (c) 2024 Feedzai
*/
import { at, isObject } from "..";
interface IGetValueObject<GenericValue = unknown> {
[key: string]: GenericValue;
}
interface IGetValuePayload<Generic> {
required?: boolean;
defaultValue?: Generic;
}
/**
* Gets the value corresponding to the path of an object.
*
* If the object is required to have that path and the path does not exists, there are 2 possible outcomes:
* if a default value is provided, a warning is emitted indicating that value. If not, an error is emitted.
*
* If the object is not required to have that path, the default value (provided or undefined) is returned,
* without any warning.
*
* @template GenericData, GenericDefaultValue, GenericReturnValue
* @param {GenericData} object Holds the object to extract the value from.
* @param {string} path Holds the object path where to extract the value.
* @param {IGetValuePayload | undefined} [payload] Set of function options.
* @returns {GenericReturnValue | undefined}
*/
export function getValue<GenericValue, GenericReturnValue>(
object: GenericValue | IGetValueObject<GenericValue>,
path: string,
payload?: IGetValuePayload<GenericReturnValue> | undefined
): GenericReturnValue | undefined {
const defaultValue = isObject(payload) ? payload?.defaultValue : undefined;
const value = at(object as any, path) as GenericReturnValue[];
const res = value[0];
let required: boolean | undefined = false;
if (isObject(payload) && Object.prototype.hasOwnProperty.call(payload, "required")) {
required = payload.required;
}
if (res === undefined) {
if (required) {
if (defaultValue === undefined) {
console.error(`The path ${path} does not exist on the object.`);
} else {
console.warn(
`The path ${path} does not exist on the object. Using ${String(defaultValue)} instead.`
);
return defaultValue;
}
} else {
return defaultValue; // can be undefined, no problem
}
}
return res;
}