Sha256: 2c4eebe646142ca0366ccbc9446cf8cd3b253cef49caa466d78fbd4ac8d93604

Contents?: true

Size: 852 Bytes

Versions: 16

Compression:

Stored size: 852 Bytes

Contents

const mongoose = require('mongoose/browser');
const cql = require('cql-execution');

function QDMDate(key, options) {
  mongoose.SchemaType.call(this, key, options, 'Date');
}
QDMDate.prototype = Object.create(mongoose.SchemaType.prototype);

QDMDate.prototype.cast = (date) => {
  if (date == null) {
    return date;
  }

  // Already a CQL Date
  if (date.isDate) {
    return date;
  }

  // Object
  if (typeof date === 'object') {
    const keys = Object.keys(date);
    if (keys.includes('year') && keys.includes('month') && keys.includes('day')) {
      return new cql.Date(date.year, date.month, date.day);
    }
  }

  // Date String
  if (!cql.Date.parse(date)) {
    throw new Error(`Date: ${date} is not a valid Date`);
  } else {
    return cql.Date.parse(date);
  }
};

mongoose.Schema.Types.QDMDate = QDMDate;
module.exports = QDMDate;

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
cqm-models-4.2.0 app/assets/javascripts/basetypes/QDMDate.js
cqm-models-3.1.2 app/assets/javascripts/basetypes/QDMDate.js
cqm-models-4.1.1 app/assets/javascripts/basetypes/QDMDate.js
cqm-models-4.1.0 app/assets/javascripts/basetypes/QDMDate.js
cqm-models-4.0.2 app/assets/javascripts/basetypes/QDMDate.js
cqm-models-4.0.1 app/assets/javascripts/basetypes/QDMDate.js
cqm-models-4.0.0 app/assets/javascripts/basetypes/QDMDate.js
cqm-models-3.1.1 app/assets/javascripts/basetypes/QDMDate.js
cqm-models-3.1.0 app/assets/javascripts/basetypes/QDMDate.js
cqm-models-3.0.6 app/assets/javascripts/basetypes/QDMDate.js
cqm-models-3.0.5 app/assets/javascripts/basetypes/QDMDate.js
cqm-models-3.0.4 app/assets/javascripts/basetypes/QDMDate.js
cqm-models-3.0.3 app/assets/javascripts/basetypes/QDMDate.js
cqm-models-3.0.2 app/assets/javascripts/basetypes/QDMDate.js
cqm-models-3.0.1 app/assets/javascripts/basetypes/QDMDate.js
cqm-models-3.0.0 app/assets/javascripts/basetypes/QDMDate.js