UNPKG

bmi-utils

Version:

An library with some functions relates to Body Mass Index (BMI), like calculate and get the ideial weight

216 lines (190 loc) 4.34 kB
// bmi-utils v1.1.0 // An library with some functions relates to Body Mass Index (BMI), like calculate and get the ideial weight // https://github.com/emanuelgsouza/bmi-utils.git // (c) 2019 Emanuel Souza <emanuelgdesouza@gmail.com> /** * @method lte * @param {Number} number * @param {Number} other * @return {Boolean} */ const lte = (number, other) => number <= other; /** * @method gte * @param {Number} number * @param {Number} other * @return {Boolean} */ const gte = (number, other) => number >= other; /** * @method isNumber * @param {Any} value * @return {Boolean} */ const isNumber = value => typeof value === 'number'; /** * @method inRange * @param {Number} value * @param {Number} start * @param {Number} end * @return {Boolean} */ const inRange = (value, start, end) => value >= start && value < end; /** * @method isEmpty * @param {Any} value * @return {Boolean} */ const isEmpty = value => { if (value === null) return true if (value === undefined) return true if (Object.prototype.hasOwnProperty.call(value, 'length')) { return value.length === 0 } return false }; /** * @method imcCalc * @param {Number} heigth * @param {Number} weight * @return {Number} */ const imcCalc = (weight, heigth) => { if (lte(weight, 0) || lte(heigth, 0)) { return 0 } const result = weight / (heigth * heigth); return result.toFixed(2) }; const MALE = 'M'; const FEMALE = 'F'; var GENRES = /*#__PURE__*/Object.freeze({ __proto__: null, MALE: MALE, FEMALE: FEMALE }); /** * @method isValidGenre * @param {String} genre * @return {Boolean} */ const isValidGenre = genre => Object.values(GENRES).includes(genre); /** * @type{Object<Array>} */ const ideialIMC = { [ MALE ]: [ 20.7, 26.39 ], [ FEMALE ]: [ 19.1, 25.79 ] }; /** * @method getBestWeight * @param {Number} height * @param {Number} ideialIMC * @return {String} */ const getBestWeight = (height, ideialIMC) => { const result = ideialIMC * (height * height); return result.toFixed(2) }; /** * @method ideialWeight * @param {Number} height * @param {String} genre can be 'F' (female) or 'M' (male) * @return {Object} { min, max } // min and max weights */ const ideialWeight = (height, genre) => { const result = { min: 0, max: 0 }; if (!isNumber(height) || isEmpty(genre)) { return result } if (!isValidGenre(genre)) { return result } return { min: getBestWeight(height, ideialIMC[genre][0]), max: getBestWeight(height, ideialIMC[genre][1]) } }; /** * @type{Object} */ var tableCategories = { [ MALE ]: [ imc => lte(Number(imc), 0), imc => inRange(Number(imc), 0.1, 20.7), imc => inRange(Number(imc), 20.7, 26.4), imc => inRange(Number(imc), 26.4, 27.8), imc => inRange(Number(imc), 27.8, 31.1), imc => gte(Number(imc), 31.10) ], [ FEMALE ]: [ imc => lte(Number(imc), 0), imc => inRange(Number(imc), 0.1, 19.1), imc => inRange(Number(imc), 19.1, 25.8), imc => inRange(Number(imc), 25.8, 27.3), imc => inRange(Number(imc), 27.3, 32.3), imc => gte(Number(imc), 32.3) ] }; /** * @method loadIndex * @param {String} genre * @return {Function} */ const loadIndex = genre => weight => { return tableCategories[ genre ] .reduce((acc, fn, index) => { if (fn(weight)) { acc = index; } return acc }, 0) }; /** * @type{Object} */ const messages = { 'pt-BR': [ null, 'Abaixo do peso', 'Peso normal', 'Um pouco acima do peso', 'Acima do peso', 'Obeso' ], 'en-US': [ null, 'Underweight', 'Normal weight', 'A little overweight', 'Overweight', 'Obese' ], 'es': [ null, 'Bajo peso', 'Peso normal', 'Un poco de sobrepeso', 'Sobre peso', 'Obeso' ] }; /** * @method loadCategory * @param {String} genre * @param {Number} imc * @param {String} translation can be 'pt-BR' or 'en-US' * @return {String} */ const loadCategory = (genre, imc, translation = 'en-US') => { if (!isValidGenre(genre) || !isNumber(imc) || isEmpty(translation)) { return '' } const loadByGenre = loadIndex(genre); const index = loadByGenre(imc); return messages[translation][index] || '' }; export { imcCalc as calculate, ideialWeight, loadCategory };