Sha256: 4e3e404828aa6db6c20c4b3505bf9a953adc9366c24f8c00f11e0b328f6b74ef

Contents?: true

Size: 1.21 KB

Versions: 11

Compression:

Stored size: 1.21 KB

Contents

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

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

Interval.prototype.cast = (interval) => {
  if (typeof interval.low === 'undefined' || interval.low === null) {
    throw new Error(`Interval: ${interval} does not have a low value`);
  }
  const casted = new cql.Interval(interval.low, interval.high, interval.lowClosed, interval.highClosed);

  // Cast Low and High values to Quantities if it is a quantity
  if (casted.low && casted.low.unit && casted.low.value) {
    casted.low = new cql.Quantity(casted.low);
    if (casted.high && casted.high.unit && casted.high.value) {
      casted.high = new cql.Quantity(casted.high);
    }
    return casted;
  }

  // Cast to DateTime if it is a string representing a DateTime
  if (casted.low && Date.parse(casted.low)) {
    casted.low = cql.DateTime.fromDate(new Date(casted.low), 0);
  }
  if (casted.high && Date.parse(casted.high)) {
    casted.high = cql.DateTime.fromDate(new Date(casted.high), 0);
  }
  return casted;
};

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

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
cqm-models-0.8.4 app/assets/javascripts/basetypes/Interval.js
cqm-models-0.8.3 app/assets/javascripts/basetypes/Interval.js
cqm-models-0.8.2 app/assets/javascripts/basetypes/Interval.js
cqm-models-0.8.1 app/assets/javascripts/basetypes/Interval.js
cqm-models-0.8.0 app/assets/javascripts/basetypes/Interval.js
cqm-models-0.7.7 app/assets/javascripts/basetypes/Interval.js
cqm-models-0.7.6 app/assets/javascripts/basetypes/Interval.js
cqm-models-0.7.5 app/assets/javascripts/basetypes/Interval.js
cqm-models-0.7.4 app/assets/javascripts/basetypes/Interval.js
cqm-models-0.7.3 app/assets/javascripts/basetypes/Interval.js
cqm-models-0.7.2 app/assets/javascripts/basetypes/Interval.js