UNPKG

mtl-js-sdk

Version:

180 lines (134 loc) 4.12 kB
"use strict"; function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } define(function () { var utils = {}; /** * 给 obj[key] 定义 getter / setter. */ utils.defineGetterSetter = function (obj, key, getFunc, opt_setFunc) { var desc = { get: getFunc, configurable: true }; if (opt_setFunc) { desc.set = opt_setFunc; } Object.defineProperty(obj, key, desc); }; utils.defineGetter = utils.defineGetterSetter; utils.arrayIndexOf = function (a, item) { if (a.indexOf) { return a.indexOf(item); } var len = a.length; for (var i = 0; i < len; ++i) { if (a[i] === item) { return i; } } return -1; }; /** * Returns whether the item was found in the array. */ utils.arrayRemove = function (a, item) { var index = utils.arrayIndexOf(a, item); if (index !== -1) { a.splice(index, 1); } return index !== -1; }; utils.typeName = function (val) { return Object.prototype.toString.call(val).slice(8, -1); }; /** * Returns an indication of whether the argument is an array or not */ utils.isArray = Array.isArray || function (a) { return utils.typeName(a) === 'Array'; }; /** * Returns an indication of whether the argument is a Date or not */ utils.isDate = function (d) { return d instanceof Date; }; /** * Does a deep clone of the object. */ utils.clone = function (obj) { if (!obj || typeof obj === 'function' || utils.isDate(obj) || _typeof(obj) !== 'object') { return obj; } var retVal, i; if (utils.isArray(obj)) { retVal = []; for (i = 0; i < obj.length; ++i) { retVal.push(utils.clone(obj[i])); } return retVal; } retVal = {}; for (i in obj) { // https://issues.apache.org/jira/browse/CB-11522 'unknown' type may be returned in // custom protocol activation case on Windows Phone 8.1 causing "No such interface supported" exception // on cloning. if ((!(i in retVal) || retVal[i] !== obj[i]) && typeof obj[i] !== 'undefined' && typeof obj[i] !== 'unknown') { // eslint-disable-line valid-typeof retVal[i] = utils.clone(obj[i]); } } return retVal; }; /** * Returns a wrapped version of the function */ utils.close = function (context, func, params) { return function () { var args = params || arguments; return func.apply(context, args); }; }; // ------------------------------------------------------------------------------ function UUIDcreatePart(length) { var uuidpart = ''; for (var i = 0; i < length; i++) { var uuidchar = parseInt(Math.random() * 256, 10).toString(16); if (uuidchar.length === 1) { uuidchar = '0' + uuidchar; } uuidpart += uuidchar; } return uuidpart; } /** * Create a UUID */ utils.createUUID = function () { return UUIDcreatePart(4) + '-' + UUIDcreatePart(2) + '-' + UUIDcreatePart(2) + '-' + UUIDcreatePart(2) + '-' + UUIDcreatePart(6); }; /** * Extends a child object from a parent object using classical inheritance * pattern. */ utils.extend = function () { // proxy used to establish prototype chain var F = function F() {}; // extend Child from Parent return function (Child, Parent) { F.prototype = Parent.prototype; Child.prototype = new F(); Child.__super__ = Parent.prototype; Child.prototype.constructor = Child; }; }(); /** * Alerts a message in any available way: alert or console.log. */ utils.alert = function (msg) { if (window.alert) { window.alert(msg); } else if (console && console.log) { console.log(msg); } }; return utils; });