Skip to content

Commit 616e889

Browse files
committed
Add double
1 parent 4619e36 commit 616e889

File tree

4 files changed

+86
-4
lines changed

4 files changed

+86
-4
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ This package's main module's default export is an object with a variety of metho
2626

2727
## Status
2828

29-
So far only `boolean` and `DOMString` types are implemented. This list will grow over time in service of the [HTML as Custom Elements](https://github.com/dglazkov/html-as-custom-elements) project, but in the meantime, pull requests welcome!
29+
So far only `boolean`, `DOMString`, and `double` types are implemented. This list will grow over time in service of the [HTML as Custom Elements](https://github.com/dglazkov/html-as-custom-elements) project, but in the meantime, pull requests welcome!
3030

3131
I'm not sure yet what the strategy will be for modifiers, e.g. [`[Clamp]`](http://heycam.github.io/webidl/#Clamp). Maybe something like `conversions["unsigned long"](x, { clamp: true })`? We'll see.
3232

33+
We might also want to extend the API to give better error messages, e.g. "Argument 1 of HTMLMediaElement.fastSeek is not a finite floating-point value" instead of "Argument is not a finite floating-point value." This would require passing in more information to the conversion functions than we currently do.
34+
3335
## Background
3436

3537
What's actually going on here, conceptually, is pretty weird. Let's try to explain.

lib/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@ var conversions = {};
22
export default conversions;
33

44
var global_String = String;
5+
var Number_isFinite = Number.isFinite;
56

67
conversions["boolean"] = val => !!val;
8+
79
conversions["DOMString"] = val => global_String(val);
10+
11+
conversions["double"] = val => {
12+
var asNumber = +val;
13+
14+
if (!Number_isFinite(asNumber)) {
15+
throw new TypeError("Argument is not a finite floating-point value.");
16+
}
17+
18+
return asNumber;
19+
};

test/dom-string.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ describe("WebIDL DOMString type", () => {
3737
});
3838

3939
it("should throw a TypeError for a symbol", () => {
40-
assert.throws(() => {
41-
sut(new Symbol());
42-
}, TypeError);
40+
assert.throws(() => sut(new Symbol()), TypeError);
4341
});
4442

4543
it("should prefer toString to valueOf on objects", () => {

test/double.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
var assert = require("assert");
2+
3+
import conversions from "..";
4+
5+
// Adapted pretty directly from
6+
// https://github.com/marcoscaceres/webidl.js/blob/e631bcf2c1ba2d3ea283f5a39ed7bd1470743552/test/WebIDL.Double-tests.js
7+
8+
describe("WebIDL double type", () => {
9+
var sut = conversions["double"];
10+
11+
it("should return `0` for `0`", () => {
12+
assert.strictEqual(sut(0), 0);
13+
});
14+
15+
it("should return `42` for `42`", () => {
16+
assert.strictEqual(sut(42), 42);
17+
});
18+
19+
it("should return `0` for `null`", () => {
20+
assert.strictEqual(sut(null), 0);
21+
});
22+
23+
it("should return `0` for `\"\"`", () => {
24+
assert.strictEqual(sut(""), 0);
25+
});
26+
27+
it("should return `0` for `false`", () => {
28+
assert.strictEqual(sut(0), 0);
29+
});
30+
31+
it("should return `1` for `true`", () => {
32+
assert.strictEqual(sut(null), 0);
33+
});
34+
35+
it("should return `0` for random whitespace", () => {
36+
assert.strictEqual(sut(" \t\n\t "), 0);
37+
});
38+
39+
it("should return `123` for `\" 123 \"`", () => {
40+
assert.strictEqual(sut(" 123 "), 123);
41+
});
42+
43+
it("should return `-123.123` for `\" -123.123 \"`", () => {
44+
assert.strictEqual(sut(" -123.123 "), -123.123);
45+
});
46+
47+
it("should throw a TypeError for no argument", () => {
48+
assert.throws(() => sut(), TypeError);
49+
});
50+
51+
it("should throw a TypeError for `undefined`", () => {
52+
assert.throws(() => sut(undefined), TypeError);
53+
});
54+
55+
it("should throw a TypeError for `NaN`", () => {
56+
assert.throws(() => sut(NaN), TypeError);
57+
});
58+
59+
it("should throw a TypeError for `+Infinity`", () => {
60+
assert.throws(() => sut(+Infinity), TypeError);
61+
});
62+
63+
it("should throw a TypeError for `-Infinity`", () => {
64+
assert.throws(() => sut(-Infinity), TypeError);
65+
});
66+
67+
it("should throw a TypeError for `\" 123,123 \"` (since it becomes `NaN`)", () => {
68+
assert.throws(() => sut(" 123,123 "), TypeError);
69+
});
70+
});

0 commit comments

Comments
 (0)