UNPKG

firebase-dates-util

Version:

A util package to transform dates string, numbers or Date Objects to firebase Timestamp and viceversa

182 lines (181 loc) 7.41 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; var _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.Dates = void 0; const date_fns_1 = require("date-fns"); const locale_1 = require("date-fns/locale"); const firestore_1 = require("firebase/firestore"); const DEFAULT_FIELDS_1 = __importDefault(require("../DEFAULT_FIELDS")); class Dates { static errorLog(functionName, message, ...rest) { console.error(Object.assign({ message, functionName }, rest)); } static toTimestamp(date) { const _date = this.toDate(date); if (_date) return firestore_1.Timestamp.fromDate(_date); this.errorLog('toTimestamp', 'invalid date', date); return null; } static toMiliseconds(date) { const _date = this.toDate(date); if (_date) return _date.getTime(); console.error('invalid date'); return null; } static toFieldDate(date) { const _date = this.toDate(date); if (_date) return this.format(_date, 'yyyy-MM-dd'); this.errorLog('toFieldDate', 'invalid date', date); return null; } static transformDateTo(date, target, options = { avoidUndefined: false }) { const _date = this.toDate(date); if (_date) { const result = { fieldDate: (d) => this.format(d, 'yyyy-MM-dd'), timestamp: (d) => firestore_1.Timestamp.fromDate(d), date: (d) => d, number: (d) => d.getTime(), }; return result[target](_date); } else { return options.avoidUndefined ? null : _date; } } static formatObjectDates(object, target, options) { const auxObj = Object.assign({}, object); const customFields = (options === null || options === void 0 ? void 0 : options.includeFields) || []; const ignoreDefaultDateTypeField = !!(options === null || options === void 0 ? void 0 : options.ignoreDefaultDateTypeField); const ignoreDefaultTimestampTypeField = !!(options === null || options === void 0 ? void 0 : options.ignoreDefaultTimestampTypeField); /** * This function should return the same object , transforming the date fields specificated in FIELDS in the target format. * also will find and change the dates formats Date and Timestamp(firebase) and transform to the target format */ Object.keys(auxObj).forEach((key) => { const objProperty = auxObj[key]; const FIELDS = [...this.DATE_FIELDS, ...customFields].filter((field) => { var _b; return !((_b = options === null || options === void 0 ? void 0 : options.avoidFields) === null || _b === void 0 ? void 0 : _b.includes(field)); }); if (FIELDS.includes(key)) { // @ts-ignore auxObj[key] = this.transformDateTo(objProperty, target, options); } else if (objProperty instanceof Date && !ignoreDefaultDateTypeField) { // TODO configure allow or reject transform if instance of Date // @ts-ignore auxObj[key] = this.transformDateTo(objProperty, target, options); } else if (objProperty instanceof firestore_1.Timestamp && !ignoreDefaultTimestampTypeField) { // TODO configure allow or reject transform if instance of Timestamp // @ts-ignore auxObj[key] = this.transformDateTo(objProperty, target, options); } else { // If field does´t match any below options return de original value // @ts-ignore auxObj[key] = object[key]; } }); return auxObj; } static isLiteralObject(a) { return !!a && a.constructor === Object; } static formatComplexObjectDates(object, target, options) { return this.deepFormatObjectDates(object, target, options); } static deepFormatObjectDates(object, target = 'number', options) { const auxObj = Object.assign({}, this.formatObjectDates(object, target, options)); for (const key in object) { if (Object.prototype.hasOwnProperty.call(object, key)) { // @ts-ignore const element = object[key]; if (Array.isArray(element)) { // @ts-ignore auxObj[key] = element.map((item) => this.isLiteralObject(item) ? this.deepFormatObjectDates(item, target, options) : item); } else if (this.isLiteralObject(element)) { // @ts-ignore auxObj[key] = this.deepFormatObjectDates(element, target, options); } } } // console.log(auxObj) return auxObj; } } exports.Dates = Dates; _a = Dates; Dates.toDate = (date) => { const typeOf = (element) => { const isLiteralObject = (a) => { return !!a && a.constructor === Object; }; if (element === null) return 'null'; if (element === undefined) return 'undefined'; if (element === null || element === void 0 ? void 0 : element.toDate) return 'timestamp'; if (element instanceof firestore_1.Timestamp) return 'timestamp'; if (Array.isArray(element)) return 'array'; if (isLiteralObject(element)) return 'literalObject'; if (typeof element === 'number') return 'number'; if (typeof element === 'string') return 'string'; if (typeof element === 'function') return 'function'; if (typeof element === 'symbol') return 'symbol'; if (element instanceof Date) return 'date'; if (typeof element === 'object') return 'object'; return 'undefined'; }; const result = { date: () => date, // @ts-ignore timestamp: () => date === null || date === void 0 ? void 0 : date.toDate(), // @ts-ignore number: () => new Date(date), // @ts-ignore string: () => { var _b; return (((_b = new Date(date)) === null || _b === void 0 ? void 0 : _b.getTime()) ? new Date(date) : date); }, null: () => date, array: () => date, literalObject: () => date, function: () => date, symbol: () => date, object: () => date, undefined: () => date, }; return result[typeOf(date)](); }; Dates.format = (date, stringFormat = 'dd/MM/yy') => { const _date = _a.toDate(date); if (_date) return (0, date_fns_1.format)(new Date(_date.setMinutes(_date.getMinutes() + _date.getTimezoneOffset())), stringFormat, { locale: locale_1.es, }); _a.errorLog('format', 'invalid date', date); return 'isNaD'; }; Dates.fromNow = (date) => { if (date && (0, date_fns_1.isValid)(date)) return (0, date_fns_1.formatDistanceToNowStrict)(new Date(date), { locale: locale_1.es, roundingMethod: 'floor', addSuffix: true, }); return 'isNaD'; }; Dates.DATE_FIELDS = DEFAULT_FIELDS_1.default;