github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/public/libs/vue-1.0.24/src/util/lang.js (about) 1 /** 2 * Set a property on an object. Adds the new property and 3 * triggers change notification if the property doesn't 4 * already exist. 5 * 6 * @param {Object} obj 7 * @param {String} key 8 * @param {*} val 9 * @public 10 */ 11 12 export function set (obj, key, val) { 13 if (hasOwn(obj, key)) { 14 obj[key] = val 15 return 16 } 17 if (obj._isVue) { 18 set(obj._data, key, val) 19 return 20 } 21 var ob = obj.__ob__ 22 if (!ob) { 23 obj[key] = val 24 return 25 } 26 ob.convert(key, val) 27 ob.dep.notify() 28 if (ob.vms) { 29 var i = ob.vms.length 30 while (i--) { 31 var vm = ob.vms[i] 32 vm._proxy(key) 33 vm._digest() 34 } 35 } 36 return val 37 } 38 39 /** 40 * Delete a property and trigger change if necessary. 41 * 42 * @param {Object} obj 43 * @param {String} key 44 */ 45 46 export function del (obj, key) { 47 if (!hasOwn(obj, key)) { 48 return 49 } 50 delete obj[key] 51 var ob = obj.__ob__ 52 if (!ob) { 53 if (obj._isVue) { 54 delete obj._data[key] 55 obj._digest() 56 } 57 return 58 } 59 ob.dep.notify() 60 if (ob.vms) { 61 var i = ob.vms.length 62 while (i--) { 63 var vm = ob.vms[i] 64 vm._unproxy(key) 65 vm._digest() 66 } 67 } 68 } 69 70 var hasOwnProperty = Object.prototype.hasOwnProperty 71 /** 72 * Check whether the object has the property. 73 * 74 * @param {Object} obj 75 * @param {String} key 76 * @return {Boolean} 77 */ 78 export function hasOwn (obj, key) { 79 return hasOwnProperty.call(obj, key) 80 } 81 82 /** 83 * Check if an expression is a literal value. 84 * 85 * @param {String} exp 86 * @return {Boolean} 87 */ 88 89 var literalValueRE = /^\s?(true|false|-?[\d\.]+|'[^']*'|"[^"]*")\s?$/ 90 export function isLiteral (exp) { 91 return literalValueRE.test(exp) 92 } 93 94 /** 95 * Check if a string starts with $ or _ 96 * 97 * @param {String} str 98 * @return {Boolean} 99 */ 100 101 export function isReserved (str) { 102 var c = (str + '').charCodeAt(0) 103 return c === 0x24 || c === 0x5F 104 } 105 106 /** 107 * Guard text output, make sure undefined outputs 108 * empty string 109 * 110 * @param {*} value 111 * @return {String} 112 */ 113 114 export function _toString (value) { 115 return value == null 116 ? '' 117 : value.toString() 118 } 119 120 /** 121 * Check and convert possible numeric strings to numbers 122 * before setting back to data 123 * 124 * @param {*} value 125 * @return {*|Number} 126 */ 127 128 export function toNumber (value) { 129 if (typeof value !== 'string') { 130 return value 131 } else { 132 var parsed = Number(value) 133 return isNaN(parsed) 134 ? value 135 : parsed 136 } 137 } 138 139 /** 140 * Convert string boolean literals into real booleans. 141 * 142 * @param {*} value 143 * @return {*|Boolean} 144 */ 145 146 export function toBoolean (value) { 147 return value === 'true' 148 ? true 149 : value === 'false' 150 ? false 151 : value 152 } 153 154 /** 155 * Strip quotes from a string 156 * 157 * @param {String} str 158 * @return {String | false} 159 */ 160 161 export function stripQuotes (str) { 162 var a = str.charCodeAt(0) 163 var b = str.charCodeAt(str.length - 1) 164 return a === b && (a === 0x22 || a === 0x27) 165 ? str.slice(1, -1) 166 : str 167 } 168 169 /** 170 * Camelize a hyphen-delmited string. 171 * 172 * @param {String} str 173 * @return {String} 174 */ 175 176 var camelizeRE = /-(\w)/g 177 export function camelize (str) { 178 return str.replace(camelizeRE, toUpper) 179 } 180 181 function toUpper (_, c) { 182 return c ? c.toUpperCase() : '' 183 } 184 185 /** 186 * Hyphenate a camelCase string. 187 * 188 * @param {String} str 189 * @return {String} 190 */ 191 192 var hyphenateRE = /([a-z\d])([A-Z])/g 193 export function hyphenate (str) { 194 return str 195 .replace(hyphenateRE, '$1-$2') 196 .toLowerCase() 197 } 198 199 /** 200 * Converts hyphen/underscore/slash delimitered names into 201 * camelized classNames. 202 * 203 * e.g. my-component => MyComponent 204 * some_else => SomeElse 205 * some/comp => SomeComp 206 * 207 * @param {String} str 208 * @return {String} 209 */ 210 211 var classifyRE = /(?:^|[-_\/])(\w)/g 212 export function classify (str) { 213 return str.replace(classifyRE, toUpper) 214 } 215 216 /** 217 * Simple bind, faster than native 218 * 219 * @param {Function} fn 220 * @param {Object} ctx 221 * @return {Function} 222 */ 223 224 export function bind (fn, ctx) { 225 return function (a) { 226 var l = arguments.length 227 return l 228 ? l > 1 229 ? fn.apply(ctx, arguments) 230 : fn.call(ctx, a) 231 : fn.call(ctx) 232 } 233 } 234 235 /** 236 * Convert an Array-like object to a real Array. 237 * 238 * @param {Array-like} list 239 * @param {Number} [start] - start index 240 * @return {Array} 241 */ 242 243 export function toArray (list, start) { 244 start = start || 0 245 var i = list.length - start 246 var ret = new Array(i) 247 while (i--) { 248 ret[i] = list[i + start] 249 } 250 return ret 251 } 252 253 /** 254 * Mix properties into target object. 255 * 256 * @param {Object} to 257 * @param {Object} from 258 */ 259 260 export function extend (to, from) { 261 var keys = Object.keys(from) 262 var i = keys.length 263 while (i--) { 264 to[keys[i]] = from[keys[i]] 265 } 266 return to 267 } 268 269 /** 270 * Quick object check - this is primarily used to tell 271 * Objects from primitive values when we know the value 272 * is a JSON-compliant type. 273 * 274 * @param {*} obj 275 * @return {Boolean} 276 */ 277 278 export function isObject (obj) { 279 return obj !== null && typeof obj === 'object' 280 } 281 282 /** 283 * Strict object type check. Only returns true 284 * for plain JavaScript objects. 285 * 286 * @param {*} obj 287 * @return {Boolean} 288 */ 289 290 var toString = Object.prototype.toString 291 var OBJECT_STRING = '[object Object]' 292 export function isPlainObject (obj) { 293 return toString.call(obj) === OBJECT_STRING 294 } 295 296 /** 297 * Array type check. 298 * 299 * @param {*} obj 300 * @return {Boolean} 301 */ 302 303 export const isArray = Array.isArray 304 305 /** 306 * Define a property. 307 * 308 * @param {Object} obj 309 * @param {String} key 310 * @param {*} val 311 * @param {Boolean} [enumerable] 312 */ 313 314 export function def (obj, key, val, enumerable) { 315 Object.defineProperty(obj, key, { 316 value: val, 317 enumerable: !!enumerable, 318 writable: true, 319 configurable: true 320 }) 321 } 322 323 /** 324 * Debounce a function so it only gets called after the 325 * input stops arriving after the given wait period. 326 * 327 * @param {Function} func 328 * @param {Number} wait 329 * @return {Function} - the debounced function 330 */ 331 332 export function debounce (func, wait) { 333 var timeout, args, context, timestamp, result 334 var later = function () { 335 var last = Date.now() - timestamp 336 if (last < wait && last >= 0) { 337 timeout = setTimeout(later, wait - last) 338 } else { 339 timeout = null 340 result = func.apply(context, args) 341 if (!timeout) context = args = null 342 } 343 } 344 return function () { 345 context = this 346 args = arguments 347 timestamp = Date.now() 348 if (!timeout) { 349 timeout = setTimeout(later, wait) 350 } 351 return result 352 } 353 } 354 355 /** 356 * Manual indexOf because it's slightly faster than 357 * native. 358 * 359 * @param {Array} arr 360 * @param {*} obj 361 */ 362 363 export function indexOf (arr, obj) { 364 var i = arr.length 365 while (i--) { 366 if (arr[i] === obj) return i 367 } 368 return -1 369 } 370 371 /** 372 * Make a cancellable version of an async callback. 373 * 374 * @param {Function} fn 375 * @return {Function} 376 */ 377 378 export function cancellable (fn) { 379 var cb = function () { 380 if (!cb.cancelled) { 381 return fn.apply(this, arguments) 382 } 383 } 384 cb.cancel = function () { 385 cb.cancelled = true 386 } 387 return cb 388 } 389 390 /** 391 * Check if two values are loosely equal - that is, 392 * if they are plain objects, do they have the same shape? 393 * 394 * @param {*} a 395 * @param {*} b 396 * @return {Boolean} 397 */ 398 399 export function looseEqual (a, b) { 400 /* eslint-disable eqeqeq */ 401 return a == b || ( 402 isObject(a) && isObject(b) 403 ? JSON.stringify(a) === JSON.stringify(b) 404 : false 405 ) 406 /* eslint-enable eqeqeq */ 407 }