mtl-js-sdk
Version:
ynf-fw-mtl-api
162 lines (143 loc) • 4 kB
JavaScript
/*
* @Author: wangyingliang@yonyou.com
* @Date: 2024-07-22 15:45:20
* @LastEditors: wangyingliang wangyingliang@yonyou.com
* @LastEditTime: 2024-07-22 16:08:00
* @FilePath: /mtl-api-project/src/common/utils.js
* @Description: 工具类
* Copyright (c) 2024 by Yonyou, All Rights Reserved.
*/
let utils = {};
/**
* 给 obj[key] 定义 getter / setter.
*/
utils.defineGetterSetter = function (obj, key, getFunc, opt_setFunc) {
let 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);
}
let len = a.length;
for (let 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) {
let 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;
}
let 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 () {
let args = params || arguments;
return func.apply(context, args);
};
};
// ------------------------------------------------------------------------------
function UUIDcreatePart(length) {
let uuidpart = '';
for (let i = 0; i < length; i++) {
let 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
let F = function () { };
// 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);
}
};
export default utils;