Repository URL to install this package:
|
Version:
1.2.8 ▾
|
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.transformPricing = transformPricing;
exports.fromRangeToString = fromRangeToString;
exports.fromRangeToStringPure = fromRangeToStringPure;
exports.formatRangeNumbers = formatRangeNumbers;
exports.validateRange = validateRange;
exports.lowercasePriceLabel = lowercasePriceLabel;
exports.toValidDecimals = toValidDecimals;
exports.toFormattedPriceValue = toFormattedPriceValue;
exports.fromFindingsToFound = fromFindingsToFound;
Object.defineProperty(exports, "isStringOrNumber", {
enumerable: true,
get: function get() {
return _exotic.isStringOrNumber;
}
});
exports.PriceType = exports.isRange = void 0;
var _index = require("../chain-able/index.js");
var _exotic = require("../exotic");
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
// const isStringOrNumber = x => isString(x) || isNumber(x)
/**
* @todo - this should be handled in Money
*/
// if (value === '' || value.startsWith('$')) {
// return value
// } else {
// return '$' + value
// }
const customizablePriceFormatters = {
standardCurrencyCode: 'USD',
currencySymbol: '$',
rangeSeparator: ' - '
/**
* @see toValidDecimals
* @param {String | Number} from
* @param {String | Number} to
* @return {String}
*/
};
function formatRangeNumbers(from, to) {
const rangeSeparator = customizablePriceFormatters.rangeSeparator;
return toValidDecimals(from) + rangeSeparator + toValidDecimals(to);
}
/**
* @param {String | Number} min
* @param {String | Number} max
* @throws {TypeError} InvalidArguments if not string or number min & max
*/
function validateRange(min, max) {
// validate
if ((0, _exotic.isStringOrNumber)(min) === false || (0, _exotic.isStringOrNumber)(max) === false) {
const data = (0, _index.stringify)({
min,
max
});
const msg = 'InvalidArguments: ' + data;
throw new Error(msg);
}
}
/**
* @private
* @extends fromRangeToString
*
* @param {Number | String | Array | Object} min
* @param {Number | String | undefined} [max]
* @return {String}
*/
function fromRangeToStringPure(min, max) {
const currencySymbol = customizablePriceFormatters.currencySymbol;
const minPrice = currencySymbol + min;
const maxPrice = currencySymbol + max;
return formatRangeNumbers(minPrice, maxPrice);
}
/**
* @description curried formatter, does tail call
* @param {Number | String | Array | Object} min
* @param {Number | String | undefined} [max]
* @return {String}
*
* @example
* fromRangeToString(1, 2)
* //=> $1 - $2
*
* fromRangeToString({min: 3, max: 4})
* // => $3 - $4
*
* fromRangeToString([5, 6])
* // => $5 - $6
*/
function fromRangeToString(min, max) {
if (arguments.length === 1) {
// just spread if array
if ((0, _exotic.isArray)(min)) {
return fromRangeToString.apply(this, min);
} // spread props when obj
else if ((0, _exotic.isObj)(min) && min.min && min.max) {
return fromRangeToString(min.min, min.max); // pass in .min .max
}
} // invariant
validateRange(min, max); // real formatting
return fromRangeToStringPure(min, max);
} // @todo where is original price? <- no such thing
const EnumType = (...args) => [...args];
const PriceType = {
value: Number('1.00'),
label: EnumType('sale', 'reg'),
type: EnumType('Sale', 'Reg'),
ismin: Boolean('true'),
ismax: Boolean('true') // and also like productinfo etc...
};
exports.PriceType = PriceType;
const PricingType = {
currencycode: '$',
prices: [PriceType]
/**
* @param {Pricing} price
* @return {void}
*/
};
function lowercasePriceLabel(price) {
price.type = price.type.toLowerCase();
price.label = price.label.toLowerCase();
}
/**
* @todo - isRange - split each value if it is a range...
*
* @param {PriceType | PricingType | PricingType} pricing
* @return {void}
* @description @modifies .pricing .type
*/
function transformPricing(pricing = {}) {
// use array when we have
const prices = (0, _exotic.isArray)(pricing) ? pricing // the obj may be buyinfo, or the price
: (0, _exotic.isObj)(pricing.prices) // buyinfo
? pricing.prices // price obj
: pricing; // plain pricing obj
if ((0, _exotic.isArray)(prices) === false && (0, _exotic.isObj)(pricing) === true && (0, _exotic.isString)(pricing.type)) {
lowercasePriceLabel(prices);
} else if ((0, _exotic.isArray)(prices)) {
// unify casing
prices.forEach(lowercasePriceLabel);
}
(0, _exotic.autofixSafe)(pricing);
}
const isRange = price => {
return (0, _exotic.isString)(price) ? price.includes('-') : (0, _exotic.isObj)(price) ? price.isRange : !!(0, _exotic.isArray)(price);
};
/**
* @todo add tests
* @param {String | Number} price
*/
exports.isRange = isRange;
function toValidDecimals(price) {
// @example 0, '', undefined, null...
if (!price) {
return '';
} // @example 4.09
else if (price.includes('.')) {
// @example 4.0 => 4.00
// @example 4.0.split.length => 1
if (price.split('.').pop().length <= 1) {
return price + '0';
} // console.log({ price })
// return ''
return price || '';
} else {
// @example 4 => 4.00
return price + '.00';
}
}
/**
* @todo test
* @example 4.00 => $4.00
* @example $4.00 => $4.00
* @example 4.0 => $4.00
*
* @param {String | Number} price
*/
function toFormattedPriceValue(price) {
// @todo simplify - price does not have a .includes so
// isFunction(price.toString) || isSafe(price.value) || hasOwnProp(price, 'value';)
if ((0, _exotic.isObj)(price) && price.value) {
price = price.value;
} else if ((0, _exotic.isNumber)(price)) {
price = `${price}`;
} else if ((0, _exotic.isSafe)(price) === false) {
price = '';
} else if ((0, _exotic.isString)(price) === false || (0, _exotic.isArray)(price) === false) {// price = ''
}
const currencySymbol = customizablePriceFormatters.currencySymbol; // @example '', 0, undefined, null, false, true...
if (!price) {
return '';
} else if (price.includes(currencySymbol)) {
return toValidDecimals(price);
} else {
return currencySymbol + toValidDecimals(price);
}
}
/**
* @param {Number | String | Array | Object} findings
* @return {Object} formatted from array or obj or nothing to obj
*/
// eslint-disable-next-line
function fromFindingsToFound(findings = []) {
let found; // not found
// if (findings === undefined) <- when using .find
if (findings.length === 0) {
found = {};
} // min-max / range
// isArray(found) <- when using .find
else if (findings.length === 2) {
const _findings = _slicedToArray(findings, 2),
min = _findings[0],
max = _findings[1]; // assety(isObj(min))
// assert(isObj(max))
const isNestedRanges = isRange(min.value) || isRange(max.value);
const range = isNestedRanges ? min.value || max.value : fromRangeToString(min.value, max.value); // const minValue = toValidDecimals(min.value)
// const maxValue = toValidDecimals(max.value)
found = {
// ismax: true, ismin: true, isrange: true,
label: min.label || max.label,
type: min.type || max.type,
value: range
};
} // just min or max or sale or reg
else if (findings.length === 1) {
found = findings[0];
} else {
throw new TypeError('found 3!');
}
return found;
}