'use strict'; const { convert, convertMany } = require('convert'); 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 convertManyApi(input, to) { const curatedInput = input .replace('\'', 'ft') .replace(/"|''/, 'in') .replace(/\d+ft\s*\d+\s*$/, (match) => `${match}in`); // height without any inch symbol return Math.round(convertMany(curatedInput).to(to)) || null; } function convertApi(input, fromOrTo, to) { if (!input) { return null; } 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).from(fromOrTo).to(to)) || null; } catch (error) { logger.error(error); return null; } } module.exports = { cmToFeetInches, cmToInches, feetInchesToCm, heightToCm, inchesToCm, lbsToKg, kgToLbs, convert: convertApi, };