UNPKG

@kwiz/common

Version:

KWIZ common utilities and helpers for M365 platform

281 lines 10.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BoolFalseStrings = exports.BoolTrueStrings = void 0; exports.typeofFullName = typeofFullName; exports.getFromFullName = getFromFullName; exports.isTypeofFullNameObject = isTypeofFullNameObject; exports.isTypeofFullNameString = isTypeofFullNameString; exports.isTypeofFullNameNumber = isTypeofFullNameNumber; exports.isTypeofFullNameNullOrUndefined = isTypeofFullNameNullOrUndefined; exports.isTypeofFullNameUndefined = isTypeofFullNameUndefined; exports.isTypeofFullNameFunction = isTypeofFullNameFunction; exports.isTypeofFullNameBoolean = isTypeofFullNameBoolean; exports.isType = isType; exports.isObject = isObject; exports.isEmptyObject = isEmptyObject; exports.isNullOrUndefined = isNullOrUndefined; exports.isNullOrNaN = isNullOrNaN; exports.isNullOrEmptyString = isNullOrEmptyString; exports.isNotEmptyArray = isNotEmptyArray; exports.isNullOrEmptyArray = isNullOrEmptyArray; exports.isString = isString; exports.isNotEmptyString = isNotEmptyString; exports.isDate = isDate; exports.isNumeric = isNumeric; exports.isNumber = isNumber; exports.isNumberArray = isNumberArray; exports.isUndefined = isUndefined; exports.isFunction = isFunction; exports.isBoolean = isBoolean; exports.isPromise = isPromise; exports.isPrimitiveValue = isPrimitiveValue; exports.isValidGuid = isValidGuid; exports.isTrueString = isTrueString; exports.newGuid = newGuid; exports.hasMembers = hasMembers; exports.CompareVersion = CompareVersion; exports.SafeIfElse = SafeIfElse; var _objectTypes = { "Function": typeof (() => { }), "Undefined": typeof (undefined), "String": typeof (""), "Number": typeof (1), "Object": typeof ({}), "Boolean": typeof (true) }; /** check if a global object in that full name exists and return its type or "undefined" */ function typeofFullName(fullName, windowOrParent) { //todo: possible to merge with getFromFullName, but we need to distinguish between null and undefined if (!fullName) { return _objectTypes.Undefined; } try { let names = fullName.split("."); let len = names.length; let obj = windowOrParent || window; for (var i = 0; i < len; i++) { obj = obj[names[i]]; if (typeof obj === _objectTypes.Undefined) return _objectTypes.Undefined; if (obj === null && i < len - 1) //one of the chained objects (not the leaf) is null - so return undefined return _objectTypes.Undefined; } return typeof obj; } catch (ex) { return _objectTypes.Undefined; } } /** get the value by full name of property */ function getFromFullName(fullName, windowOrParent) { try { if (isNullOrEmptyString(fullName)) { return null; } try { var names = fullName.split("."); var len = names.length; var obj = windowOrParent || window; for (var i = 0; i < len; i++) { obj = obj[names[i]]; if (typeof obj === _objectTypes.Undefined || obj === null) { return null; } } return obj; } catch (ex) { } } catch (e) { } return null; } function isTypeofFullNameObject(fullName, windowOrParent) { return typeofFullName(fullName, windowOrParent) === _objectTypes.Object; } function isTypeofFullNameString(fullName, windowOrParent) { return typeofFullName(fullName, windowOrParent) === _objectTypes.String; } function isTypeofFullNameNumber(fullName, windowOrParent) { return typeofFullName(fullName, windowOrParent) === _objectTypes.Number; } function isTypeofFullNameNullOrUndefined(fullName, windowOrParent) { if (typeofFullName(fullName, windowOrParent) === _objectTypes.Undefined) { return true; } try { var names = fullName.split("."); var len = names.length; var obj = windowOrParent || window; for (var i = 0; i < len && obj !== null; i++) { obj = obj[names[i]]; } return obj === null || obj === undefined; } catch (ex) { return true; } } function isTypeofFullNameUndefined(fullName, windowOrParent) { return typeofFullName(fullName, windowOrParent) === _objectTypes.Undefined; } function isTypeofFullNameFunction(fullName, windowOrParent) { return typeofFullName(fullName, windowOrParent) === _objectTypes.Function; } function isTypeofFullNameBoolean(fullName, windowOrParent) { return typeofFullName(fullName, windowOrParent) === _objectTypes.Boolean; } function isType(obj, str) { return typeof (obj) === str; } // eslint-disable-next-line @typescript-eslint/ban-types function isObject(obj) { return isType(obj, _objectTypes.Object); } /** Checks if obj is empty - as primitive, array or object * If an object, pass an optional array of keys to ignore */ function isEmptyObject(obj, options) { let ignoreKeys = options && options.ignoreKeys || []; return isNullOrUndefined(obj) || (Array.isArray(obj) && obj.length === 0) || (isObject(obj) && Object.keys(obj) //if options.ignoreKeys is not empty - only include keys that are NOT in this array .filter(key => SafeIfElse(() => ignoreKeys.indexOf(key) < 0, true)) .length === 0); } function isNullOrUndefined(obj) { return isUndefined(obj) || obj === null; } /** return true if o is undefined, null or not a number */ function isNullOrNaN(o) { return isNullOrEmptyString(o) || isNaN(o); } /** return true if o is undefined, null or empty string - false for any other types or non empty strings */ function isNullOrEmptyString(o) { return isNullOrUndefined(o) || o === ''; } /** o is an array that is not empty (length > 0) */ function isNotEmptyArray(o) { return Array.isArray(o) && o.length > 0; } /** o is undefined, null or an empty array */ function isNullOrEmptyArray(o) { return isNullOrUndefined(o) || (Array.isArray(o) && o.length < 1); } function isString(obj) { return isType(obj, _objectTypes.String); } function isNotEmptyString(obj) { return isString(obj) && obj.length > 0; } /** true if object is a Date object */ function isDate(obj) { return !isNullOrUndefined(obj) && isFunction(obj.getTime) && !isNullOrNaN(obj.getTime()); } /** true if obj is a number or a numeric string */ function isNumeric(obj) { return !isNullOrEmptyString(obj) && !Array.isArray(obj) && //[14] will return true, since [14].toString() is "14" !isNaN(parseFloat(obj)) && isFinite(obj) && isType(Number(obj), _objectTypes.Number); } /** true if obj is a number */ function isNumber(obj) { return !isNullOrNaN(obj) && isType(obj, _objectTypes.Number); } function isNumberArray(obj) { return !isNullOrUndefined(obj) && Array.isArray(obj) && obj.every((entry) => { return isNumber(entry); }); } function isUndefined(obj) { return isType(obj, _objectTypes.Undefined); } //eslint-disable-next-line @typescript-eslint/ban-types function isFunction(obj) { return isType(obj, _objectTypes.Function); } function isBoolean(obj) { return isType(obj, _objectTypes.Boolean); } function isPromise(obj) { return obj && isFunction(obj["then"]); } /**returns true if object is string, number, date, boolean value or null*/ function isPrimitiveValue(obj) { return isNullOrUndefined(obj) || isString(obj) || isNumber(obj) || isDate(obj) || isBoolean(obj); } function isValidGuid(str) { if (isNullOrEmptyString(str)) { return false; } let a = new RegExp("^[{|\\(]?[0-9a-fA-F]{8}[-]?([0-9a-fA-F]{4}[-]?){3}[0-9a-fA-F]{12}[\\)|}]?$"); return !!a.exec(str); } exports.BoolTrueStrings = ["true", "1", "on", "yes"]; exports.BoolFalseStrings = ["false", "0", "off", "no"]; function isTrueString(str, options) { if (isNullOrEmptyString(str)) return false; else if (exports.BoolTrueStrings.includes(str.toLowerCase())) return true; else if (options && options.allowPositiveNumbers && isNumeric(str)) return Number(str) > 0; //any number greater than 0 is considered true. else return false; } function newGuid() { var S4 = () => { return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1); }; // then to call it, plus stitch in '4' in the third group return (S4() + S4() + "-" + S4() + "-4" + S4().substr(0, 3) + "-" + S4() + "-" + S4() + S4() + S4()).toLowerCase(); } /** check if object has all members (they are not undefined) */ function hasMembers(o, ...members) { if (!isNullOrUndefined(o)) { for (let i = 0; i < members.length; i++) if (isUndefined(o[members[i]])) return false; return true; //not null and all members exist } return false; } /** compares 2 versions. if v1 is bigger return 1, if v2 is bigger returns -1, if equals return 0 */ function CompareVersion(v1, v2) { let v1Split = v1 && v1.split('.').map(s => parseInt(s, 10)) || []; let v2Split = v2 && v2.split('.').map(s => parseInt(s, 10)) || []; for (let i = 0; i < Math.max(v1.length, v2.length); i++) { let p1 = v1Split[i]; let p2 = v2Split[i]; if (isNaN(p1)) p1 = -1; if (isNaN(p2)) p2 = -1; if (p1 > p2) return 1; else if (p1 < p2) return -1; } //finished while equal? return 0 return 0; } /** pass along a list of unsafe tests to get a value, the first one that doesn't throw an exception and doesnt return null will get returned */ function SafeIfElse(...list) { for (let i = 0; i < list.length; i++) { let v = null; let getter = list[i]; if (isFunction(getter)) { try { v = getter(); } catch (e) { v = null; } } else v = getter; if (!isNullOrUndefined(v)) return v; } return null; } //# sourceMappingURL=typecheckers.js.map