-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add some tests and update the deadme for the code
- Loading branch information
1 parent
147b18b
commit 2aa5474
Showing
6 changed files
with
128 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { assertEquals, assertThrows } from "https://deno.land/std/testing/asserts.ts" | ||
import { percent, percentToRatio, toFixed, clamp } from "./math.js" | ||
|
||
Deno.test("percent", () => { | ||
assertEquals(percent(50, 200), 100) | ||
assertEquals(percent(25, 80), 20) | ||
assertEquals(percent(75, 120), 90) | ||
}) | ||
|
||
Deno.test("percent throws error for invalid input", () => { | ||
assertThrows(() => percent("50", 200)) | ||
assertThrows(() => percent(50, "200")) | ||
assertThrows(() => percent(150, 200)) | ||
assertThrows(() => percent(-50, 200)) | ||
}) | ||
|
||
Deno.test("percentToRatio", () => { | ||
assertEquals(percentToRatio(50), 0.5) | ||
assertEquals(percentToRatio(25), 0.25) | ||
assertEquals(percentToRatio(75), 0.75) | ||
}) | ||
|
||
Deno.test("toFixed", () => { | ||
assertEquals(toFixed(1.2345), 1.234) | ||
assertEquals(toFixed(3.14159, 2), 3.14) | ||
assertEquals(toFixed(0.123456789, 6), 0.123457) | ||
}) | ||
|
||
Deno.test("clamp", () => { | ||
assertEquals(clamp(5, 0, 10), 5) | ||
assertEquals(clamp(15, 0, 10), 10) | ||
assertEquals(clamp(-5, 0, 10), 0) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,24 @@ | ||
/** | ||
* Taks a nested array of coordinates and returns a string suitable to pass to | ||
* the `points` attribute of an SVG polygon element. | ||
* @param {...any} coordinates each coordinate is an array like [x, y] | ||
* @param {...any} coordinates an array of coordinates. Each coordinate is an array like [x, y]. | ||
* @returns a string like 'x1,y1 x2,y2 x3,y3' | ||
*/ | ||
export function arrToPolygonPoints(...coordinates) { | ||
return coordinates.map(pair => pair.join(',')).join(' ') | ||
if (coordinates.length === 0) { | ||
throw new Error('Cannot convert empty array to polygon points') | ||
} | ||
|
||
return coordinates.map(function (coordinate) { | ||
if (!Array.isArray(coordinate)) { | ||
throw new Error(`Each coordinate is supposed to be an array. Got ${coodinate}`) | ||
} | ||
if (coordinate.length !== 2) { | ||
throw new Error(`Each coordinate is supposed to be an array of length 2. Got ${coordinate}`) | ||
} | ||
if (!Number.isFinite(coordinate[0]) || !Number.isFinite(coordinate[1])) { | ||
throw new Error(`Each coordinate is supposed to be an array of finite numbers. Got ${coordinate}`) | ||
} | ||
return coordinate.join(',') | ||
}).join(' ') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { assertEquals, assertThrows } from "https://deno.land/std/testing/asserts.ts" | ||
import { arrToPolygonPoints } from "./svg.js" | ||
|
||
Deno.test("arrToPolygonPoints", () => { | ||
const coordinates = [ | ||
[0, 0], | ||
[0, 1], | ||
[1, 1], | ||
[1, 0], | ||
] | ||
const expected = "0,0 0,1 1,1 1,0" | ||
assertEquals(arrToPolygonPoints(...coordinates), expected) | ||
}) | ||
|
||
Deno.test("arrToPolygonPoints throws with no input", () => { | ||
assertThrows(() => arrToPolygonPoints()) | ||
}) | ||
|
||
Deno.test("arrToPolygonPoints with single point", () => { | ||
assertEquals(arrToPolygonPoints( | ||
[0, 0] | ||
), '0,0') | ||
}) | ||
|
||
Deno.test("arrToPolygonPoints with invalid input", () => { | ||
const coordinates = [1, 2, 3] | ||
assertThrows(() => arrToPolygonPoints(...coordinates)) | ||
}) | ||
|
||
Deno.test("arrToPolygonPoints with invalid input", () => { | ||
assertThrows(() => arrToPolygonPoints([1, 2, 3])) | ||
}) | ||
|
||
Deno.test("arrToPolygonPoints with invalid input", () => { | ||
assertThrows(() => arrToPolygonPoints( | ||
["1", 2], | ||
[3, 4], | ||
)) | ||
assertThrows(() => arrToPolygonPoints( | ||
[1, 2], | ||
[3, "4"], | ||
)) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { assertEquals } from "https://deno.land/std/testing/asserts.ts" | ||
import { humanTime } from "./time.js" | ||
|
||
Deno.test('humanTime()', () => { | ||
assertEquals(humanTime(10), '10 seconds') | ||
assertEquals(humanTime(60), '1 minute') | ||
assertEquals(humanTime(120), '2 minutes') | ||
assertEquals(humanTime(3600), '1 hour') | ||
assertEquals(humanTime(3660), '1 hour, 1 minute') | ||
}) | ||
|
||
Deno.test('humanTime() short form', () => { | ||
assertEquals(humanTime(10, true), '10s') | ||
assertEquals(humanTime(60, true), '1m') | ||
assertEquals(humanTime(120, true), '2m') | ||
assertEquals(humanTime(3600, true), '1h') | ||
assertEquals(humanTime(3660, true), '1h, 1m') | ||
}) |