| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338 |
- var Dep = require('../observer/dep')
- /**
- * Check is a string starts with $ or _
- *
- * @param {String} str
- * @return {Boolean}
- */
- exports.isReserved = function (str) {
- var c = (str + '').charCodeAt(0)
- return c === 0x24 || c === 0x5F
- }
- /**
- * Guard text output, make sure undefined outputs
- * empty string
- *
- * @param {*} value
- * @return {String}
- */
- exports.toString = function (value) {
- return value == null
- ? ''
- : value.toString()
- }
- /**
- * Check and convert possible numeric strings to numbers
- * before setting back to data
- *
- * @param {*} value
- * @return {*|Number}
- */
- exports.toNumber = function (value) {
- if (typeof value !== 'string') {
- return value
- } else {
- var parsed = Number(value)
- return isNaN(parsed)
- ? value
- : parsed
- }
- }
- /**
- * Convert string boolean literals into real booleans.
- *
- * @param {*} value
- * @return {*|Boolean}
- */
- exports.toBoolean = function (value) {
- return value === 'true'
- ? true
- : value === 'false'
- ? false
- : value
- }
- /**
- * Strip quotes from a string
- *
- * @param {String} str
- * @return {String | false}
- */
- exports.stripQuotes = function (str) {
- var a = str.charCodeAt(0)
- var b = str.charCodeAt(str.length - 1)
- return a === b && (a === 0x22 || a === 0x27)
- ? str.slice(1, -1)
- : false
- }
- /**
- * Camelize a hyphen-delmited string.
- *
- * @param {String} str
- * @return {String}
- */
- exports.camelize = function (str) {
- return str.replace(/-(\w)/g, toUpper)
- }
- function toUpper (_, c) {
- return c ? c.toUpperCase() : ''
- }
- /**
- * Hyphenate a camelCase string.
- *
- * @param {String} str
- * @return {String}
- */
- exports.hyphenate = function (str) {
- return str
- .replace(/([a-z\d])([A-Z])/g, '$1-$2')
- .toLowerCase()
- }
- /**
- * Converts hyphen/underscore/slash delimitered names into
- * camelized classNames.
- *
- * e.g. my-component => MyComponent
- * some_else => SomeElse
- * some/comp => SomeComp
- *
- * @param {String} str
- * @return {String}
- */
- var classifyRE = /(?:^|[-_\/])(\w)/g
- exports.classify = function (str) {
- return str.replace(classifyRE, toUpper)
- }
- /**
- * Simple bind, faster than native
- *
- * @param {Function} fn
- * @param {Object} ctx
- * @return {Function}
- */
- exports.bind = function (fn, ctx) {
- return function (a) {
- var l = arguments.length
- return l
- ? l > 1
- ? fn.apply(ctx, arguments)
- : fn.call(ctx, a)
- : fn.call(ctx)
- }
- }
- /**
- * Convert an Array-like object to a real Array.
- *
- * @param {Array-like} list
- * @param {Number} [start] - start index
- * @return {Array}
- */
- exports.toArray = function (list, start) {
- start = start || 0
- var i = list.length - start
- var ret = new Array(i)
- while (i--) {
- ret[i] = list[i + start]
- }
- return ret
- }
- /**
- * Mix properties into target object.
- *
- * @param {Object} to
- * @param {Object} from
- */
- exports.extend = function (to, from) {
- for (var key in from) {
- to[key] = from[key]
- }
- return to
- }
- /**
- * Quick object check - this is primarily used to tell
- * Objects from primitive values when we know the value
- * is a JSON-compliant type.
- *
- * @param {*} obj
- * @return {Boolean}
- */
- exports.isObject = function (obj) {
- return obj !== null && typeof obj === 'object'
- }
- /**
- * Strict object type check. Only returns true
- * for plain JavaScript objects.
- *
- * @param {*} obj
- * @return {Boolean}
- */
- var toString = Object.prototype.toString
- var OBJECT_STRING = '[object Object]'
- exports.isPlainObject = function (obj) {
- return toString.call(obj) === OBJECT_STRING
- }
- /**
- * Array type check.
- *
- * @param {*} obj
- * @return {Boolean}
- */
- exports.isArray = Array.isArray
- /**
- * Define a non-enumerable property
- *
- * @param {Object} obj
- * @param {String} key
- * @param {*} val
- * @param {Boolean} [enumerable]
- */
- exports.define = function (obj, key, val, enumerable) {
- Object.defineProperty(obj, key, {
- value: val,
- enumerable: !!enumerable,
- writable: true,
- configurable: true
- })
- }
- /**
- * Define a reactive property.
- *
- * @param {Object} obj
- * @param {String} key
- * @param {*} val
- */
- exports.defineReactive = function (obj, key, val) {
- var dep = new Dep()
- Object.defineProperty(obj, key, {
- get: function metaGetter () {
- if (Dep.target) {
- dep.depend()
- }
- return val
- },
- set: function metaSetter (newVal) {
- if (val !== newVal) {
- val = newVal
- dep.notify()
- }
- }
- })
- }
- /**
- * Debounce a function so it only gets called after the
- * input stops arriving after the given wait period.
- *
- * @param {Function} func
- * @param {Number} wait
- * @return {Function} - the debounced function
- */
- exports.debounce = function (func, wait) {
- var timeout, args, context, timestamp, result
- var later = function () {
- var last = Date.now() - timestamp
- if (last < wait && last >= 0) {
- timeout = setTimeout(later, wait - last)
- } else {
- timeout = null
- result = func.apply(context, args)
- if (!timeout) context = args = null
- }
- }
- return function () {
- context = this
- args = arguments
- timestamp = Date.now()
- if (!timeout) {
- timeout = setTimeout(later, wait)
- }
- return result
- }
- }
- /**
- * Manual indexOf because it's slightly faster than
- * native.
- *
- * @param {Array} arr
- * @param {*} obj
- */
- exports.indexOf = function (arr, obj) {
- var i = arr.length
- while (i--) {
- if (arr[i] === obj) return i
- }
- return -1
- }
- /**
- * Make a cancellable version of an async callback.
- *
- * @param {Function} fn
- * @return {Function}
- */
- exports.cancellable = function (fn) {
- var cb = function () {
- if (!cb.cancelled) {
- return fn.apply(this, arguments)
- }
- }
- cb.cancel = function () {
- cb.cancelled = true
- }
- return cb
- }
- /**
- * Check if two values are loosely equal - that is,
- * if they are plain objects, do they have the same shape?
- *
- * @param {*} a
- * @param {*} b
- * @return {Boolean}
- */
- exports.looseEqual = function (a, b) {
- /* eslint-disable eqeqeq */
- return a == b || (
- exports.isObject(a) && exports.isObject(b)
- ? JSON.stringify(a) === JSON.stringify(b)
- : false
- )
- /* eslint-enable eqeqeq */
- }
|