Browse Source

Renamed the `WebStorage.has()` method to `exists()`

tags/v1.0.0
Cédric Belin 1 month ago
parent
commit
3c3ce7de19
6 changed files with 63 additions and 55 deletions
  1. +38
    -30
      etc/mkdocs.yaml
  2. +1
    -1
      example/Main.hx
  3. +3
    -3
      readthedocs.yaml
  4. +5
    -5
      src/webstorage/WebStorage.hx
  5. +1
    -1
      test/TestAll.hx
  6. +15
    -15
      test/webstorage/WebStorageTest.hx

+ 38
- 30
etc/mkdocs.yaml View File

@@ -12,39 +12,47 @@ edit_uri: ""

copyright: Copyright © 2020 Cédric Belin
extra:
social:
- icon: fontawesome/solid/globe
link: "https://belin.io"
- icon: fontawesome/brands/github
link: "https://github.com/cedx"
- icon: fontawesome/brands/twitter
link: "https://twitter.com/cedxbelin"
- icon: fontawesome/brands/linkedin
link: "https://linkedin.com/in/cedxbelin"
social:
- icon: fontawesome/solid/globe
link: "https://belin.io"
name: Belin.io
- icon: fontawesome/brands/github
link: "https://github.com/cedx"
name: GitHub
- icon: fontawesome/brands/twitter
link: "https://twitter.com/cedxbelin"
name: Twitter
- icon: fontawesome/brands/linkedin
link: "https://linkedin.com/in/cedxbelin"
name: LinkedIn

markdown_extensions:
- admonition
- codehilite
- meta
- toc:
permalink: true
- admonition
- codehilite
- meta
- pymdownx.tabbed
- toc:
permalink: true

nav:
- Overview: index.md
- Installation: installation.md
- Usage: usage.md
- About:
- License: about/license.md
- See also: about/see_also.md
- Overview: index.md
- Installation: installation.md
- Usage:
- Programming interface: usage/api.md
- Iteration: usage/iteration.md
- Events: usage/events.md
- About:
- License: about/license.md
- See also: about/see_also.md

theme:
favicon: img/favicon.ico
features:
- instant
icon:
logo: fontawesome/solid/book-reader
repo: fontawesome/brands/git-alt
name: material
palette:
accent: amber
primary: amber
favicon: img/favicon.ico
features:
- instant
icon:
logo: fontawesome/solid/book-reader
repo: fontawesome/brands/git-alt
name: material
palette:
accent: amber
primary: amber

+ 1
- 1
example/Main.hx View File

@@ -6,7 +6,7 @@ import webstorage.LocalStorage;
class Main {
/** Application entry point. **/
public static function main(): Void {
static function main(): Void {
final service = new LocalStorage();

// Query the storage.


+ 3
- 3
readthedocs.yaml View File

@@ -1,6 +1,6 @@
version: 2
mkdocs:
configuration: etc/mkdocs.yaml
configuration: etc/mkdocs.yaml
python:
install:
- requirements: etc/requirements.txt
install:
- requirements: etc/requirements.txt

+ 5
- 5
src/webstorage/WebStorage.hx View File

@@ -57,6 +57,10 @@ class WebStorage extends EventTarget {
public function destroy(): Void
if (listener != null) removeEventListener("storage", listener);

/** Gets a value indicating whether this storage contains the specified `key`. **/
public function exists(key: String): Bool
return keys.indexOf(key) >= 0;

/**
Gets the value associated to the specified `key`.
Returns the given `defaultValue` if the item does not exist.
@@ -70,7 +74,7 @@ class WebStorage extends EventTarget {
Gets the deserialized value associated to the specified `key`.
Returns the given `defaultValue` if the item does not exist.
**/
public function getObject(key: String, ?defaultValue: Any): Null<Dynamic> {
public function getObject(key: String, ?defaultValue: Any): Dynamic {
try {
final value = get(key);
return value != null ? Json.parse(value) : defaultValue;
@@ -81,10 +85,6 @@ class WebStorage extends EventTarget {
}
}

/** Gets a value indicating whether this storage contains the specified `key`. **/
public function has(key: String): Bool
return keys.indexOf(key) >= 0;

/** Returns a new iterator that allows iterating the entries of this storage. **/
public function keyValueIterator(): KeyValueIterator<String, String>
return new WebStorageIterator(backend);


+ 1
- 1
test/TestAll.hx View File

@@ -7,7 +7,7 @@ import webstorage.*;
class TestAll {

/** Application entry point. **/
public static function main(): Void {
static function main(): Void {
Assert.results = new List();
describe("WebStorage", new WebStorageTest().run);
}


+ 15
- 15
test/webstorage/WebStorageTest.hx View File

@@ -22,9 +22,9 @@ class WebStorageTest {
describe(".length", testLength);
describe(".addEventListener('change')", testAddEventListener);
describe(".clear()", testLength);
describe(".exists()", testExists);
describe(".get()", testGet);
describe(".getObject()", testGetObject);
describe(".has()", testHas);
describe(".keyValueIterator()", testKeyValueIterator);
describe(".putIfAbsent()", testPutIfAbsent);
describe(".putObjectIfAbsent()", testPutObjectIfAbsent);
@@ -143,6 +143,20 @@ class WebStorageTest {
});
}

/** Tests the `exists()` method. **/
function testHas(): Void {
it("should return `false` if the specified key is not contained", () -> {
Assert.isFalse(new SessionStorage().exists("foo"));
});

it("should return `true` if the specified key is contained", () -> {
final service = new SessionStorage();
sessionStorage.setItem("foo", "bar");
Assert.isTrue(service.exists("foo"));
Assert.isFalse(service.exists("bar"));
});
}

/** Tests the `get()` method. **/
function testGet(): Void {
it("should properly get the storage entries", () -> {
@@ -183,20 +197,6 @@ class WebStorageTest {
});
}

/** Tests the `has()` method. **/
function testHas(): Void {
it("should return `false` if the specified key is not contained", () -> {
Assert.isFalse(new SessionStorage().has("foo"));
});

it("should return `true` if the specified key is contained", () -> {
final service = new SessionStorage();
sessionStorage.setItem("foo", "bar");
Assert.isTrue(service.has("foo"));
Assert.isFalse(service.has("bar"));
});
}

/** Tests the `keyValueIterator()` method. **/
function testKeyValueIterator(): Void {
it("should end iteration immediately if storage is empty", () -> {


Loading…
Cancel
Save