Skip to content
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## v1.0.0 - 2025-01-03

- The symbol type and functions have been moved to a new `symbol` module.
- Removed the `raceN` and `awaitN` functions from the `promise` module.
- Removed the `map` module.
- Removed the `type_of` function and `TypeOf` type from the `javascript` module.
Expand Down
15 changes: 0 additions & 15 deletions src/gleam/javascript.gleam

This file was deleted.

41 changes: 41 additions & 0 deletions src/gleam/javascript/symbol.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/// Symbols are special unique values in JavaScript.
///
/// For further information view the MDN documentation:
/// <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol>
///
pub type Symbol

/// Creates a new symbol with the given description.
///
/// Symbols created with this function are "local", they are not in the global
/// symbol registry.
///
/// For further information see the MDN documentation:
/// <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/Symbol>
///
@external(javascript, "../../gleam_javascript_ffi.mjs", "new_symbol")
pub fn new(description: String) -> Symbol

/// Returns the symbol for the given key from the global symbol registry,
/// creating and registering a new one if one did not already exist.
///
/// Uses the JavaScript `Symbol.for` internally.
///
/// For further information see the MDN documentation:
/// <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/for>
///
@external(javascript, "../../gleam_javascript_ffi.mjs", "get_symbol")
pub fn get_or_create_global(key: String) -> Symbol

/// Get the description of the symbol, if it has one.
///
/// # Examples
///
/// ```gleam
/// symbol.new("wibble")
/// |> symbol.description
/// // -> Ok("wibble")
/// ```
///
@external(javascript, "../../gleam_javascript_ffi.mjs", "symbol_description")
pub fn description(symbol: Symbol) -> Result(String, Nil)
8 changes: 8 additions & 0 deletions src/gleam_javascript_ffi.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,17 @@ export function object_from_entries(entries) {
return Object.fromEntries(entries);
}

export function new_symbol(name) {
return Symbol(name);
}
export function get_symbol(name) {
return Symbol.for(name);
}
export function symbol_description(symbol) {
const description = symbol.description;
if (symbol.description === undefined) return new Error(undefined);
return new Ok(description);
}

// A wrapper around a promise to prevent `Promise<Promise<T>>` collapsing into
// `Promise<T>`.
Expand Down
25 changes: 25 additions & 0 deletions test/gleam/javascript/symbol_test.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import gleam/javascript/symbol
import gleeunit/should

pub fn new_test() {
let name = "same name"

symbol.new(name)
|> should.not_equal(symbol.new(name))
}

pub fn get_or_create_global_test() {
let name = "same name"

symbol.get_or_create_global(name)
|> should.equal(symbol.get_or_create_global(name))

symbol.get_or_create_global(name)
|> should.not_equal(symbol.new(name))
}

pub fn description_test() {
symbol.new("wibble")
|> symbol.description
|> should.equal(Ok("wibble"))
}