Skip to content

Commit 77ec8b4

Browse files
committed
Remove type_of
1 parent a97497d commit 77ec8b4

File tree

7 files changed

+74
-101
lines changed

7 files changed

+74
-101
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Removed the `raceN` and `awaitN` functions from the `promise` module.
66
- Removed the `map` module.
7+
- Removed the `type_of` function and `TypeOf` type from the `javascript` module.
78

89
## v0.13.0 - 2024-08-30
910

src/gleam/javascript.gleam

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,10 @@
1-
/// The variants that `type_of` can return.
2-
pub type TypeOf {
3-
/// It is the value `undefined`.
4-
UndefinedType
5-
/// It is some object, or it is `null`.
6-
ObjectType
7-
/// It is either `true` or `false`.
8-
BooleanType
9-
/// It is some number, a 64 bit float.
10-
NumberType
11-
/// It is a JavaScript big-integer.
12-
BigIntType
13-
/// It is a string.
14-
StringType
15-
/// It is a JavaScript symbol.
16-
SymbolType
17-
/// It is a function of unknown argument types and return type.
18-
FunctionType
19-
}
20-
211
/// Symbols are special unique values in JavaScript.
222
///
233
/// For further information view the MDN documentation:
244
/// <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol>
255
///
266
pub type Symbol
277

28-
/// Determine what category of JavaScript type a value belongs to.
29-
///
30-
/// This uses the JavaScript `typeof` operator and has limited accuracy. It
31-
/// cannot tell you anything about what Gleam type a value has.
32-
///
33-
/// For further information view the MDN documentation:
34-
/// <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol>
35-
///
36-
@external(javascript, "../gleam_javascript_ffi.mjs", "type_of")
37-
pub fn type_of(a: value) -> TypeOf
38-
398
/// Use the JavaScript `Symbol.for` method to look up a symbol with the given
409
/// string name, creating a new one if one does exist.
4110
///

src/gleam_javascript_ffi.mjs

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,4 @@
11
import { Ok, Error } from "./gleam.mjs";
2-
import {
3-
UndefinedType,
4-
ObjectType,
5-
BooleanType,
6-
NumberType,
7-
BigIntType,
8-
StringType,
9-
SymbolType,
10-
FunctionType,
11-
} from "./gleam/javascript.mjs";
12-
132
export function toArray(list) {
143
return list.toArray();
154
}
@@ -38,29 +27,6 @@ export function object_from_entries(entries) {
3827
return Object.fromEntries(entries);
3928
}
4029

41-
export function type_of(value) {
42-
switch (typeof value) {
43-
case "undefined":
44-
return new UndefinedType();
45-
case "object":
46-
return new ObjectType();
47-
case "boolean":
48-
return new BooleanType();
49-
case "number":
50-
return new NumberType();
51-
case "bigint":
52-
return new BigIntType();
53-
case "string":
54-
return new StringType();
55-
case "symbol":
56-
return new SymbolType();
57-
case "function":
58-
return new FunctionType();
59-
default:
60-
throw new globalThis.Error(`Unexpected typeof ${typeof value}`);
61-
}
62-
}
63-
6430
export function get_symbol(name) {
6531
return Symbol.for(name);
6632
}
@@ -92,9 +58,11 @@ export function newPromise(executor) {
9258
export function start_promise() {
9359
let resolve;
9460
const promise = new Promise((r) => {
95-
resolve = (value) => {r(PromiseLayer.wrap(value))}
96-
})
97-
return [promise, resolve]
61+
resolve = (value) => {
62+
r(PromiseLayer.wrap(value));
63+
};
64+
});
65+
return [promise, resolve];
9866
}
9967

10068
export function resolve(value) {
@@ -118,7 +86,7 @@ export function rescue(promise, fn) {
11886
export function wait(delay) {
11987
return new Promise((resolve) => {
12088
globalThis.setTimeout(resolve, delay);
121-
})
89+
});
12290
}
12391

