// 輸入目標, 返回目標具體類型 /** * @param {any} target 要判斷類型的目標對象 * @returns 返回相應類型 */ export const targetType = (target) => { let result = '' switch (Object.prototype.toString.call(target)) { case '[object Object]': result = 'Object' break case '[object Array]': result = 'Array' break case '[object String]': result = 'String' break case '[object Number]': result = 'Number' break case '[object Boolean]': result = 'Boolean' break case '[object Function]': result = 'Function' break case '[object Null]': result = 'Null' break case '[object Undefined]': result = 'Undefined' break } return result } // 檢查目標是否有效( 空數組以及空對象被認爲不合法 ) export const checkIsValid = (item) => { switch (typeof (item)) { case 'undefined': return false case 'null': return false case 'number': { if (isNaN(item)) { return false } else { return true } } case 'object': { for (let key in item) { return true } return false } } } // 駝峯與下劃線寫法相互轉換 /** * @function 駝峯命名->下劃線命名 * @param {string} name {駝峯名稱} * @return {string} {下劃線名稱} */ export const toLineName = (name) => { return name && name.replace(/([A-Z])/g, '_$1').toLowerCase() } /** * @function 下劃線命名->駝峯命名 * @param {string} name {下劃線名稱} * @return {string} {駝峯名稱} */ export const toHumpName = (name) => { return name && name.replace(/_([a-zA-Z])/g, function (all, letter) { return letter.toUpperCase() }) } // 合併對象 /** * Mix properties into target object. */ export function extend (to, _from) { for (const key in _from) { to[key] = _from[key] } return to } // 類數組轉爲真數組 /** * Convert an Array-like object to a real Array. */ export function toArray (list, start) { start = start || 0 let i = list.length - start const ret = new Array(i) while (i--) { ret[i] = list[i + start] } return ret }