diff options
author | Louis Pilfold <louis@lpil.uk> | 2021-08-29 16:15:15 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2021-08-29 16:15:15 +0100 |
commit | 61fd456ecf2763202df299ab92b09adffe2c2c2d (patch) | |
tree | 796b113b4b800a92fb94c8db7ea13bc89b600584 | |
parent | d7cf6c9a27487b182c1e6d832a58e01d022b97fa (diff) | |
download | javascript-61fd456ecf2763202df299ab92b09adffe2c2c2d.tar.gz javascript-61fd456ecf2763202df299ab92b09adffe2c2c2d.zip |
JavaScript module
-rw-r--r-- | CHANGELOG.md | 7 | ||||
-rw-r--r-- | src/ffi.js | 41 | ||||
-rw-r--r-- | src/gleam/javascript.gleam | 19 | ||||
-rw-r--r-- | test/gleam/javascript_test.gleam | 25 |
4 files changed, 90 insertions, 2 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c049cb..fe8e251 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,5 +2,8 @@ ## Unreleased -- The `array` module was created with the `Array(element)` type and `to_list`, - `from_list`, `map`, `fold`, `fold_right`, `get`, and `length` functions. +- The `javascript/array` module was created with the `Array(element)` type and + `to_list`, `from_list`, `map`, `fold`, `fold_right`, `get`, and `length` + functions. +- The `javascript` module was created with the `TypeOf`, and `Symbol` types, and + `find_symbol`, and `type_of` functions. @@ -1,4 +1,14 @@ import { Ok, Error } from "./gleam.js"; +import { + UndefinedType, + ObjectType, + BooleanType, + NumberType, + BigIntType, + StringType, + SymbolType, + FunctionType, +} from "./gleam/javascript.js"; export function toArray(list) { return list.toArray(); @@ -23,3 +33,34 @@ export function reduceRight(thing, acc, fn) { export function index(thing, index) { return index in thing ? new Ok(thing[index]) : new Error(undefined); } + +export function object_from_entries(entries) { + return Object.fromEntries(entries); +} + +export function type_of(value) { + switch (typeof value) { + case "undefined": + return new UndefinedType(); + case "object": + return new ObjectType(); + case "boolean": + return new BooleanType(); + case "number": + return new NumberType(); + case "bigint": + return new BigIntType(); + case "string": + return new StringType(); + case "symbol": + return new SymbolType(); + case "function": + return new FunctionType(); + default: + throw new globalThis.Error(`Unexpected typeof ${typeof value}`); + } +} + +export function get_symbol(name) { + return Symbol.for(name); +} diff --git a/src/gleam/javascript.gleam b/src/gleam/javascript.gleam new file mode 100644 index 0000000..590dece --- /dev/null +++ b/src/gleam/javascript.gleam @@ -0,0 +1,19 @@ +// TODO: docs +pub type TypeOf { + UndefinedType + ObjectType + BooleanType + NumberType + BigIntType + StringType + SymbolType + FunctionType +} + +pub external type Symbol + +pub external fn type_of(value) -> TypeOf = + "../ffi.js" "type_of" + +pub external fn get_symbol(String) -> Symbol = + "../ffi.js" "get_symbol" diff --git a/test/gleam/javascript_test.gleam b/test/gleam/javascript_test.gleam new file mode 100644 index 0000000..81477d8 --- /dev/null +++ b/test/gleam/javascript_test.gleam @@ -0,0 +1,25 @@ +import gleam/javascript.{ + BooleanType, FunctionType, NumberType, ObjectType, StringType, SymbolType, UndefinedType, +} + +pub fn type_of_test() { + assert UndefinedType = javascript.type_of(Nil) + assert NumberType = javascript.type_of(1) + assert NumberType = javascript.type_of(1.1) + assert BooleanType = javascript.type_of(True) + assert BooleanType = javascript.type_of(False) + assert StringType = javascript.type_of("ok") + assert StringType = javascript.type_of("") + assert FunctionType = javascript.type_of(fn() { 1 }) + assert FunctionType = javascript.type_of(fn(x) { x }) + assert FunctionType = javascript.type_of(type_of_test) + assert FunctionType = javascript.type_of(Ok) + assert ObjectType = javascript.type_of(Ok(1)) + assert ObjectType = javascript.type_of(Error("ok")) + assert SymbolType = javascript.type_of(javascript.get_symbol("Gleam")) +} + +pub fn find_symbol_test() { + assert True = javascript.get_symbol("Gleam") == javascript.get_symbol("Gleam") + assert False = javascript.get_symbol("Gleam") == javascript.get_symbol("Lua") +} |