Skip to content

Commit e0683cc

Browse files
committed
Symbol module
1 parent 77ec8b4 commit e0683cc

File tree

5 files changed

+75
-15
lines changed

5 files changed

+75
-15
lines changed

CHANGELOG.md

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

33
## v1.0.0 - 2025-01-03
44

5+
- The symbol type and functions have been moved to a new `symbol` module.
56
- Removed the `raceN` and `awaitN` functions from the `promise` module.
67
- Removed the `map` module.
78
- Removed the `type_of` function and `TypeOf` type from the `javascript` module.

src/gleam/javascript.gleam

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

src/gleam/javascript/symbol.gleam

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/// Symbols are special unique values in JavaScript.
2+
///
3+
/// For further information view the MDN documentation:
4+
/// <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol>
5+
///
6+
pub type Symbol
7+
8+
/// Creates a new symbol with the given description.
9+
///
10+
/// Symbols created with this function are "local", they are not in the global
11+
/// symbol registry.
12+
///
13+
/// For further information see the MDN documentation:
14+
/// <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/Symbol>
15+
///
16+
@external(javascript, "../../gleam_javascript_ffi.mjs", "new_symbol")
17+
pub fn new(description: String) -> Symbol
18+
19+
/// Returns the symbol for the given key from the global symbol registry,
20+
/// creating and registering a new one if one did not already exist.
21+
///
22+
/// Uses the JavaScript `Symbol.for` internally.
23+
///
24+
/// For further information see the MDN documentation:
25+
/// <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/for>
26+
///
27+
@external(javascript, "../../gleam_javascript_ffi.mjs", "get_symbol")
28+
pub fn get_or_create_global(key: String) -> Symbol
29+
30+
/// Get the description of the symbol, if it has one.
31+
///
32+
/// # Examples
33+
///
34+
/// ```gleam
35+
/// symbol.new("wibble")
36+
/// |> symbol.description
37+
/// // -> Ok("wibble")
38+
/// ```
39+
///
40+
@external(javascript, "../../gleam_javascript_ffi.mjs", "symbol_description")
41+
pub fn description(symbol: Symbol) -> Result(String, Nil)

src/gleam_javascript_ffi.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,17 @@ export function object_from_entries(entries) {
2727
return Object.fromEntries(entries);
2828
}
2929

30+
export function new_symbol(name) {
31+
return Symbol(name);
32+
}
3033
export function get_symbol(name) {
3134
return Symbol.for(name);
3235
}
36+
export function symbol_description(symbol) {
37+
const description = symbol.description;
38+
if (symbol.description === undefined) return new Error(undefined);
39+
return new Ok(description);
40+
}
3341

3442
// A wrapper around a promise to prevent `Promise<Promise<T>>` collapsing into
3543
// `Promise<T>`.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import gleam/javascript/symbol
2+
import gleeunit/should
3+
4+
pub fn new_test() {
5+
let name = "same name"
6+
7+
symbol.new(name)
8+
|> should.not_equal(symbol.new(name))
9+
}
10+
11+
pub fn get_or_create_global_test() {
12+
let name = "same name"
13+
14+
symbol.get_or_create_global(name)
15+
|> should.equal(symbol.get_or_create_global(name))
16+
17+
symbol.get_or_create_global(name)
18+
|> should.not_equal(symbol.new(name))
19+
}
20+
21+
pub fn description_test() {
22+
symbol.new("wibble")
23+
|> symbol.description
24+
|> should.equal(Ok("wibble"))
25+
}

0 commit comments

Comments
 (0)