UNPKG

@bjesuiter/serializr-helpers

Version:

Provides some helper functions and serialization PropertySchemas for mobxjs/serializr - library

93 lines (92 loc) 4.17 kB
/** * This serializr PropertySchema can be used with @serializable(MomentSerializationSchema) * to make moment.js Moment objects serializable * @type {PropSchema} */ import { custom, SKIP } from 'serializr'; import moment from 'moment'; function buildSerializer(valueIfUndefined, useUtc, serializationFormat) { if (useUtc === void 0) { useUtc = false; } if (serializationFormat === void 0) { serializationFormat = 'ISO'; } return function (value) { //value.format is used here to output a datetime with attached offset to utc //value.toJson would normalize the output to utc, // which would make it impossible to reconstruct the original timezone // intended use of == vs. === to include null when checking for undefined if (value == undefined) { // logger.debug('Moment object will be skipped in serialization - object is undefined'); return (valueIfUndefined) ? valueIfUndefined : SKIP; } if (serializationFormat === 'ISO') { // see documentation for toISO String for keepOffset explanation: // https://momentjs.com/docs/#/displaying/as-iso-string/ return (useUtc) ? value.toISOString(true) : value.toISOString(); } value = (useUtc) ? value.utc() : value; return value.format(serializationFormat); }; } function buildDeserializer(useUtc) { return function (jsonValue) { return (useUtc) ? moment.utc(jsonValue) : moment(jsonValue); }; } /** * This is the default serialization schema. * It skips undefined moment objects and uses local time deserialization with `moment()` instead of moment.utc(); */ export var MomentIsoSerialization = custom(buildSerializer(), buildDeserializer()); var MomentSerializationSchema = /** @class */ (function () { function MomentSerializationSchema() { return MomentIsoSerialization; } return MomentSerializationSchema; }()); export { MomentSerializationSchema }; (function (MomentSerializationSchema) { var Builder = /** @class */ (function () { function Builder() { this.useUtcFlag = false; this.serializationFormat = 'ISO'; } /** * This value will be returned, if the moment value is undefined, which should have been serialized, * It must be a JSON compatible value. * This means, it must be of one type of: string | boolean | number | object | []. * If this is not set, this moment value will be skipped in serialization. * * Applies only to serialization. * @param value */ Builder.prototype.useValueIfUndefined = function (value) { this.valueIfUndefined = value; return this; }; /** * Uses moment.utc() instead of moment(), which parses the variable as utc * and does not convert the time into local time of the running node instance. * * Applies to serialization & deserialization */ Builder.prototype.useUtc = function () { this.useUtcFlag = true; return this; }; /** * * @param string format which is used to serialize moment objects into * @default complete RFC-3339 format, which is equal to ISO-8601, for example: 2009-01-01T12:00:00+01:00 * would be written as YYYY-MM-DDTHH:mm:ss.SSSZZ. In contrast to moment js default, * this default keeps the offset in serialization. This breaks the compatibility with javascript Date, * but if it's not needed, this is the more complete option. */ Builder.prototype.useSerializationFormat = function (format) { this.serializationFormat = format; }; Builder.prototype.build = function () { return custom(buildSerializer(this.valueIfUndefined, this.useUtcFlag, this.serializationFormat), buildDeserializer(this.useUtcFlag)); }; return Builder; }()); MomentSerializationSchema.Builder = Builder; })(MomentSerializationSchema || (MomentSerializationSchema = {}));