From d7d00046b16dbe77769759ad2c00f1b7f7b62c34 Mon Sep 17 00:00:00 2001 From: Sebin Nyshkim Date: Thu, 26 Jun 2025 19:47:46 +0200 Subject: [PATCH] refactor: :recycle: restructure character data files, move shared functionality into library --- src/includes/util.js | 73 +++++++++++ src/index.webc | 2 +- src/jarek/anatomy.md | 2 +- src/jarek/jarek.11tydata.js | 55 +++----- src/sebin/anatomy.md | 4 +- src/sebin/sebin.11tydata.js | 219 ++++++++++++-------------------- src/viktor/anatomy.md | 2 +- src/viktor/viktor.11tydata.js | 155 ++++------------------ src/viktor/viktor.11tydata.json | 39 +++++- 9 files changed, 239 insertions(+), 312 deletions(-) create mode 100644 src/includes/util.js diff --git a/src/includes/util.js b/src/includes/util.js new file mode 100644 index 0000000..4ae5c4b --- /dev/null +++ b/src/includes/util.js @@ -0,0 +1,73 @@ +const getClientLocale = () => { + return navigator.languages.length > 0 ? navigator.languages[0] : 'en-US'; +}; + +const getAge = (dateOfBirth) => { + const today = new Date(); + + const thisYear = today.getFullYear(); + const thisMonth = today.getMonth(); + const thisDay = today.getDate(); + + const dobYear = dateOfBirth.getFullYear(); + const dobMonth = dateOfBirth.getMonth(); + const dobDay = dateOfBirth.getDate(); + + let age = thisYear - dobYear; + + if (thisMonth < dobMonth) age--; + if (thisMonth === dobMonth && thisDay < dobDay) age--; + + return age; +}; + +const toImperial = (cm) => { + const realFeet = (cm * 0.3937) / 12; + const feet = Math.floor(realFeet); + const inches = Math.round((realFeet - feet) * 12); + + return `${feet}'${inches}"`; +}; + +const toInch = (cm) => { + return `${Math.round(cm / 2.45)} in`; +}; + +const toLbs = (kg) => { + const nearExact = kg / 0.45359237; + const lbs = Math.floor(nearExact); + + return lbs; +}; + +const toFahrenheit = (celsius) => { + return celsius * 1.8 + 32; +}; + +const dateFormat = new Intl.DateTimeFormat(getClientLocale(), { + year: 'numeric', + month: 'long', + day: '2-digit' +}); + +const getFullName = (...names) => names.join(' '); +const getDateOfBirth = (dob) => `${dateFormat.format(dob)} (${getAge(dob)})`; +const getHeight = (height) => `${height} cm (${toImperial(height)})`; +const getWeight = (weight) => `${weight} kg (${toLbs(weight)} lbs)`; +const getTailLength = (length) => `${length / 100} m (${toImperial(length)})`; +const getWingspan = (wingspan) => `${wingspan / 100} m (${toImperial(wingspan)})`; + +export { + getAge, + toImperial, + toInch, + toLbs, + toFahrenheit, + dateFormat, + getFullName, + getDateOfBirth, + getHeight, + getWeight, + getTailLength, + getWingspan +}; diff --git a/src/index.webc b/src/index.webc index 61caeaa..cff532f 100644 --- a/src/index.webc +++ b/src/index.webc @@ -1,7 +1,7 @@