forked from keroxp/servest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_holder.ts
40 lines (39 loc) · 1023 Bytes
/
data_holder.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Copyright 2019-2020 Yusuke Sakurai. All rights reserved. MIT license.
type Key = string | symbol;
export interface DataHolder {
set<T = unknown>(key: Key, value: T): void;
get<T = unknown>(key: Key): T | undefined;
getString(key: Key): string | undefined;
getNumber(key: Key): number | undefined;
getBoolean(key: Key): boolean | undefined;
delete(key: Key): void;
}
export function createDataHolder(): DataHolder {
const data = new Map<Key, any>();
function _set<T>(k: Key, v: T) {
data.set(k, v);
}
function _get<T>(k: Key): T | undefined {
return data.get(k) as T;
}
function _delete(k: Key) {
data.delete(k);
}
function getString(k: Key): string | undefined {
return _get<string>(k);
}
function getNumber(k: Key): number | undefined {
return _get<number>(k);
}
function getBoolean(k: Key): boolean | undefined {
return _get<boolean>(k);
}
return {
set: _set,
get: _get,
delete: _delete,
getNumber,
getString,
getBoolean,
};
}