projects/app-base-library/src/lib/shared/util/util.ts
Methods |
|
Static castToType | ||||
castToType(o: )
|
||||
Parameters :
Returns :
any
|
Static copy | ||||
copy(obj: )
|
||||
Parameters :
Returns :
any
|
Static deepAssign | |||||||||
deepAssign(target: , ...sources: any[])
|
|||||||||
Parameters :
Returns :
any
|
Static getStyle | ||||||
getStyle(el: , styleProp: )
|
||||||
Parameters :
Returns :
any
|
Static getTimeStamp | ||||||||||
getTimeStamp(date: any)
|
||||||||||
Returns the current date as seconds since midnight Jan 1, 1970.
Parameters :
Returns :
any
|
Static getUUID |
getUUID()
|
Returns a UUID.
Returns :
any
|
Static isBrowser |
isBrowser()
|
Checks if the application is running in a browser.
Returns :
boolean
|
Static isMergebleObject | ||||
isMergebleObject(item: )
|
||||
Parameters :
Returns :
boolean
|
Static isObject | ||||||
isObject(item: any)
|
||||||
Parameters :
Returns :
boolean
|
Static isOnline |
isOnline()
|
Checks if the application is running in a browser.
Returns :
boolean
|
Static viewportSize |
viewportSize()
|
Returns the size of the viewport.
Returns :
any
|
export class Util {
static copy(obj) {
return JSON.parse(JSON.stringify(obj));
}
static deepAssign(target, ...sources) {
if (!sources.length) {
return target;
}
const source = sources.shift();
if (source === undefined) {
return target;
}
if (Util.isMergebleObject(target) && Util.isMergebleObject(source)) {
Object.keys(source).forEach(function(key: string) {
if (Util.isMergebleObject(source[key])) {
if (!target[key]) {
target[key] = {};
}
Util.deepAssign(target[key], source[key]);
} else {
target[key] = source[key];
}
});
}
return Util.deepAssign(target, ...sources);
};
static isObject(item: any) {
return item !== null && typeof item === 'object';
}
static isMergebleObject(item) {
return Util.isObject(item) && !Array.isArray(item);
}
static castToType(o) {
let value = o;
if (Number(value) && value !== "true" && value !== "false") {value = Number(value);}
if (value === "0") {value = 0;}
if (value === "true") {value = true;}
if (value === "false") {value = false;}
return value;
};
/**
* Returns the current date as seconds since midnight Jan 1, 1970.
* @method Util.getTimeStamp
* @param date optional: A string or Date object to return as seconds since midnight Jan 1, 1970.
* @returns
*/
static getTimeStamp(date: any = null) {
if (date) {
if (typeof date === 'string') {
return Math.round(+new Date(date) / 1000);
} else {
return Math.round(+date / 1000);
}
} else {
return Math.round(+new Date() / 1000);
}
}
/**
* Returns a UUID.
* @method Util.getUUID
* @returns
*/
static getUUID() {
let d = new Date().getTime();
const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
const r = (d + Math.random() * 16) % 16 | 0;
d = Math.floor(d / 16);
return (c === 'x' ? r : (r & 0x7 | 0x8)).toString(16);
});
return uuid;
}
/**
* Checks if the application is running in a browser.
* @method Util.isOnline
* @returns
*/
static isOnline() {
if (typeof window !== 'undefined' && window.navigator.onLine) {
return true;
} else {
return false;
}
}
/**
* Returns the size of the viewport.
* @method Util.viewportSize
* @returns
*/
static viewportSize() {
let obj: any = {};
if (typeof window !== 'undefined') {
if (window.innerHeight) {
obj.width = window.innerWidth;
obj.height = window.innerHeight;
} else {
obj.width = document.documentElement.clientWidth;
obj.height = document.documentElement.clientHeight;
}
}
// todo: native + nodejs
if (typeof window === 'undefined') { }
return obj;
}
/**
* Checks if the application is running in a browser.
* @method Util.isBrowser
* @returns
*/
static isBrowser() {
return typeof window !== 'undefined';
}
static getStyle(el, styleProp) {
let value;
const defaultView = el.ownerDocument.defaultView;
// W3C standard way:
if (defaultView && defaultView.getComputedStyle) {
// sanitize property name to css notation (hypen separated words eg. font-Size)
styleProp = styleProp.replace(/([A-Z])/g, '-$1').toLowerCase();
return defaultView.getComputedStyle(el, null).getPropertyValue(styleProp);
} else if (el.currentStyle) { // IE
// sanitize property name to camelCase
styleProp = styleProp.replace(/\-(\w)/g, function(str, letter) {
return letter.toUpperCase();
});
value = el.currentStyle[styleProp];
// convert other units to pixels on IE
if (/^\d+(em|pt|%|ex)?$/i.test(value)) {
return (function(value) {
const oldLeft = el.style.left, oldRsLeft = el.runtimeStyle.left;
el.runtimeStyle.left = el.currentStyle.left;
el.style.left = value || 0;
value = el.style.pixelLeft + 'px';
el.style.left = oldLeft;
el.runtimeStyle.left = oldRsLeft;
return value;
})(value);
}
return value;
}
}
}