-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
49 lines (44 loc) · 1.63 KB
/
index.js
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
/**
* Represents an error in some accessing operation
* @interface
* @prop { true } isNothing
*/
const Nothing = { isNothing: true }
/**
* Gets the obj[key] property, returns {@see Nothing} if it doesnt exists
* @private
* @param { Object } obj Object to be accessed
* @param { String } key Key to be used
* @returns { * | Nothing } Accessed property or nothing
*/
const get = (obj, key) => (obj && obj[key] !== null && obj[key] !== undefined) ? obj[key] : Nothing
/**
* Access deep properties on object going through the given path,
* returns {@link Nothing} if it is not possible to fetch the value
* @global
* @name deepGet
* @param { Object } obj Object to be accessed
* @param { String } path Path to be accessed separated by .
* @returns { * | Nothing } Final accessed property or nothing
*/
const deepGet = (obj, path) => path && path.split('.').reduce(get, obj)
/**
* Checks if x is Nothing
* @global
* @name isNothing
* @param { * } x Anything you want to test
* @returns { Boolean } whether it's equal or not
*/
const isNothing = (x) => x === Nothing
/**
* Access deep properties on object going through the given path
* If it would return a {@link Nothing}, it returns `fallback` instead
* @global
* @name deepGetOrElse
* @param { Object } obj Object to be accessed
* @param { String } path Path to be accessed separated by .
* @param { * } fallback the value to return if obj[path] is Nothing
* @returns { * } The accessed prop, or the fallback value
*/
const deepGetOrElse = (obj, path, fallback) => isNothing(deepGet(obj, path)) ? fallback : deepGet(obj, path)
module.exports = { deepGet, isNothing, deepGetOrElse }