UNPKG

purespectrum-lib

Version:

shared code between front and backend projects

205 lines 8.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ChildGenderEnum = exports.Children = void 0; var tslib_1 = require("tslib"); var quota_mapper_constants_1 = require("../quota-mapper/quota-mapper.constants"); var child_1 = require("../child/child"); var util_1 = require("../../shared/util"); var child_age_gender_mapper_constants_1 = require("../child-age-gender-mapper.constants"); /** * Extension class mapper for new child age gender qualification. * A instance of this class represent a group of child, its age range, genders and * the new condition code that represents them. * This class use an Child array to determine children group age, gender and age. */ var Children = /** @class */ (function () { /** * Create a new Child group from an array of Child */ function Children(_children, _unit) { if (_unit === void 0) { _unit = quota_mapper_constants_1.YEARS_CHILD_AGE_UNIT_CODE; } this._children = _children; this._unit = _unit; this._ageUnit = this._unit; } /** * Create a new Child group from criterias * @param {boolean} isBaby - true for baby ages, meaning months from 1 to 11. * false for child ages, meaning years from 1 to 17. * @param {ChildGenderEnum | string | boolean} genderOrIsBoy - child gender: boy, girl or both * If boolean: true for boy or false for girl. * If string: values from ChildGenderEnum. * @param {number} ageFrom - "from" value of age range * @param {number} ageTo - "to" value of age range * @returns a new Children instance */ Children.create = function (isBaby, genderOrIsBoy, ageFrom, ageTo) { var ageUnit = isBaby ? quota_mapper_constants_1.MONTHS_CHILD_AGE_UNIT_CODE : quota_mapper_constants_1.YEARS_CHILD_AGE_UNIT_CODE; var gender = this._parseGender(genderOrIsBoy); var children = []; if (!isBaby) { if (ageFrom < 4 && ageTo < 4) { var ageFromValidated = this._validateAgeFrom(isBaby, ageFrom); var to = ageTo === 3 ? 47 : ageTo * 12; this._mapMonthConditionCodes(true, gender, ageFromValidated, to, children); } if (ageFrom < 4 && ageTo >= 4) { var ageFromValidated = this._validateAgeFrom(isBaby, ageFrom); var to = 47; this._mapMonthConditionCodes(true, gender, ageFromValidated, to, children); var fromYear = 4; var toYear = 4 + (ageTo - 4); this._mapYearConditionCodes(gender, fromYear, toYear, children); } if (ageFrom >= 4 && ageTo >= 4) { this._mapYearConditionCodes(gender, ageFrom, ageTo, children); } } else { this._mapMonthConditionCodes(isBaby, gender, ageFrom, ageTo, children); } return new Children(children, ageUnit); }; Children.prototype.toEachChild = function () { return this._children; }; Children._mapMonthConditionCodes = function (isBaby, gender, from, to, children) { var ageFromValidated = this._validateAgeFrom(isBaby, from); for (var index = ageFromValidated; index <= to; index++) { if (index % 12 === 0) { var age = index / 12; this._createChild(false, gender, age, children); } else { this._createChild(true, gender, index, children); } } }; Children._mapYearConditionCodes = function (gender, from, to, children) { for (var age = from; age <= to; age++) { this._createChild(false, gender, age, children); } }; Children._createChild = function (isBaby, gender, age, children) { var isBoy = gender === ChildGenderEnum.boy; if (gender === ChildGenderEnum.both) { children.push(child_1.Child.create(isBaby, true, age), child_1.Child.create(isBaby, false, age)); } else { children.push(child_1.Child.create(isBaby, isBoy, age)); } }; /** * Validate ageFrom from if it come with 0 we need to consider it as 1 * @param {number} ageFrom - "ageFrom" value */ Children._validateAgeFrom = function (isBaby, ageFrom) { if (ageFrom === 0) { return 1; } if (isBaby) { return ageFrom; } return ageFrom * 12; }; Children._parseGender = function (genderOrIsBoy) { if (typeof genderOrIsBoy === 'boolean') { return genderOrIsBoy ? ChildGenderEnum.boy : ChildGenderEnum.girl; } else if (typeof genderOrIsBoy === 'string') { var genderEnumValue = (0, util_1.getEnumByValue)(ChildGenderEnum, genderOrIsBoy); if (!genderEnumValue) { throw new Error('Gender key not found'); } return genderEnumValue; } else if (!genderOrIsBoy) { throw new Error('Gender must be defined for children creation'); } return genderOrIsBoy; }; Children.prototype._filterChildrenAges = function () { return this._children.filter(function (child) { return child.code !== child_age_gender_mapper_constants_1.NO_CHILDREN_CODE; }); }; Children.prototype._mapChildrenAges = function (filteredChildren) { var ages = []; if (this.ageUnit === quota_mapper_constants_1.YEARS_CHILD_AGE_UNIT_CODE) { filteredChildren = filteredChildren.filter(function (_a) { var isBaby = _a.isBaby; return !isBaby; }); ages = filteredChildren.map(function (_a) { var age = _a.age; return Number(age); }); if (this._children.some(function (child) { return child.code < child_age_gender_mapper_constants_1.START_CHILDREN_AGE_CODE; })) { ages.unshift(0); } } if (this.ageUnit === quota_mapper_constants_1.MONTHS_CHILD_AGE_UNIT_CODE) { ages = filteredChildren.map(function (_a) { var age = _a.age, isBaby = _a.isBaby; return isBaby ? Number(age) : Number(age) * 12; }); } return ages; }; Object.defineProperty(Children.prototype, "gender", { get: function () { var isBoy = this._children.some(function (child) { return child.isBoy; }); var isGirl = this._children.some(function (child) { return child.isBoy === false; }); if (isBoy && isGirl) { return ChildGenderEnum.both; } if (isBoy) { return ChildGenderEnum.boy; } return ChildGenderEnum.girl; }, enumerable: false, configurable: true }); Object.defineProperty(Children.prototype, "ageUnit", { get: function () { return this._ageUnit || quota_mapper_constants_1.MONTHS_CHILD_AGE_UNIT_CODE; }, enumerable: false, configurable: true }); Object.defineProperty(Children.prototype, "age", { get: function () { var filteredChildren = this._filterChildrenAges(); var ages = this._mapChildrenAges(filteredChildren); var from = Math.min.apply(Math, tslib_1.__spreadArray([], tslib_1.__read(ages), false)); var to = Math.max.apply(Math, tslib_1.__spreadArray([], tslib_1.__read(ages), false)); return { from: from, to: to }; }, enumerable: false, configurable: true }); Object.defineProperty(Children.prototype, "noChildren", { get: function () { return this._children.some(function (child) { return child.noChildren; }); }, enumerable: false, configurable: true }); Object.defineProperty(Children.prototype, "description", { get: function () { return this.noChildren ? 'No Children' : 'Children'; }, enumerable: false, configurable: true }); return Children; }()); exports.Children = Children; var ChildGenderEnum; (function (ChildGenderEnum) { ChildGenderEnum["boy"] = "111"; ChildGenderEnum["girl"] = "112"; ChildGenderEnum["both"] = "both"; })(ChildGenderEnum || (exports.ChildGenderEnum = ChildGenderEnum = {})); //# sourceMappingURL=children.js.map