Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@skava/modules / ___dist / money / MoneyValueObject.js
Size: Mime:
"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.MoneyValueObject = void 0;

var _deps = require("./deps");
/**
 * @TODO extendCoerce
 * this is a common DDD entity, with our work cut out for us
 * @see  https://github.com/moneyphp/money/blob/master/src/Money.php
 *
 * @prop {number | string} amount
 * @prop {string} formatted (for view, with currency etc)
 * @prop {number} digits
 * @prop {string} type (type from api - usually used in Moneys to compare diff)
 * @prop {STRING} currencyCode Valid(Currency Code)
 * @prop {string} currency $, etc
 * @prop {Object} formattingOptions
 * @prop {Boolean} isValid
 */


class MoneyValueObject {
  // could be # or Obj...
  constructor(price) {
    this.diff = $ => {
      return {
        percent: +$,
        price: +$
      };
    }; // super()


    this.type = 'type';
    this.amount = price.value;
    this.raw = price;
    this.isValid = false;
    /**
     * @todo inherit globally
     * @alias currencycode
     * @desc symbol for currency, our api has invalid naming for this
     */

    this.currencySymbol = '$';
    /**
     * @api https://en.wikipedia.org/wiki/ISO_4217
     */

    this.standardCurrencyCode = 'USD';
    this.formattingOptions = {
      digits: 2
    };
  }

  get longtype() {
    return this.type === 'reg' ? 'Regular' : this.type === 'sale' ? 'Sale' : this.type === 'orig' ? 'Original' : 'N/A';
  }

  get isRange() {
    return this.raw.ismin && this.raw.ismax;
  }

  toPrimitive(hint) {
    if (hint === 'number') {
      return this.toNumber();
    } else {
      return this.toString();
    }
  } // [Symbol.toPrimitive](hint) {
  //   if (hint === 'number') {
  //     return this.toNumber()
  //   } else {
  //     return this.toString()
  //   }
  // }


  toNumber() {
    return this.amount;
  }

  toString() {
    if (this.isRange) {
      return (0, _deps.fromRangeToString)(this.min, this.max);
    } else {
      return this.toFixed();
    }
  }

  toFixed() {
    return this.amount.toFixed(this.formattingOptions.digits);
  }

  get digits() {
    return this.toString().split('.').pop();
  }
  /**
   * @todo some of our apis provide .Math
   *
   * @tutorial https://www.mathsisfun.com/percentage-calculator.html
   * @alias salePercentDiff
   * @alias saleNumberDiff
   * @param  {Money} $
   * @return {Object}
   */


}

exports.MoneyValueObject = MoneyValueObject;
Object.defineProperty(MoneyValueObject.prototype, Symbol.toPrimitive, {
  value: MoneyValueObject.prototype.toPrimitive
});