all files / ember-joda/transforms/ joda.js

28.57% Statements 2/7
100% Branches 0/0
25% Functions 1/4
28.57% Lines 2/7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59                                48×   48×                                                                                
import Transform from 'ember-data/transform';
import { assert } from '@ember/debug';
 
/**
 * The base class for all joda transforms.
 * @protected
 */
export default class JodaTransform extends Transform {
  /**
   * Override this property in derived classes.
   * @type {Function}
   * @private
   */
  // static JodaClass = null;
 
  constructor() {
    super(...arguments);
 
    assert(
      'You have to define a JodaClass for a JodaTransform.',
      typeof this.constructor.JodaClass === 'function'
    );
  }
 
  /**
   * Deserializes a serialized joda type.
   * @param  {String} serialized
   * @return {Object}
   */
  deserialize(serialized) {
    return this.constructor.JodaClass.parse(serialized);
  }
 
  /**
   * Serializes a joda type.
   * @param  {Object} deserialized
   * @return {String}
   */
  serialize(deserialized) {
    this._assertJodaType(deserialized);
    return deserialized.toString();
  }
 
  /**
   * Asserts that the provided input is of the correct joda type.
   * @param  {Object} obj
   * @throws
   * @protected
   */
  _assertJodaType(obj) {
    const { constructor, constructor: { JodaClass } } = this;
    assert(
      `${constructor.name} can only serialize ${JodaClass.name} instances.`,
      obj instanceof JodaClass
    );
  }
}