colouring-montreal/app/src/helpers.ts

37 lines
1.1 KiB
TypeScript
Raw Normal View History

2019-09-09 10:17:44 -04:00
/**
* A function to be passed to JSON.parse as a JSON reviver, in order to transform date values
* (which don't have a native JSON representation and therefore are serialized as strings)
* back to a JavaScript Date object.
* This works by first checking if a string value complies with a date format
* and then converting to a Date if and only if that's the case
* @param name name of the JSON field to revive
* @param value value of the JSON field to revive
*/
2019-08-30 08:45:55 -04:00
export function dateReviver(name, value) {
if (typeof value === "string" && /^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d.\d\d\dZ$/.test(value)) {
return new Date(value);
}
return value;
}
2019-11-26 07:09:27 -05:00
export function parseJsonOrDefault(jsonString: string) {
try {
return JSON.parse(jsonString);
} catch(error) {
console.error(error);
return null;
}
}
export function hasAnyOwnProperty(obj: {}, keys: string[]) {
return keys.some(k => obj.hasOwnProperty(k));
}
2019-12-03 12:30:51 -05:00
export function isNullishOrEmpty(obj: any) {
return obj == undefined || isEmptyArray(obj);
}
export function isEmptyArray(obj: any) {
return Array.isArray(obj) && obj.length === 0;
}