Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
11 changes: 11 additions & 0 deletions playground/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions playground/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 11 additions & 4 deletions playground/src/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
22 changes: 18 additions & 4 deletions playground/src/lib/distance.js
Original file line number Diff line number Diff line change
@@ -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));
}
22 changes: 16 additions & 6 deletions playground/src/lib/temperature.js
Original file line number Diff line number Diff line change
@@ -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));
}
22 changes: 18 additions & 4 deletions playground/src/lib/weight.js
Original file line number Diff line number Diff line change
@@ -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));
}
121 changes: 121 additions & 0 deletions playground/tests/new-units.test.js
Original file line number Diff line number Diff line change
@@ -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);
});