-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck.js
42 lines (34 loc) · 1.05 KB
/
check.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
import isPlainObj from 'is-plain-obj'
// Return whether the argument is an object that follows the shape expected
// by `set()`.
export const test = function (updatesObj) {
return testUpdatesObj(updatesObj) === undefined
}
export const testUpdatesObj = function (updatesObj) {
if (!isPlainObj(updatesObj)) {
return { error: 'plainObj' }
}
// eslint-disable-next-line fp/no-loops
for (const key in updatesObj) {
// eslint-disable-next-line max-depth
if (!isValidKey(key)) {
return { error: 'key', key }
}
}
return testSymbols(updatesObj)
}
const testSymbols = function (updatesObj) {
const [symbol] = Object.getOwnPropertySymbols(updatesObj)
if (symbol !== undefined) {
return { error: 'symbol', symbol }
}
}
const isValidKey = function (key) {
return key === ANY_KEY || UPDATE_KEY_REGEXP.test(key)
}
// Special key targeting all array elements
export const ANY_KEY = '*'
// Means the value should be prepended
export const PREPEND_CHAR = '+'
// Matches -5, 5+ or -5+, for any integer
const UPDATE_KEY_REGEXP = /^-?\d+\+?$/u