UNPKG

@rschedule/luxon-date-adapter

Version:

An rSchedule DateAdapter for "luxon" DateTime objects.

221 lines (210 loc) 7.53 kB
(function(global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@rschedule/core'), require('luxon')) : typeof define === 'function' && define.amd ? define(['exports', '@rschedule/core', 'luxon'], factory) : ((global = global || self), factory((global.rScheduleLuxonDateAdapter = {}), global.rSchedule, global.luxon)); })(this, function(exports, core, luxon) { 'use strict'; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. See the Apache Version 2.0 License for specific language governing permissions and limitations under the License. ***************************************************************************** */ /* global Reflect, Promise */ var extendStatics = function(d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function(d, b) { d.__proto__ = b; }) || function(d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; function __extends(d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : ((__.prototype = b.prototype), new __()); } function __read(o, n) { var m = typeof Symbol === 'function' && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i['return'])) m.call(i); } finally { if (e) throw e.error; } } return ar; } function __spread() { for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); return ar; } /** * The `LuxonDateAdapter` is a DateAdapter for `luxon` DateTime * objects. * * It supports timezone handling in so far as luxon supports * timezone handling. Note: that, if able, luxon always adds * a timezone to a DateTime (i.e. timezone may never be undefined). * * At the moment, that means that serializing to/from iCal will * always apply a specific timezone (which may or may not be what * you want). If this is a problem for you, you can try opening * an issue in the rSchedule monorepo. */ var LuxonDateAdapter = /** @class */ (function(_super) { __extends(LuxonDateAdapter, _super); function LuxonDateAdapter(date, options) { if (options === void 0) { options = {}; } var _this = _super.call(this, undefined, options) || this; _this.date = date; _this.timezone = date.zone instanceof luxon.LocalZone ? null : date.zoneName; if (options.metadata) { Object.assign(_this.metadata, options.metadata); } _this.assertIsValid(); return _this; } /** * Checks if object is an instance of `LuxonDateTime` */ LuxonDateAdapter.isDate = function(object) { return luxon.DateTime.isDateTime(object); }; LuxonDateAdapter.fromDate = function(date, options) { return new LuxonDateAdapter(date, options); }; LuxonDateAdapter.fromJSON = function(json) { var zone = json.timezone === null ? 'local' : json.timezone === 'UTC' ? 'utc' : json.timezone; var date = new LuxonDateAdapter( luxon.DateTime.fromObject({ zone: zone, year: json.year, month: json.month, day: json.day, hour: json.hour, minute: json.minute, second: json.second, millisecond: json.millisecond, }), { duration: json.duration }, ); if (json.metadata) { Object.assign(date.metadata, json.metadata); } return date; }; /** * ### Important!!! * * This method expects an *rSchedule* `DateTime` object which bares * *no relation* to a luxon `DateTime` object. This method is largely * meant for private, internal rSchedule use. * * @param datetime rSchedule DateTime object */ LuxonDateAdapter.fromDateTime = function(datetime) { var _a; var date = LuxonDateAdapter.fromJSON(datetime.toJSON()); (_a = date.generators).push.apply(_a, __spread(datetime.generators)); if (datetime.metadata) { Object.assign(date.metadata, datetime.metadata); } return date; }; Object.defineProperty(LuxonDateAdapter.prototype, 'end', { get: function() { if (!this.duration) return; if (this._end) return this._end; this._end = LuxonDateAdapter.fromDateTime( this.toDateTime().add(this.duration, 'millisecond'), ).date; return this._end; }, enumerable: false, configurable: true, }); LuxonDateAdapter.prototype.set = function(prop, value) { if (prop === 'timezone') { if (this.timezone === value) return this; else if (value === null) { return new LuxonDateAdapter(this.date.toLocal(), { duration: this.duration, generators: this.generators, }); } else { return new LuxonDateAdapter(this.date.setZone(value), { duration: this.duration, generators: this.generators, }); } } else if (prop === 'duration') { if (this.duration === value) return this; else { return new LuxonDateAdapter(this.date, { duration: value, generators: this.generators, }); } } throw new core.ArgumentError('Unknown prop "' + prop + '" for LuxonDateAdapter#set()'); }; LuxonDateAdapter.prototype.valueOf = function() { return this.date.valueOf(); }; LuxonDateAdapter.prototype.toJSON = function() { var json = { timezone: this.timezone, year: this.date.get('year'), month: this.date.get('month'), day: this.date.get('day'), hour: this.date.get('hour'), minute: this.date.get('minute'), second: this.date.get('second'), millisecond: this.date.get('millisecond'), }; if (this.duration) { json.duration = this.duration; } return json; }; LuxonDateAdapter.prototype.assertIsValid = function() { if (!this.date.isValid) { throw new core.InvalidDateAdapterError(); } else if (this.duration && this.duration <= 0) { throw new core.InvalidDateAdapterError('If provided, duration must be greater than 0.'); } return true; }; LuxonDateAdapter.hasTimezoneSupport = true; return LuxonDateAdapter; })(core.DateAdapterBase); exports.LuxonDateAdapter = LuxonDateAdapter; Object.defineProperty(exports, '__esModule', { value: true }); });