-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsippets-objects.js
99 lines (83 loc) · 2.8 KB
/
sippets-objects.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* 23. default
* This snippet assigns default values for all properties in an object that are undefined.
*/
const defaults = (obj, ...defs) =>
Object.assign({}, obj, ...defs.reverse(), obj)
defaults({ a: 1 }, { b: 2 }, { b: 6 }, { a: 3 }) // { a: 1, b: 2 }
/**
* 37. findKey
* This snippet returns the first key that satisfies a given function.
*/
const findKey = (obj, fn) =>
Object.keys(obj).find((key) => fn(obj[key], key, obj))
findKey(
{
barney: { age: 36, active: true },
fred: { age: 40, active: false },
pebbles: { age: 1, active: true },
},
(o) => o['active']
) // 'barney'
/**
* 41. forOwn
* This snippet iterates on each property of an object and iterates a callback for each one respectively.
*/
const forOwn = (obj, fn) =>
Object.keys(obj).forEach((key) => fn(obj[key], key, obj))
forOwn({ foo: 'bar', a: 1 }, (v) => console.log(v)) // 'bar', 1
/**
* 70. isObject
* This snippet can be used to check whether a provided value is an object. It uses the Object constructor to create an object wrapper for the given value.
* If it is already an object, then an object type that corresponds to the given value will be returned. Otherwise, a new object will be returned.
*/
const isObject = (obj) => obj === Object(obj)
isObject([1, 2, 3, 4]) // true
isObject([]) // true
isObject(['Hello!']) // true
isObject({ a: 1 }) // true
isObject({}) // true
isObject(true) // false
/**
* 71. isObjectLike
* This snippet can be used to check if a value is not null and that its typeof is “object”.
*/
const isObjectLike = (val) => val !== null && typeof val === 'object'
isObjectLike({}) // true
isObjectLike([1, 2, 3]) // true
isObjectLike((x) => x) // false
isObjectLike(null) // false
/**
* 72. isPlainObject
* This snippet checks whether a value is an object created by the Object constructor.
*/
const isPlainObject = (val) =>
!!val && typeof val === 'object' && val.constructor === Object
isPlainObject({ a: 1 }) // true
isPlainObject(new Map()) // false
/**
* 81. matches
* This snippet compares two objects to determine if the first one contains the same property values as the second one.
*/
const matches = (obj, source) =>
Object.keys(source).every(
(key) => obj.hasOwnProperty(key) && obj[key] === source[key]
)
matches({ age: 25, hair: 'long', beard: true }, { hair: 'long', beard: true }) // true
matches({ hair: 'long', beard: true }, { age: 25, hair: 'long', beard: true }) // false
/**
* 104. shallowClone
* This snippet can be used to create a shallow clone of an object.
*/
const shallowClone = (obj) => Object.assign({}, obj)
const a = { x: true, y: 1 }
const b = shallowClone(a) // a !== b
/**
* Create an empty object without any inheritance
*/
const a = Object.create(null)
/**
* Check if an object is empty
*/
const isEmpty = (obj) =>
Reflect.ownKeys(obj).length === 0 && obj.constructor === Object