134 lines
2.8 KiB
JavaScript
Executable File
134 lines
2.8 KiB
JavaScript
Executable File
'use strict';
|
||
|
||
const { convert, convertMany } = require('convert');
|
||
const { decode } = require('html-entities');
|
||
|
||
const logger = require('../logger')(__filename);
|
||
|
||
function inchesToCm(inches) {
|
||
if (!inches) return null;
|
||
|
||
return Math.round(Number(inches) * 2.54);
|
||
}
|
||
|
||
function feetInchesToCm(feet, inches) {
|
||
if (!feet && !inches) return null;
|
||
|
||
if (typeof feet === 'string' && !inches) {
|
||
const [feetPart, inchesPart] = feet.match(/\d+/g);
|
||
return feetInchesToCm(Number(feetPart), Number(inchesPart));
|
||
}
|
||
|
||
return Math.round((Number(feet) * 30.48) + ((Number(inches) || 0) * 2.54));
|
||
}
|
||
|
||
function cmToFeetInches(centimeters) {
|
||
if (!centimeters) return null;
|
||
|
||
const feet = Math.floor(centimeters / 30.48);
|
||
const inches = Math.round((centimeters / 2.54) % (feet * 12));
|
||
|
||
return { feet, inches };
|
||
}
|
||
|
||
function cmToInches(centimeters) {
|
||
return centimeters / 2.54;
|
||
}
|
||
|
||
function heightToCm(height) {
|
||
if (!height) return null;
|
||
|
||
const [feet, inches] = height.match(/\d+/g);
|
||
|
||
return feetInchesToCm(feet, inches);
|
||
}
|
||
|
||
function lbsToKg(lbs) {
|
||
if (!lbs) return null;
|
||
|
||
const pounds = lbs.toString().match(/\d+/)[0];
|
||
|
||
return Math.round(Number(pounds) * 0.453592);
|
||
}
|
||
|
||
function kgToLbs(kgs) {
|
||
if (!kgs) return null;
|
||
|
||
const kilos = kgs.toString().match(/\d+/)[0];
|
||
|
||
return Math.round(Number(kilos) / 0.453592);
|
||
}
|
||
|
||
function curateConvertInput(string) {
|
||
if (/['’]|(fe*o*t)/.test(string)) {
|
||
const result = string.match(/(\d+).*?(\d+)/);
|
||
|
||
if (result) {
|
||
return `${result[1]}ft ${result[2]}in`;
|
||
}
|
||
}
|
||
|
||
return string;
|
||
}
|
||
|
||
function convertManyApi(input, to) {
|
||
const curatedInput = curateConvertInput(input);
|
||
|
||
return Math.round(convertMany(curatedInput).to(to)) || null;
|
||
}
|
||
|
||
function convertApi(rawInput, fromOrTo, to) {
|
||
if (!rawInput) {
|
||
return null;
|
||
}
|
||
|
||
const input = decode(rawInput); // remove html entities, e.g. 5' 8" for 5' 8"
|
||
|
||
try {
|
||
if (typeof input === 'string' && to === undefined) {
|
||
return convertManyApi(input, fromOrTo);
|
||
}
|
||
|
||
const inputNumber = Number(typeof input === 'string' ? input.match(/\d+(\.\d+)?/)?.[0] : input);
|
||
|
||
return Math.round(convert(inputNumber, fromOrTo).to(to)) || null;
|
||
} catch (error) {
|
||
logger.error(error);
|
||
return null;
|
||
}
|
||
}
|
||
|
||
function maleFeetUsToEu(input) {
|
||
if (!input) {
|
||
return null;
|
||
}
|
||
|
||
const size = Number(input.toString().match(/\d+(\.\d+)?/)?.[0]);
|
||
|
||
return Math.round((1.27 * size + 29.94) / 0.5) * 0.5; // round to nearest half
|
||
}
|
||
|
||
function femaleFeetUsToEu(input) {
|
||
if (!input) {
|
||
return null;
|
||
}
|
||
|
||
const size = Number(input.toString().match(/\d+(\.\d+)?/)?.[0]);
|
||
|
||
// return Math.round((1.27 * size + 28.67) / 0.5) * 0.5; // round to nearest half
|
||
return Math.round(1.27 * size + 28.67);
|
||
}
|
||
|
||
module.exports = {
|
||
cmToFeetInches,
|
||
cmToInches,
|
||
feetInchesToCm,
|
||
heightToCm,
|
||
inchesToCm,
|
||
lbsToKg,
|
||
kgToLbs,
|
||
maleFeetUsToEu,
|
||
femaleFeetUsToEu,
|
||
convert: convertApi,
|
||
};
|