-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
326 lines (299 loc) · 7.83 KB
/
utils.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
var config = require('./config'),
toString = ({}).toString,
win = window,
console = win.console,
def = Object.defineProperty,
OBJECT = 'object',
THIS_RE = /[^\w]this[^\w]/,
BRACKET_RE_S = /\['([^']+)'\]/g,
BRACKET_RE_D = /\["([^"]+)"\]/g,
hasClassList = 'classList' in document.documentElement,
ViewModel // late def
var defer =
win.requestAnimationFrame ||
win.webkitRequestAnimationFrame ||
win.setTimeout
/**
* Normalize keypath with possible brackets into dot notations
*/
function normalizeKeypath (key) {
return key.indexOf('[') < 0
? key
: key.replace(BRACKET_RE_S, '.$1')
.replace(BRACKET_RE_D, '.$1')
}
var utils = module.exports = {
/**
* Convert a string template to a dom fragment
*/
toFragment: require('./fragment'),
/**
* Parse the various types of template options
*/
parseTemplateOption: require('./template-parser.js'),
/**
* get a value from an object keypath
*/
get: function (obj, key) {
/* jshint eqeqeq: false */
key = normalizeKeypath(key)
if (key.indexOf('.') < 0) {
return obj[key]
}
var path = key.split('.'),
d = -1, l = path.length
while (++d < l && obj != null) {
obj = obj[path[d]]
}
return obj
},
/**
* set a value to an object keypath
*/
set: function (obj, key, val) {
/* jshint eqeqeq: false */
key = normalizeKeypath(key)
if (key.indexOf('.') < 0) {
obj[key] = val
return
}
var path = key.split('.'),
d = -1, l = path.length - 1
while (++d < l) {
if (obj[path[d]] == null) {
obj[path[d]] = {}
}
obj = obj[path[d]]
}
obj[path[d]] = val
},
/**
* return the base segment of a keypath
*/
baseKey: function (key) {
return key.indexOf('.') > 0
? key.split('.')[0]
: key
},
/**
* Create a prototype-less object
* which is a better hash/map
*/
hash: function () {
return Object.create(null)
},
/**
* get an attribute and remove it.
*/
attr: function (el, type) {
var attr = config.prefix + '-' + type,
val = el.getAttribute(attr)
if (val !== null) {
el.removeAttribute(attr)
}
return val
},
/**
* Define an ienumerable property
* This avoids it being included in JSON.stringify
* or for...in loops.
*/
defProtected: function (obj, key, val, enumerable, writable) {
def(obj, key, {
value : val,
enumerable : enumerable,
writable : writable,
configurable : true
})
},
/**
* A less bullet-proof but more efficient type check
* than Object.prototype.toString
*/
isObject: function (obj) {
return typeof obj === OBJECT && obj && !Array.isArray(obj)
},
/**
* A more accurate but less efficient type check
*/
isTrueObject: function (obj) {
return toString.call(obj) === '[object Object]'
},
/**
* Most simple bind
* enough for the usecase and fast than native bind()
*/
bind: function (fn, ctx) {
return function (arg) {
return fn.call(ctx, arg)
}
},
/**
* Make sure null and undefined output empty string
*/
guard: function (value) {
/* jshint eqeqeq: false, eqnull: true */
return value == null
? ''
: (typeof value == 'object')
? JSON.stringify(value)
: value
},
/**
* When setting value on the VM, parse possible numbers
*/
checkNumber: function (value) {
return (isNaN(value) || value === null || typeof value === 'boolean')
? value
: Number(value)
},
/**
* simple extend
*/
extend: function (obj, ext) {
for (var key in ext) {
if (obj[key] !== ext[key]) {
obj[key] = ext[key]
}
}
return obj
},
/**
* filter an array with duplicates into uniques
*/
unique: function (arr) {
var hash = utils.hash(),
i = arr.length,
key, res = []
while (i--) {
key = arr[i]
if (hash[key]) continue
hash[key] = 1
res.push(key)
}
return res
},
/**
* Convert the object to a ViewModel constructor
* if it is not already one
*/
toConstructor: function (obj) {
ViewModel = ViewModel || require('./viewmodel')
return utils.isObject(obj)
? ViewModel.extend(obj)
: typeof obj === 'function'
? obj
: null
},
/**
* Check if a filter function contains references to `this`
* If yes, mark it as a computed filter.
*/
checkFilter: function (filter) {
if (THIS_RE.test(filter.toString())) {
filter.computed = true
}
},
/**
* convert certain option values to the desired format.
*/
processOptions: function (options) {
var components = options.components,
partials = options.partials,
template = options.template,
filters = options.filters,
key
if (components) {
for (key in components) {
components[key] = utils.toConstructor(components[key])
}
}
if (partials) {
for (key in partials) {
partials[key] = utils.parseTemplateOption(partials[key])
}
}
if (filters) {
for (key in filters) {
utils.checkFilter(filters[key])
}
}
if (template) {
options.template = utils.parseTemplateOption(template)
}
},
/**
* used to defer batch updates
*/
nextTick: function (cb) {
defer(cb, 0)
},
/**
* add class for IE9
* uses classList if available
*/
addClass: function (el, cls) {
if (hasClassList) {
el.classList.add(cls)
} else {
var cur = ' ' + el.className + ' '
if (cur.indexOf(' ' + cls + ' ') < 0) {
el.className = (cur + cls).trim()
}
}
},
/**
* remove class for IE9
*/
removeClass: function (el, cls) {
if (hasClassList) {
el.classList.remove(cls)
} else {
var cur = ' ' + el.className + ' ',
tar = ' ' + cls + ' '
while (cur.indexOf(tar) >= 0) {
cur = cur.replace(tar, ' ')
}
el.className = cur.trim()
}
},
/**
* Convert an object to Array
* used in v-repeat and array filters
*/
objectToArray: function (obj) {
var res = [], val, data
for (var key in obj) {
val = obj[key]
data = utils.isObject(val)
? val
: { $value: val }
data.$key = key
res.push(data)
}
return res
}
}
enableDebug()
function enableDebug () {
/**
* log for debugging
*/
utils.log = function (msg) {
if (config.debug && console) {
console.log(msg)
}
}
/**
* warnings, traces by default
* can be suppressed by `silent` option.
*/
utils.warn = function (msg) {
if (!config.silent && console) {
console.warn(msg)
if (config.debug && console.trace) {
console.trace()
}
}
}
}