deno.land / x / fuse@v6.4.1 / src / helpers / get.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
50
51
52
import { isDefined, isString, isNumber, isBoolean, isArray, toString} from './types'
export default function get(obj, path) { let list = [] let arr = false
const deepGet = (obj, path, index) => { if (!path[index]) { // If there's no path left, we've arrived at the object we care about. list.push(obj) } else { let key = path[index]
const value = obj[key]
if (!isDefined(value)) { return }
// If we're at the last value in the path, and if it's a string/number/bool, // add it to the list if ( index === path.length - 1 && (isString(value) || isNumber(value) || isBoolean(value)) ) { list.push(toString(value)) } else if (isArray(value)) { arr = true // Search each item in the array. for (let i = 0, len = value.length; i < len; i += 1) { deepGet(value[i], path, index + 1) } } else if (path.length) { // An object. Recurse further. deepGet(value, path, index + 1) } } }
// Backwards compatibility (since path used to be a string) deepGet(obj, isString(path) ? path.split('.') : path, 0)
return arr ? list : list[0]}
fuse

Version Info

Tagged at
3 years ago