UNPKG

@benshi.ai/js-sdk

Version:

Benshi SDK

65 lines 2.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.toISOLocal = exports.offsetToISO8601 = exports.isISOLocal = exports.hasRepeatedIds = exports.checkObjectFieldEmptyiness = void 0; var offsetToISO8601 = function (offset) { var zeroize = function (number) { return number < 10 ? "0".concat(number) : "".concat(number); }; var sign = offset > 0 ? '-' : '+'; var hours = zeroize(Math.floor(Math.abs(offset) / 60)); var minutes = zeroize(Math.abs(offset) % 60); return "".concat(sign).concat(hours).concat(minutes); }; exports.offsetToISO8601 = offsetToISO8601; var toISOLocal = function (d) { // format date as: 2022-07-04T16:50:28.334+02:00 var z = function (n) { return ('0' + n).slice(-2); }; var zz = function (n) { return ('00' + n).slice(-3); }; var off = d.getTimezoneOffset(); var sign = off > 0 ? '-' : '+'; off = Math.abs(off); return d.getFullYear() + '-' + z(d.getMonth() + 1) + '-' + z(d.getDate()) + 'T' + z(d.getHours()) + ':' + z(d.getMinutes()) + ':' + z(d.getSeconds()) + '.' + zz(d.getMilliseconds()) + sign + z(off / 60 | 0) + ':' + z(off % 60); }; exports.toISOLocal = toISOLocal; /** * Checks whether the given date matches the format * * 1937-01-01T12:00:27.87+00:20 * @param timestamp */ var isISOLocal = function (timestamp) { var referenceDate = "1937-01-01T12:00:27.87+00:20"; // millis may have two or three digits var correctLength = timestamp.length === referenceDate.length || timestamp.length === referenceDate.length + 1; var isString = typeof timestamp === 'string'; var isValidGeneralDate = !isNaN(new Date(timestamp).getTime()); return correctLength && isString && isValidGeneralDate; }; exports.isISOLocal = isISOLocal; var hasRepeatedIds = function (collection) { var ids = {}; for (var _i = 0, collection_1 = collection; _i < collection_1.length; _i++) { var id = collection_1[_i].id; if (ids[id]) { return true; } ids[id] = true; } return false; }; exports.hasRepeatedIds = hasRepeatedIds; var checkObjectFieldEmptyiness = function (obj, fieldsToCheck) { for (var _i = 0, fieldsToCheck_1 = fieldsToCheck; _i < fieldsToCheck_1.length; _i++) { var field = fieldsToCheck_1[_i]; if ((typeof obj[field] === 'string') && (obj[field].trim().length === 0)) { throw new Error("non empty ".concat(field, " are mandatory")); } } }; exports.checkObjectFieldEmptyiness = checkObjectFieldEmptyiness; //# sourceMappingURL=utils.js.map