This repository was archived by the owner on Sep 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
149 lines (140 loc) · 3.4 KB
/
index.js
File metadata and controls
149 lines (140 loc) · 3.4 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const getQuery = query => {
const assign = (X, Path, value) => {
var path = Path.shift()
var toArr = path.substr(-2) == '[]'
if (toArr) {
path = path.substr(0, path.length - 2)
}
const isArr = X[path] instanceof Array
toArr = toArr || (X[path] != null && !(
Path.length && typeof X[path] == 'object'
))
if (!isArr && toArr) {
X[path] = X[path] == null ? [] : [X[path]]
} else if (X[path] == null) {
X[path] = {}
}
if (toArr || isArr) {
X[path].push(Path.length ? assign({}, Path, value) : value)
} else {
X[path] = Path.length ? assign(X[path], Path, value) : value
}
return X
}
return query.split('&').reduce((Q, pair) => {
var P = pair.split('=', 2)
if (P.length == 2) {
const key = decodeURIComponent(P[0])
const value = decodeURIComponent(P[1])
Q = assign(Q, key.split('.'), value)
}
return Q
}, {})
}
const setQuery = (Q, path) => {
path = path || ''
const finish = A => {
return A.filter(pair => {
return pair && pair.length
}).join('&')
}
const addPath = next => {
return path+(path.length ? '.' : '')+next
}
if (Q instanceof Array) {
const terminal = Q.reduce((terminal, value) => {
return terminal && (typeof value != 'object' || value == null)
}, true)
return finish(Q.map((value, i) => {
return setQuery(value, terminal && path ? (path+'[]') : addPath(i))
}))
} else if (Q != null && typeof Q == 'object') {
return finish(Object.keys(Q).map(key => {
return setQuery(Q[key], addPath(key))
}))
} else if (Q != null) {
return encodeURIComponent(path)+'='+encodeURIComponent(String(Q))
} else {
return ''
}
}
const merge = (A, B) => {
return Object.keys(B).reduce((R, key) => {
if (B[key] == null) {
delete R[key]
} else if (R[key] == null ||
typeof R[key] != 'object' ||
typeof B[key] != 'object'
) {
R[key] = B[key]
} else {
R[key] = merge(R[key], B[key])
}
return R
}, {...A})
}
const project = (A, Paths) => {
return Paths.reduce((R, path) => {
const P = path.split('.')
const l = P.length
var X = A
var Y = R
var Z = null
var W = null
var k = null
const stop = P.reduce((stop, key, i) => {
const last = i == l - 1
if (!stop && X[key] != null) {
if (Y[key] != null && !last) {
Y = Y[key]
} else if (k == null) {
k = key
if (!last) {
Z = {}
} else {
Z = X[key]
}
W = Z
} else {
if (!last) {
W[key] = {}
} else {
W[key] = X[key]
}
W = W[key]
}
X = X[key]
} else {
stop = true
}
return stop
}, false)
if (!stop) {
Y[k] = Z
}
return R
}, {})
}
export default function () {
const B = arguments[0]
const isObj = typeof B == 'object' || B == null
const load = X => {
if (X == null) {
return {}
} else if (typeof X != 'object') {
return getQuery(String(X))
} else {
return X
}
}
var Base = load(B)
for (var i = 1; i < arguments.length; i++) {
const A = load(arguments[i])
if (A instanceof Array) {
Base = project(Base, A)
} else {
Base = merge(Base, A)
}
}
return (isObj && i == 1) || (!isObj && i > 1) ? setQuery(Base) : Base
}