tickethead-sdk
Version:
SDK for the Tickethead API
93 lines • 2.96 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.toSnakeCase = toSnakeCase;
exports.toCamelCase = toCamelCase;
exports.stringifyDates = stringifyDates;
exports.parseDates = parseDates;
exports.isDateFormatted = isDateFormatted;
const object_case_converter_1 = require("@ridi/object-case-converter");
function toSnakeCase(payload) {
return (0, object_case_converter_1.decamelize)(payload, {
recursive: true,
exception: {
street1: 'street1',
street2: 'street2',
statusCode: 'statusCode',
privateKey: 'privateKey', // TODO: Remove this when bc service schema is fixed
},
});
}
function toCamelCase(payload) {
return (0, object_case_converter_1.camelize)(payload, {
recursive: { excludes: ['permissions'] }, // Getting the users permissions contains org names as keys, so we don't want to convert those to snake case
});
}
function isValidDate(date) {
return (date &&
Object.prototype.toString.call(date) === '[object Date]' &&
!isNaN(date));
}
/**
* Converts all `Date` objects to the ISO8601 formatted strings
*/
function stringifyDates(obj) {
if (obj == null)
return undefined;
if (typeof obj !== 'object')
return obj;
if (Array.isArray(obj)) {
return obj.map((val) => stringifyDates(val));
}
const result = {};
Object.entries(obj !== null && obj !== void 0 ? obj : {}).forEach(([key, val]) => {
if (isValidDate(val)) {
result[key] = val.toISOString();
}
else if (val !== null && typeof val === 'object') {
result[key] = stringifyDates(val);
}
else {
result[key] = val;
}
});
return result;
}
/**
* Converts an object's string dates to `Date` objects
*/
function parseDates(obj) {
const res = recursiveDateParser(obj);
return res;
}
function recursiveDateParser(obj) {
if (obj == null)
return undefined;
// Don't process primitives
if (typeof obj !== 'object')
return obj;
// Handle arrays
if (Array.isArray(obj)) {
return obj.map((val) => recursiveDateParser(val));
}
const result = {};
Object.entries(obj !== null && obj !== void 0 ? obj : {}).forEach(([key, val]) => {
if (val !== null && typeof val === 'object') {
// Recursively parse sub-objects
result[key] = recursiveDateParser(val);
}
else if (typeof val === 'string' &&
isDateFormatted(val) &&
isValidDate(new Date(val))) {
// If the string is a valid date, parse it
result[key] = new Date(val);
}
else {
result[key] = val;
}
});
return result;
}
function isDateFormatted(str) {
return str.match(/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(.(\d{3}))?/);
}
//# sourceMappingURL=converter.js.map