@kwiz/common
Version:
KWIZ common utilities and helpers for M365 platform
278 lines • 10.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SafeIfElse = exports.CompareVersion = exports.hasMembers = exports.newGuid = exports.isTrueString = exports.BoolFalseStrings = exports.BoolTrueStrings = exports.isValidGuid = exports.isPrimitiveValue = exports.isPromise = exports.isBoolean = exports.isFunction = exports.isUndefined = exports.isNumberArray = exports.isNumber = exports.isNumeric = exports.isDate = exports.isNotEmptyString = exports.isString = exports.isNullOrEmptyArray = exports.isNotEmptyArray = exports.isNullOrEmptyString = exports.isNullOrNaN = exports.isNullOrUndefined = exports.isEmptyObject = exports.isObject = exports.isType = exports.isTypeofFullNameBoolean = exports.isTypeofFullNameFunction = exports.isTypeofFullNameUndefined = exports.isTypeofFullNameNullOrUndefined = exports.isTypeofFullNameNumber = exports.isTypeofFullNameString = exports.isTypeofFullNameObject = exports.getFromFullName = exports.typeofFullName = void 0;
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) //one of the chained objects (not the leaf) is null - so return undefined
return _objectTypes.Undefined;
}
return typeof obj;
}
catch (ex) {
return _objectTypes.Undefined;
}
}
exports.typeofFullName = typeofFullName;
/** 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;
}
exports.getFromFullName = getFromFullName;
function isTypeofFullNameObject(fullName, windowOrParent) {
return typeofFullName(fullName, windowOrParent) === _objectTypes.Object;
}
exports.isTypeofFullNameObject = isTypeofFullNameObject;
function isTypeofFullNameString(fullName, windowOrParent) {
return typeofFullName(fullName, windowOrParent) === _objectTypes.String;
}
exports.isTypeofFullNameString = isTypeofFullNameString;
function isTypeofFullNameNumber(fullName, windowOrParent) {
return typeofFullName(fullName, windowOrParent) === _objectTypes.Number;
}
exports.isTypeofFullNameNumber = isTypeofFullNameNumber;
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;
}
}
exports.isTypeofFullNameNullOrUndefined = isTypeofFullNameNullOrUndefined;
function isTypeofFullNameUndefined(fullName, windowOrParent) {
return typeofFullName(fullName, windowOrParent) === _objectTypes.Undefined;
}
exports.isTypeofFullNameUndefined = isTypeofFullNameUndefined;
function isTypeofFullNameFunction(fullName, windowOrParent) {
return typeofFullName(fullName, windowOrParent) === _objectTypes.Function;
}
exports.isTypeofFullNameFunction = isTypeofFullNameFunction;
function isTypeofFullNameBoolean(fullName, windowOrParent) {
return typeofFullName(fullName, windowOrParent) === _objectTypes.Boolean;
}
exports.isTypeofFullNameBoolean = isTypeofFullNameBoolean;
function isType(obj, str) {
return typeof (obj) === str;
}
exports.isType = isType;
// eslint-disable-next-line @typescript-eslint/ban-types
function isObject(obj) {
return isType(obj, _objectTypes.Object);
}
exports.isObject = isObject;
/** 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);
}
exports.isEmptyObject = isEmptyObject;
function isNullOrUndefined(obj) {
return isUndefined(obj) || obj === null;
}
exports.isNullOrUndefined = isNullOrUndefined;
/** return true if o is undefined, null or not a number */
function isNullOrNaN(o) {
return isNullOrEmptyString(o) || isNaN(o);
}
exports.isNullOrNaN = isNullOrNaN;
/** return true if o is undefined, null or empty string */
function isNullOrEmptyString(o) {
return isNullOrUndefined(o) || o === '';
}
exports.isNullOrEmptyString = isNullOrEmptyString;
/** o is an array that is not empty (length > 0) */
function isNotEmptyArray(o) {
return Array.isArray(o) && o.length > 0;
}
exports.isNotEmptyArray = isNotEmptyArray;
/** o is undefined, null or an empty array */
function isNullOrEmptyArray(o) {
return isNullOrUndefined(o) || (Array.isArray(o) && o.length < 1);
}
exports.isNullOrEmptyArray = isNullOrEmptyArray;
function isString(obj) {
return isType(obj, _objectTypes.String);
}
exports.isString = isString;
function isNotEmptyString(obj) {
return isString(obj) && obj.length > 0;
}
exports.isNotEmptyString = isNotEmptyString;
/** true if object is a Date object */
function isDate(obj) {
return !isNullOrUndefined(obj) && isFunction(obj.getTime) && !isNullOrNaN(obj.getTime());
}
exports.isDate = isDate;
/** 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);
}
exports.isNumeric = isNumeric;
/** true if obj is a number */
function isNumber(obj) {
return !isNullOrNaN(obj) && isType(obj, _objectTypes.Number);
}
exports.isNumber = isNumber;
function isNumberArray(obj) {
return !isNullOrUndefined(obj) && Array.isArray(obj) && obj.every((entry) => {
return isNumber(entry);
});
}
exports.isNumberArray = isNumberArray;
function isUndefined(obj) {
return isType(obj, _objectTypes.Undefined);
}
exports.isUndefined = isUndefined;
//eslint-disable-next-line @typescript-eslint/ban-types
function isFunction(obj) {
return isType(obj, _objectTypes.Function);
}
exports.isFunction = isFunction;
function isBoolean(obj) {
return isType(obj, _objectTypes.Boolean);
}
exports.isBoolean = isBoolean;
function isPromise(obj) {
return obj && isFunction(obj["then"]);
}
exports.isPromise = isPromise;
/**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);
}
exports.isPrimitiveValue = isPrimitiveValue;
function isValidGuid(str) {
var a = new RegExp("^[{|\\(]?[0-9a-fA-F]{8}[-]?([0-9a-fA-F]{4}[-]?){3}[0-9a-fA-F]{12}[\\)|}]?$");
return !!a.exec(str);
}
exports.isValidGuid = isValidGuid;
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;
}
exports.isTrueString = isTrueString;
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();
}
exports.newGuid = newGuid;
/** 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;
}
exports.hasMembers = hasMembers;
/** 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;
}
exports.CompareVersion = CompareVersion;
/** 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;
}
exports.SafeIfElse = SafeIfElse;
//# sourceMappingURL=typecheckers.js.map