12492
export function all_promises(...promises) {

test/gleam/javascript/promise_test.gleam

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import gleam/javascript.{ObjectType}
21
import gleam/javascript/array
32
import gleam/javascript/promise.{type Promise}
3+
import helper.{ObjectType}
44

55
pub fn new_promise_test() {
66
promise.new(fn(resolve) { resolve(1) })
@@ -15,7 +15,7 @@ pub fn new_does_not_collapse_nested_promise_test() {
1515
// If the `Promise(Promise(Int))` collapsed into `Promise(Int)` (as they
1616
// do for normal JS promises) then this would fail as the value would be the
1717
// int value `1`.
18-
let assert ObjectType = javascript.type_of(value)
18+
let assert ObjectType = helper.type_of(value)
1919
})
2020
}
2121

@@ -33,7 +33,7 @@ pub fn start_does_not_collapse_nested_promise_test() {
3333
// If the `Promise(Promise(Int))` collapsed into `Promise(Int)` (as they
3434
// do for normal JS promises) then this would fail as the value would be the
3535
// int value `1`.
36-
let assert ObjectType = javascript.type_of(value)
36+
let assert ObjectType = helper.type_of(value)
3737
})
3838
resolve(promise.resolve(1))
3939
}
@@ -45,7 +45,7 @@ pub fn map_does_not_collapse_nested_promise_test() -> Promise(Promise(Int)) {
4545
// If the `Promise(Promise(Int))` collapsed into `Promise(Int)` (as they
4646
// do for normal JS promises) then this would fail as the value would be the
4747
// int value `1`.
48-
let assert ObjectType = javascript.type_of(value)
48+
let assert ObjectType = helper.type_of(value)
4949
})
5050
}
5151

test/gleam/javascript_test.gleam

Lines changed: 0 additions & 28 deletions
This file was deleted.

test/gleam_javascript_test_ffi.mjs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import {
2+
UndefinedType,
3+
ObjectType,
4+
BooleanType,
5+
NumberType,
6+
BigIntType,
7+
StringType,
8+
SymbolType,
9+
FunctionType,
10+
} from "./helper.mjs";
11+
12+
export function type_of(value) {
13+
switch (typeof value) {
14+
case "undefined":
15+
return new UndefinedType();
16+
case "object":
17+
return new ObjectType();
18+
case "boolean":
19+
return new BooleanType();
20+
case "number":
21+
return new NumberType();
22+
case "bigint":
23+
return new BigIntType();
24+
case "string":
25+
return new StringType();
26+
case "symbol":
27+
return new SymbolType();
28+
case "function":
29+
return new FunctionType();
30+
default:
31+
throw new globalThis.Error(`Unexpected typeof ${typeof value}`);
32+
}
33+
}

test/helper.gleam

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/// The variants that `type_of` can return.
2+
pub type TypeOf {
3+
/// It is the value `undefined`.
4+
UndefinedType
5+
/// It is some object, or it is `null`.
6+
ObjectType
7+
/// It is either `true` or `false`.
8+
BooleanType
9+
/// It is some number, a 64 bit float.
10+
NumberType
11+
/// It is a JavaScript big-integer.
12+
BigIntType
13+
/// It is a string.
14+
StringType
15+
/// It is a JavaScript symbol.
16+
SymbolType
17+
/// It is a function of unknown argument types and return type.
18+
FunctionType
19+
}
20+
21+
/// Determine what category of JavaScript type a value belongs to.
22+
///
23+
/// This uses the JavaScript `typeof` operator and has limited accuracy. It
24+
/// cannot tell you anything about what Gleam type a value has.
25+
///
26+
/// For further information view the MDN documentation:
27+
/// <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol>
28+
///
29+
@external(javascript, "./gleam_javascript_test_ffi.mjs", "type_of")
30+
pub fn type_of(a: value) -> TypeOf

0 commit comments

Comments
 (0)