func/date.js

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ISODateStringToObject = ISODateStringToObject;
exports.isNullDate = isNullDate;
var _isNil2 = _interopRequireDefault(require("lodash/isNil"));
var _has2 = _interopRequireDefault(require("lodash/has"));
var _isPlainObject2 = _interopRequireDefault(require("lodash/isPlainObject"));
var _isString2 = _interopRequireDefault(require("lodash/isString"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/*
 * Copyright (C) 2019  Nicolas Pelletier
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/**
 * Parse an ISO 8601-2004 string and return an object with separate day, month and year, if they exist.
 * If any of the values don't exist, the default is an empty string.
 * @function ISODateStringToObject
 * @param {string} value - relationshipId number for initaial relationship
 * @returns {object} a {day, month, year} object
 */
function ISODateStringToObject(value) {
  if (!(0, _isString2.default)(value)) {
    if ((0, _isPlainObject2.default)(value) && (0, _has2.default)(value, 'year')) {
      return value;
    }
    return {
      day: '',
      month: '',
      year: ''
    };
  }
  const date = value ? value.split('-') : [];
  // A leading minus sign denotes a BC date
  // This creates an empty first array item that needs to be removed,
  // and requires us to add the negative sign back for the year
  if (date.length && date[0] === '') {
    date.shift();
    date[0] = (-parseInt(date[0], 10)).toString();
  }
  return {
    day: date.length > 2 ? date[2] : '',
    month: date.length > 1 ? date[1] : '',
    year: date.length > 0 ? date[0] : ''
  };
}

/**
 * Determines wether a given date is empty or null, meaning no year month or day has been specified.
 * Accepts a {day, month, year} object or an ISO 8601-2004 string (±YYYYYY-MM-DD)
 * @function isNullDate
 * @param {object|string} date - a {day, month, year} object or ISO 8601-2004 string (±YYYYYY-MM-DD)
 * @returns {boolean} true if the date is empty/null
 */
function isNullDate(date) {
  const dateObject = ISODateStringToObject(date);
  const isNullYear = (0, _isNil2.default)(dateObject.year) || dateObject.year === '';
  const isNullMonth = (0, _isNil2.default)(dateObject.month) || dateObject.month === '';
  const isNullDay = (0, _isNil2.default)(dateObject.day) || dateObject.day === '';
  return isNullYear && isNullMonth && isNullDay;
}