@ecomplus/utils
Version:
JS utility functions to E-Com Plus (not only) related apps
60 lines (59 loc) • 2.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
require("core-js/modules/es.regexp.exec.js");
require("core-js/modules/es.string.replace.js");
var _config2 = _interopRequireDefault(require("./../lib/config"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* @method
* @memberof ecomUtils
* @name parseDate
* @description Returns customer birth date object from string.
* @param {string} dateStr - Formatted date string
* @param {string} [countryCode=$ecomConfig.get('country_code')] - Country ISO 3166-1 alpha-2 code
* @returns {Object.<string, *>}
*
* @example
* // Date string is fixed to digits only and parsed to E-Com Plus APIs date object
* ecomUtils.parseDate('1990-01-12')
* // => { day: 12, month: 1, year: 1990 }
* ecomUtils.parseDate('10/02/1997', 'BR')
* // => { day: 10, month: 2, year: 1997 }
*
* @example
* // You can also set the configured country code first
* $ecomConfig.set('country_code', 'BR')
* // Then call `parseDate` without expliciting country code again
* ecomUtils.parseDate('10/02/1997')
* // => { day: 10, month: 2, year: 1997 }
*/
var parsePhone = function parsePhone(dateStr) {
var countryCode = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _config2.default.get('country_code');
// parse country formatted date string to { day, month, year }
var day, month, year;
if (typeof dateStr === 'string') {
var dateNumber = function dateNumber(start, ln) {
return parseInt(dateStr.substr(start, ln), 10);
};
// fix date string to digits only first
dateStr = dateStr.replace(/[\D]/g, '');
if (countryCode === 'BR') {
day = dateNumber(0, 2);
month = dateNumber(2, 2);
year = dateNumber(4, 4);
} else {
day = dateNumber(6, 2);
month = dateNumber(4, 2);
year = dateNumber(0, 4);
}
}
return {
day: day,
month: month,
year: year
};
};
var _default = exports.default = parsePhone;