diff --git a/README.md b/README.md index f62f10a..a29e8f9 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,9 @@ # Onboarding Guide +> **Note:** This README was updated. + +> **Additional Note:** This is a second note. + Thank you for your interest in our study. This guide walks you through all of the required setup steps so that you're fully ready to participate in the main study. As you work through this onboarding guide, please update your completion status using [this form](https://forms.gle/SLXdJk3SbjHCYnpd9). diff --git a/playground/README.md b/playground/README.md index 56de75a..3f4d8df 100644 --- a/playground/README.md +++ b/playground/README.md @@ -38,9 +38,20 @@ npm install ## Usage ```bash +# Temperature conversions npx convert temperature 100 C F +npx convert temperature 273.15 K C +npx convert temperature 0 K F + +# Distance conversions npx convert distance 5 km mi +npx convert distance 1000 m km +npx convert distance 1609 m mi + +# Weight conversions npx convert weight 200 g oz +npx convert weight 1 lb g +npx convert weight 16 oz lb ``` ## Run tests diff --git a/playground/package-lock.json b/playground/package-lock.json new file mode 100644 index 0000000..74184bc --- /dev/null +++ b/playground/package-lock.json @@ -0,0 +1,15 @@ +{ + "name": "tiny-unit-converter", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "tiny-unit-converter", + "version": "1.0.0", + "bin": { + "convert": "bin/cli.js" + } + } + } +} diff --git a/playground/src/convert.js b/playground/src/convert.js index 33b48ee..884d110 100644 --- a/playground/src/convert.js +++ b/playground/src/convert.js @@ -12,17 +12,24 @@ const defaults = JSON.parse( ); export function convert(type, value, from, to) { + // Validate input value is numeric + const numValue = Number(value); + if (isNaN(numValue)) { + throw new Error("Invalid number: value must be numeric"); + } + switch (type) { case "temperature": return temperature.convertTemperature( - value, + numValue, from || defaults.temperature.defaultFrom, - to || defaults.temperature.defaultTo + to || defaults.temperature.defaultTo, + defaults.precision ); case "distance": - return distance.convertDistance(value, from, to); + return distance.convertDistance(numValue, from, to, defaults.precision); case "weight": - return weight.convertWeight(value, from, to); + return weight.convertWeight(numValue, from, to, defaults.precision); default: throw new Error("Unknown type " + type); } diff --git a/playground/src/lib/distance.js b/playground/src/lib/distance.js index 8bca154..de9edd6 100644 --- a/playground/src/lib/distance.js +++ b/playground/src/lib/distance.js @@ -1,5 +1,19 @@ -export function convertDistance(value, from, to) { - if (from === "km" && to === "mi") return value * 0.621371; - if (from === "mi" && to === "km") return value / 0.621371; - throw new Error(`Unsupported distance conversion: ${from} to ${to}`); +export function convertDistance(value, from, to, precision = 2) { + let result; + if (from === "km" && to === "mi") { + result = value * 0.621371; + } else if (from === "mi" && to === "km") { + result = value / 0.621371; + } else if (from === "m" && to === "km") { + result = value / 1000; + } else if (from === "km" && to === "m") { + result = value * 1000; + } else if (from === "m" && to === "mi") { + result = value * 0.000621371; + } else if (from === "mi" && to === "m") { + result = value * 1609.344; + } else { + throw new Error(`Unsupported distance conversion: ${from} to ${to}`); + } + return Number(result.toFixed(precision)); } diff --git a/playground/src/lib/temperature.js b/playground/src/lib/temperature.js index 69ebff6..5a1e329 100644 --- a/playground/src/lib/temperature.js +++ b/playground/src/lib/temperature.js @@ -1,9 +1,19 @@ -export function convertTemperature(value, from, to) { +export function convertTemperature(value, from, to, precision = 2) { + let result; if (from === "C" && to === "F") { - return value * (9 / 5) + 32; + result = value * (9 / 5) + 32; + } else if (from === "F" && to === "C") { + result = (value - 32) * (5 / 9); + } else if (from === "K" && to === "C") { + result = value - 273.15; + } else if (from === "C" && to === "K") { + result = value + 273.15; + } else if (from === "K" && to === "F") { + result = value * (9 / 5) - 459.67; + } else if (from === "F" && to === "K") { + result = (value + 459.67) * (5 / 9); + } else { + throw new Error(`Unsupported temperature conversion: ${from} to ${to}`); } - if (from === "F" && to === "C") { - return (value - 32) * (5 / 9); - } - throw new Error(`Unsupported temperature conversion: ${from} to ${to}`); + return Number(result.toFixed(precision)); } diff --git a/playground/src/lib/weight.js b/playground/src/lib/weight.js index d69877c..85dfcb2 100644 --- a/playground/src/lib/weight.js +++ b/playground/src/lib/weight.js @@ -1,5 +1,19 @@ -export function convertWeight(value, from, to) { - if (from === "g" && to === "oz") return value / 28.3495; - if (from === "oz" && to === "g") return value * 28.3495; - throw new Error(`Unsupported weight conversion: ${from} to ${to}`); +export function convertWeight(value, from, to, precision = 2) { + let result; + if (from === "g" && to === "oz") { + result = value / 28.3495; + } else if (from === "oz" && to === "g") { + result = value * 28.3495; + } else if (from === "lb" && to === "g") { + result = value * 453.592; + } else if (from === "g" && to === "lb") { + result = value / 453.592; + } else if (from === "lb" && to === "oz") { + result = value * 16; + } else if (from === "oz" && to === "lb") { + result = value / 16; + } else { + throw new Error(`Unsupported weight conversion: ${from} to ${to}`); + } + return Number(result.toFixed(precision)); } diff --git a/playground/tests/new-units.test.js b/playground/tests/new-units.test.js new file mode 100644 index 0000000..b559c4f --- /dev/null +++ b/playground/tests/new-units.test.js @@ -0,0 +1,121 @@ +import { test } from "node:test"; +import { strictEqual } from "node:assert"; +import { convertTemperature } from "../src/lib/temperature.js"; +import { convertDistance } from "../src/lib/distance.js"; +import { convertWeight } from "../src/lib/weight.js"; + +// Temperature - Kelvin tests +test("converts Kelvin to Celsius (freezing point)", () => { + strictEqual(convertTemperature(273.15, "K", "C"), 0); +}); + +test("converts Kelvin to Celsius (boiling point)", () => { + strictEqual(convertTemperature(373.15, "K", "C"), 100); +}); + +test("converts Celsius to Kelvin (freezing point)", () => { + strictEqual(convertTemperature(0, "C", "K"), 273.15); +}); + +test("converts Celsius to Kelvin (boiling point)", () => { + strictEqual(convertTemperature(100, "C", "K"), 373.15); +}); + +test("converts Kelvin to Fahrenheit (freezing point)", () => { + strictEqual(convertTemperature(273.15, "K", "F"), 32); +}); + +test("converts Kelvin to Fahrenheit (absolute zero)", () => { + strictEqual(convertTemperature(0, "K", "F"), -459.67); +}); + +test("converts Fahrenheit to Kelvin (freezing point)", () => { + strictEqual(convertTemperature(32, "F", "K"), 273.15); +}); + +test("converts Fahrenheit to Kelvin (absolute zero)", () => { + strictEqual(convertTemperature(-459.67, "F", "K"), 0); +}); + +// Distance - Meters tests +test("converts meters to kilometers", () => { + strictEqual(convertDistance(1000, "m", "km"), 1); +}); + +test("converts meters to kilometers (5000m)", () => { + strictEqual(convertDistance(5000, "m", "km"), 5); +}); + +test("converts kilometers to meters", () => { + strictEqual(convertDistance(1, "km", "m"), 1000); +}); + +test("converts kilometers to meters (2.5km)", () => { + strictEqual(convertDistance(2.5, "km", "m"), 2500); +}); + +test("converts meters to miles", () => { + strictEqual(convertDistance(1609.344, "m", "mi"), 1); +}); + +test("converts meters to miles (1000m)", () => { + strictEqual(convertDistance(1000, "m", "mi"), 0.62); +}); + +test("converts miles to meters", () => { + strictEqual(convertDistance(1, "mi", "m"), 1609.34); +}); + +test("converts miles to meters (5mi)", () => { + strictEqual(convertDistance(5, "mi", "m"), 8046.72); +}); + +// Weight - Pounds tests +test("converts pounds to grams", () => { + strictEqual(convertWeight(1, "lb", "g"), 453.59); +}); + +test("converts pounds to grams (2lb)", () => { + strictEqual(convertWeight(2, "lb", "g"), 907.18); +}); + +test("converts grams to pounds", () => { + strictEqual(convertWeight(453.592, "g", "lb"), 1); +}); + +test("converts grams to pounds (1000g)", () => { + strictEqual(convertWeight(1000, "g", "lb"), 2.2); +}); + +test("converts pounds to ounces", () => { + strictEqual(convertWeight(1, "lb", "oz"), 16); +}); + +test("converts pounds to ounces (2.5lb)", () => { + strictEqual(convertWeight(2.5, "lb", "oz"), 40); +}); + +test("converts ounces to pounds", () => { + strictEqual(convertWeight(16, "oz", "lb"), 1); +}); + +test("converts ounces to pounds (8oz)", () => { + strictEqual(convertWeight(8, "oz", "lb"), 0.5); +}); + +// Edge cases +test("handles very small meter distances", () => { + strictEqual(convertDistance(1, "m", "mi"), 0); // 0.000621371 rounds to 0.00 +}); + +test("handles absolute zero in Kelvin to Celsius", () => { + strictEqual(convertTemperature(0, "K", "C"), -273.15); +}); + +test("handles room temperature in Celsius to Kelvin", () => { + strictEqual(convertTemperature(20, "C", "K"), 293.15); +}); + +test("handles typical weight conversion (100g to lb)", () => { + strictEqual(convertWeight(100, "g", "lb"), 0.22); +});