65 lines
2.2 KiB
TypeScript
65 lines
2.2 KiB
TypeScript
import _ from 'lodash';
|
|
import { UtilStringHelper } from './Utils.string';
|
|
|
|
const StringHelper = new UtilStringHelper();
|
|
|
|
export const interpolate = StringHelper.interpolate;
|
|
|
|
export const sleep = async (ms: number): Promise<void> => {
|
|
return new Promise((resolve) => {
|
|
setTimeout(resolve, ms);
|
|
});
|
|
};
|
|
|
|
export const promiseAll = async (promises: Promise<any>[]): Promise<any> => {
|
|
const results = await Promise.allSettled(promises);
|
|
|
|
results.forEach((element) => {
|
|
if (element.status == 'rejected') console.error('[ASYNC - ERROR]', element.reason);
|
|
});
|
|
|
|
console.log('[PROMISE ALL - DONE]');
|
|
|
|
return results;
|
|
};
|
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
const isNullBlankOrUndefined = function(o) {
|
|
return typeof o === 'undefined' || o == null || o === '';
|
|
};
|
|
/**
|
|
* Deep diff between two object, using lodash
|
|
* @param {Object} object Object compared
|
|
* @param {Object} base Object to compare with
|
|
* @return {Object} Return a new object who represent the diff
|
|
*/
|
|
export const differenceBetweenObject = (object, base, ignoreBlanks = false): any => {
|
|
if (!_.isObject(object) || _.isDate(object) || Array.isArray(object)) return object; // special case dates & array
|
|
return _.transform(object as any, (result, value, key) => {
|
|
if (!_.isEqual(value, base[key])) {
|
|
if (ignoreBlanks && isNullBlankOrUndefined(value) && isNullBlankOrUndefined(base[key])) return;
|
|
result[key] = _.isObject(value) && _.isObject(base[key]) ? differenceBetweenObject(value, base[key]) : value;
|
|
}
|
|
});
|
|
};
|
|
|
|
export const generateCodeNumber = () => {
|
|
return Math.floor(100000 + Math.random() * 900000);
|
|
};
|
|
|
|
export const clog = (data: any): void => {
|
|
console.log(JSON.stringify(data, null, 2));
|
|
};
|
|
|
|
export const nullToUndefined = (value: any): any | undefined => {
|
|
if (_.isString(value) || _.isBoolean(value) || _.isDate(value) || _.isFinite(value) || _.isInteger(value)) return value;
|
|
|
|
if (_.isArray(value)) return (value as any[]).map(nullToUndefined);
|
|
|
|
if (_.isObject(value)) return _.mapValues(value as any, nullToUndefined);
|
|
|
|
if (value === null) return undefined;
|
|
|
|
return value;
|
|
};
|