From b0d576f0e9c369191d59b0dfd8ac558f1143d06c Mon Sep 17 00:00:00 2001 From: Steven Lin Date: Thu, 2 Jan 2025 22:28:00 +0800 Subject: [PATCH] Fix bindings in `Storage` --- src/WebStorageAPI/Storage.res | 4 ++-- tests/WebStorageAPI/Storage__test.js | 20 ++++++++++++++++++++ tests/WebStorageAPI/Storage__test.res | 14 ++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 tests/WebStorageAPI/Storage__test.js create mode 100644 tests/WebStorageAPI/Storage__test.res diff --git a/src/WebStorageAPI/Storage.res b/src/WebStorageAPI/Storage.res index e0778a4..17765bb 100644 --- a/src/WebStorageAPI/Storage.res +++ b/src/WebStorageAPI/Storage.res @@ -5,14 +5,14 @@ Returns the name of the nth key, or null if n is greater than or equal to the nu [Read more on MDN](https://developer.mozilla.org/docs/Web/API/Storage/key) */ @send -external key: (storage, int) => string = "key" +external key: (storage, int) => Null.t = "key" /** Returns the current value associated with the given key, or null if the given key does not exist. [Read more on MDN](https://developer.mozilla.org/docs/Web/API/Storage/getItem) */ @send -external getItem: (storage, string) => string = "getItem" +external getItem: (storage, string) => Null.t = "getItem" /** Sets the value of the pair identified by key to value, creating a new key/value pair if none existed for key previously. diff --git a/tests/WebStorageAPI/Storage__test.js b/tests/WebStorageAPI/Storage__test.js new file mode 100644 index 0000000..ca08ebb --- /dev/null +++ b/tests/WebStorageAPI/Storage__test.js @@ -0,0 +1,20 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + +import * as Null from "rescript/lib/es6/Null.js"; + +for (let i = 0, i_finish = localStorage.length; i < i_finish; ++i) { + console.log(Null.getOr(localStorage.key(i), "nothing")); +} + +let item1 = Null.getOr(localStorage.getItem("foo"), "nothing"); + +localStorage.setItem("bar", "..."); + +localStorage.removeItem("bar"); + +localStorage.clear(); + +export { + item1, +} +/* Not a pure module */ diff --git a/tests/WebStorageAPI/Storage__test.res b/tests/WebStorageAPI/Storage__test.res new file mode 100644 index 0000000..4a7d2dc --- /dev/null +++ b/tests/WebStorageAPI/Storage__test.res @@ -0,0 +1,14 @@ +open WebAPI.Global +open WebAPI.Storage + +for i in 0 to localStorage.length - 1 { + localStorage->key(i)->Null.getOr("nothing")->Console.log +} + +let item1 = localStorage->getItem("foo")->Null.getOr("nothing") + +localStorage->setItem(~key="bar", ~value="...") + +localStorage->removeItem("bar") + +localStorage->clear