UNPKG

local-date

Version:

Replacement of Date for dealing with dates independent of time or timezone.

124 lines (103 loc) 5.92 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _util = require('./util'); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } function _extendableBuiltin(cls) { function ExtendableBuiltin() { var instance = Reflect.construct(cls, Array.from(arguments)); Object.setPrototypeOf(instance, Object.getPrototypeOf(this)); return instance; } ExtendableBuiltin.prototype = Object.create(cls.prototype, { constructor: { value: cls, enumerable: false, writable: true, configurable: true } }); if (Object.setPrototypeOf) { Object.setPrototypeOf(ExtendableBuiltin, cls); } else { ExtendableBuiltin.__proto__ = cls; } return ExtendableBuiltin; } function warn(message) { if (process.env.NODE_ENV !== 'production') { console.warn(message); // eslint-disable-line no-console } } var LocalDate = function (_extendableBuiltin2) { _inherits(LocalDate, _extendableBuiltin2); _createClass(LocalDate, null, [{ key: 'test', value: function test(isoDate) { return LocalDate.ISO_DATE_FORMAT.test(isoDate); } }]); function LocalDate(value) { _classCallCheck(this, LocalDate); if (typeof value === 'undefined') { var now = new Date(); var _this = _possibleConstructorReturn(this, (LocalDate.__proto__ || Object.getPrototypeOf(LocalDate)).call(this, now.getFullYear(), now.getMonth(), now.getDate())); } else if (value instanceof LocalDate) { var _this = _possibleConstructorReturn(this, (LocalDate.__proto__ || Object.getPrototypeOf(LocalDate)).call(this, value.getFullYear(), value.getMonth(), value.getDate())); } else if (typeof value === 'string' && LocalDate.ISO_DATE_FORMAT.test(value)) { var _LocalDate$ISO_DATE_F = LocalDate.ISO_DATE_FORMAT.exec(value).slice(1).map(function (s) { return parseInt(s, 10); }), _LocalDate$ISO_DATE_F2 = _slicedToArray(_LocalDate$ISO_DATE_F, 3), year = _LocalDate$ISO_DATE_F2[0], month = _LocalDate$ISO_DATE_F2[1], date = _LocalDate$ISO_DATE_F2[2]; var _this = _possibleConstructorReturn(this, (LocalDate.__proto__ || Object.getPrototypeOf(LocalDate)).call(this, year, month - 1, date, 0, 0, 0, 0)); } else { if (process.env.NODE_ENV !== 'production') { throw new Error('Invalid date supplied. Please specify an ISO date string (YYYY-MM-DD) or a LocalDate object.\nhttps://github.com/buildo/local-date#parser'); // eslint-disable-line max-len } else { throw new Error('Invalid date supplied'); } } return _possibleConstructorReturn(_this); } _createClass(LocalDate, [{ key: 'toISOString', value: function toISOString() { return [this.getFullYear(), (0, _util.pad2)(this.getMonth() + 1), (0, _util.pad2)(this.getDate())].join('-'); } }, { key: 'getHours', value: function getHours() { warn('You shouldn\'t use LocalDate.getHours as LocalDate is time agnostic.'); return Date.prototype.getHours.call(this); } }, { key: 'getMinutes', value: function getMinutes() { warn('You shouldn\'t use LocalDate.getMinutes as LocalDate is time agnostic.'); return Date.prototype.getMinutes.call(this); } }, { key: 'getSeconds', value: function getSeconds() { warn('You shouldn\'t use LocalDate.getSeconds as LocalDate is time agnostic.'); return Date.prototype.getSeconds.call(this); } }, { key: 'getMilliseconds', value: function getMilliseconds() { warn('You shouldn\'t use LocalDate.getMilliseconds as LocalDate is time agnostic.'); return Date.prototype.getMilliseconds.call(this); } }]); return LocalDate; }(_extendableBuiltin(Date)); LocalDate.ISO_DATE_FORMAT = /^(\d{4})-(\d{2})-(\d{2})$/; exports.default = LocalDate;