1 | import isPlainObject from 'lodash/isPlainObject'
|
2 | import { Current } from '@tarojs/taro'
|
3 | import { SimpleMap } from '@tarojs/utils'
|
4 |
|
5 | export function addLeadingSlash (str) {
|
6 | return str[0] === '/' ? str : `/${str}`
|
7 | }
|
8 |
|
9 | export function isEmptyObject (obj) {
|
10 | if (!obj || !isPlainObject(obj)) {
|
11 | return false
|
12 | }
|
13 | for (const n in obj) {
|
14 | if (obj.hasOwnProperty(n)) {
|
15 | return false
|
16 | }
|
17 | }
|
18 | return true
|
19 | }
|
20 |
|
21 | export function isUndefined (o) {
|
22 | return o === undefined
|
23 | }
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | export function objClone (jsonObj) {
|
31 | let buf
|
32 | if (Array.isArray(jsonObj)) {
|
33 | buf = []
|
34 | let i = jsonObj.length
|
35 | while (i--) {
|
36 | buf[i] = objClone(jsonObj[i])
|
37 | }
|
38 | return buf
|
39 | } else if (isPlainObject(jsonObj)) {
|
40 | buf = {}
|
41 | for (const k in jsonObj) {
|
42 | buf[k] = objClone(jsonObj[k])
|
43 | }
|
44 | return buf
|
45 | } else {
|
46 | return jsonObj
|
47 | }
|
48 | }
|
49 |
|
50 | export function getPrototype (obj) {
|
51 |
|
52 | if (Object.getPrototypeOf) {
|
53 | return Object.getPrototypeOf(obj)
|
54 | } else if (obj.__proto__) {
|
55 | return obj.__proto__
|
56 | }
|
57 |
|
58 | return obj.constructor.prototype
|
59 | }
|
60 |
|
61 | export function getPrototypeChain (obj) {
|
62 | const protoChain = []
|
63 | while ((obj = getPrototype(obj))) {
|
64 | protoChain.push(obj)
|
65 | }
|
66 | return protoChain
|
67 | }
|
68 |
|
69 | export function noop () {}
|
70 |
|
71 | export function isFunction (arg) {
|
72 | return typeof arg === 'function'
|
73 | }
|
74 |
|
75 | export function isArray (arg) {
|
76 | return Array.isArray(arg)
|
77 | }
|
78 |
|
79 | export function shakeFnFromObject (obj) {
|
80 | let newObj
|
81 | if (isArray(obj)) {
|
82 | newObj = []
|
83 | const len = obj.length
|
84 | for (let i = 0; i < len; i++) {
|
85 | newObj.push(shakeFnFromObject(obj[i]))
|
86 | }
|
87 | } else if (isPlainObject(obj)) {
|
88 | newObj = {}
|
89 | for (const key in obj) {
|
90 | if (isFunction(obj[key])) {
|
91 | continue
|
92 | }
|
93 | const ret = shakeFnFromObject(obj[key])
|
94 | newObj[key] = ret
|
95 | }
|
96 | } else {
|
97 | return obj
|
98 | }
|
99 | return newObj
|
100 | }
|
101 |
|
102 | const keyList = Object.keys
|
103 | const hasProp = Object.prototype.hasOwnProperty
|
104 |
|
105 | function diffArrToPath (to, from, res = {}, keyPrev = '') {
|
106 | const len = to.length
|
107 | for (let i = 0; i < len; i++) {
|
108 | const toItem = to[i]
|
109 | const fromItem = from[i]
|
110 | const targetKey = `${keyPrev}[${i}]`
|
111 | if (toItem === fromItem) {
|
112 | continue
|
113 | } else if (typeof toItem !== typeof fromItem) {
|
114 | res[targetKey] = toItem
|
115 | } else {
|
116 | if (typeof toItem !== 'object') {
|
117 | res[targetKey] = toItem
|
118 | } else {
|
119 | const arrTo = isArray(toItem)
|
120 | const arrFrom = isArray(fromItem)
|
121 | if (arrTo !== arrFrom) {
|
122 | res[targetKey] = toItem
|
123 | } else if (arrTo && arrFrom) {
|
124 | if (toItem.length < fromItem.length) {
|
125 | res[targetKey] = toItem
|
126 | } else {
|
127 |
|
128 | diffArrToPath(toItem, fromItem, res, `${targetKey}`)
|
129 | }
|
130 | } else {
|
131 | if (!toItem || !fromItem || keyList(toItem).length < keyList(fromItem).length) {
|
132 | res[targetKey] = toItem
|
133 | } else {
|
134 |
|
135 | diffObjToPath(toItem, fromItem, res, `${targetKey}.`)
|
136 | }
|
137 | }
|
138 | }
|
139 | }
|
140 | }
|
141 | return res
|
142 | }
|
143 |
|
144 |
|
145 | export function diffObjToPath (to, from, res = {}, keyPrev = '') {
|
146 | const keys = keyList(to)
|
147 | const len = keys.length
|
148 |
|
149 | for (let i = 0; i < len; i++) {
|
150 | const key = keys[i]
|
151 | const toItem = to[key]
|
152 | const fromItem = from[key]
|
153 | const targetKey = `${keyPrev}${key}`
|
154 | if (toItem === fromItem) {
|
155 | continue
|
156 | } else
|
157 | if (!hasProp.call(from, key)) {
|
158 | res[targetKey] = toItem
|
159 | } else
|
160 | if (typeof toItem !== typeof fromItem) {
|
161 | res[targetKey] = toItem
|
162 | } else {
|
163 | if (typeof toItem !== 'object') {
|
164 | res[targetKey] = toItem
|
165 | } else {
|
166 | const arrTo = isArray(toItem)
|
167 | const arrFrom = isArray(fromItem)
|
168 | if (arrTo !== arrFrom) {
|
169 | res[targetKey] = toItem
|
170 | } else if (arrTo && arrFrom) {
|
171 | if (toItem.length < fromItem.length) {
|
172 | res[targetKey] = toItem
|
173 | } else {
|
174 |
|
175 | diffArrToPath(toItem, fromItem, res, `${targetKey}`)
|
176 | }
|
177 | } else {
|
178 |
|
179 | if (!toItem || !fromItem || keyList(toItem).length < keyList(fromItem).length) {
|
180 | res[targetKey] = toItem
|
181 | } else {
|
182 |
|
183 | diffObjToPath(toItem, fromItem, res, `${targetKey}.`)
|
184 | }
|
185 | }
|
186 | }
|
187 | }
|
188 | }
|
189 | return res
|
190 | }
|
191 |
|
192 | export function queryToJson (str) {
|
193 | const dec = decodeURIComponent
|
194 | const qp = str.split('&')
|
195 | let ret = {}
|
196 | let name
|
197 | let val
|
198 | for (let i = 0, l = qp.length, item; i < l; ++i) {
|
199 | item = qp[i]
|
200 | if (item.length) {
|
201 | const s = item.indexOf('=')
|
202 | if (s < 0) {
|
203 | name = dec(item)
|
204 | val = ''
|
205 | } else {
|
206 | name = dec(item.slice(0, s))
|
207 | val = dec(item.slice(s + 1))
|
208 | }
|
209 | if (typeof ret[name] === 'string') {
|
210 | ret[name] = [ret[name]]
|
211 | }
|
212 |
|
213 | if (isArray(ret[name])) {
|
214 | ret[name].push(val)
|
215 | } else {
|
216 | ret[name] = val
|
217 | }
|
218 | }
|
219 | }
|
220 | return ret
|
221 | }
|
222 |
|
223 | const _loadTime = (new Date()).getTime().toString()
|
224 | let _i = 1
|
225 | export function getUniqueKey () {
|
226 | return _loadTime + (_i++)
|
227 | }
|
228 |
|
229 | export function getObjChainValue (obj, keyChain) {
|
230 | const keys = keyChain.split('.')
|
231 | for (let i = 0, len = keys.length; i < len; i++) {
|
232 | const key = keys[i]
|
233 | if (i === len - 1) return obj[key]
|
234 | obj = obj[key]
|
235 | }
|
236 | }
|
237 |
|
238 | let id = 0
|
239 | function genId () {
|
240 | return String(id++)
|
241 | }
|
242 |
|
243 | let compIdsMapper
|
244 | try {
|
245 | compIdsMapper = new Map()
|
246 | } catch (error) {
|
247 | compIdsMapper = new SimpleMap()
|
248 | }
|
249 | export function genCompid (key, isNeedCreate) {
|
250 | if (!Current || !Current.current || !Current.current.$scope) return []
|
251 |
|
252 | const prevId = compIdsMapper.get(key)
|
253 | if (isNeedCreate) {
|
254 | const id = genId()
|
255 | compIdsMapper.set(key, id)
|
256 | return [prevId, id]
|
257 | } else {
|
258 | const id = prevId || genId()
|
259 | !prevId && compIdsMapper.set(key, id)
|
260 | return [null, id]
|
261 | }
|
262 | }
|
263 |
|
264 | let prefix = 0
|
265 | export function genCompPrefix () {
|
266 | return String(prefix++)
|
267 | }
|