Sha256: eb3de03d0f12368bb90f6bfa2e82c01ebb94f3d71a3612701e938a02fb92af48

Contents?: true

Size: 1.48 KB

Versions: 4

Compression:

Stored size: 1.48 KB

Contents

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

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

function RecursiveCast(any) {
  if (any && any.value && any.unit) {
    return new cql.Quantity(any);
  }
  if (any && any.low) {
    const casted = new cql.Interval(any.low, any.high, any.lowClosed, any.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;
  }
  if (Array.isArray(any)) {
    const casted = [];
    any.forEach((val) => {
      casted.push(RecursiveCast(val));
    });
    return casted;
  }
  if (Number.isInteger(any)) {
    return any;
  }
  if (Date.parse(any)) {
    return cql.DateTime.fromDate(new Date(any), 0);
  }
  return any;
}

Any.prototype.cast = any => RecursiveCast(any);

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
cqm-models-0.8.0 app/assets/javascripts/basetypes/Any.js
cqm-models-0.7.7 app/assets/javascripts/basetypes/Any.js
cqm-models-0.7.6 app/assets/javascripts/basetypes/Any.js
cqm-models-0.7.5 app/assets/javascripts/basetypes/Any.js