Skip to content

Commit 861a82a

Browse files
Zephyr-BlessedZephyr-Blessed
andauthored
🤖 Merge PR DefinitelyTyped#74514 Add type definitions for memorystorage by @Zephyr-Blessed
Co-authored-by: Zephyr-Blessed <zephyr-blessed@users.noreply.github.com> Co-authored-by: Zephyr-Blessed <zephyr@aitruist.org>
1 parent a0ff3cf commit 861a82a

5 files changed

Lines changed: 142 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*
2+
!**/*.d.ts
3+
!**/*.d.cts
4+
!**/*.d.mts
5+
!**/*.d.*.ts
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* A memory-backed implementation of the Web Storage API.
3+
*
4+
* @see https://github.com/download/memorystorage
5+
*/
6+
7+
interface MemoryStorage {
8+
/** The identifier for this storage instance. */
9+
readonly id: string;
10+
11+
/** The number of key/value pairs currently present in the storage. */
12+
readonly length: number;
13+
14+
/**
15+
* Returns the value associated with the given key, or `null` if the key does not exist.
16+
* @param key The name of the key to retrieve the value of.
17+
*/
18+
getItem(key: string): string | null;
19+
20+
/**
21+
* Sets the value of the pair identified by key to value, creating a new key/value pair if none existed for key previously.
22+
* @param key The name of the key to create/update.
23+
* @param value The value to set.
24+
*/
25+
setItem(key: string, value: string): void;
26+
27+
/**
28+
* Removes the key/value pair with the given key, if a key/value pair with the given key exists.
29+
* @param key The name of the key to remove.
30+
*/
31+
removeItem(key: string): void;
32+
33+
/**
34+
* Returns the name of the nth key, or `null` if n is greater than or equal to the number of key/value pairs.
35+
* @param index The index of the key to return.
36+
*/
37+
key(index: number): string | null;
38+
39+
/**
40+
* Removes all key/value pairs, if there are any.
41+
*/
42+
clear(): void;
43+
44+
/** Index signature to allow accessing stored items as properties. */
45+
[key: string]: any;
46+
}
47+
48+
interface MemoryStorageConstructor {
49+
/**
50+
* Creates a new MemoryStorage object implementing the Web Storage API using memory.
51+
*
52+
* If no arguments are given, the created memory storage object will read from and write to the
53+
* `global` memory storage. If a string argument is given, the new storage object will read from
54+
* and write to its own segment of memory.
55+
*
56+
* Can be called with or without `new`.
57+
*
58+
* @param id Optional string argument used to isolate this memory storage object from others.
59+
*/
60+
new(id?: string): MemoryStorage;
61+
(id?: string): MemoryStorage;
62+
}
63+
64+
declare const MemoryStorage: MemoryStorageConstructor;
65+
66+
export = MemoryStorage;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import MemoryStorage = require("memorystorage");
2+
3+
// Can be called with new
4+
const storage1 = new MemoryStorage("test-1");
5+
6+
// Can be called without new
7+
const storage2 = MemoryStorage("test-2");
8+
9+
// Can be called with no arguments (uses global)
10+
const globalStorage = new MemoryStorage();
11+
12+
// $ExpectType string
13+
storage1.id;
14+
15+
// $ExpectType number
16+
storage1.length;
17+
18+
// $ExpectType string | null
19+
storage1.getItem("key");
20+
21+
// $ExpectType void
22+
storage1.setItem("key", "value");
23+
24+
// $ExpectType void
25+
storage1.removeItem("key");
26+
27+
// $ExpectType string | null
28+
storage1.key(0);
29+
30+
// $ExpectType void
31+
storage1.clear();
32+
33+
// Can set/get properties directly via index signature
34+
storage1["myKey"] = "myValue";
35+
const val: any = storage1["myKey"];
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"private": true,
3+
"name": "@types/memorystorage",
4+
"version": "0.12.9999",
5+
"projects": [
6+
"https://github.com/download/memorystorage"
7+
],
8+
"devDependencies": {
9+
"@types/memorystorage": "workspace:."
10+
},
11+
"owners": [
12+
{
13+
"name": "Zephyr-Blessed",
14+
"githubUsername": "Zephyr-Blessed"
15+
}
16+
]
17+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"compilerOptions": {
3+
"module": "node16",
4+
"lib": [
5+
"es6"
6+
],
7+
"noImplicitAny": true,
8+
"noImplicitThis": true,
9+
"strictNullChecks": true,
10+
"strictFunctionTypes": true,
11+
"types": [],
12+
"noEmit": true,
13+
"forceConsistentCasingInFileNames": true
14+
},
15+
"files": [
16+
"index.d.ts",
17+
"memorystorage-tests.ts"
18+
]
19+
}

0 commit comments

Comments
 (0)