Skip to content

Commit 22da2f2

Browse files
committed
chore: improve typing info
1 parent b94054c commit 22da2f2

4 files changed

Lines changed: 16 additions & 19 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/helpers.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import type { Dictionary } from './types';
22

3-
export const NOOP = (...args: unknown[]): unknown => undefined;
4-
5-
export const isObject = (item: any): boolean => typeof item === 'object' && item !== null && !Array.isArray(item);
3+
export const isObject = (item: unknown): boolean => typeof item === 'object' && item !== null && !Array.isArray(item);
64

75
/** Specific to local-storage */
86

src/ls.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* MIT License
55
*/
66

7-
import { isObject, NOOP, memoryStore } from './helpers';
7+
import { isObject, memoryStore } from './helpers';
88
import type { Encrypter, Decrypter, StorageConfig } from './types';
99

1010
// private fields
@@ -29,15 +29,14 @@ const init = () => {
2929
const APX = String.fromCharCode(0);
3030

3131
// tiny obsfuscator as a default implementation
32-
const encrypter: Encrypter | Decrypter = (str, key, encrypt = true) => {
33-
const s = encrypt ? JSON.stringify(str) : (str as string);
34-
const offset = encrypt ? (key as number) : -(key as number);
35-
let result = '';
36-
for (let i = 0; i < s.length; i++) result += String.fromCharCode(s.charCodeAt(i) + offset);
37-
return encrypt ? result : JSON.parse(result);
32+
const shift = (s: string, offset: number): string => {
33+
let r = '';
34+
for (let i = 0; i < s.length; i++) r += String.fromCharCode(s.charCodeAt(i) + offset);
35+
return r;
3836
};
3937

40-
const decrypter: Decrypter = (str, key) => encrypter(str, key, false);
38+
const encrypter: Encrypter = (str, key) => shift(JSON.stringify(str), key as number);
39+
const decrypter: Decrypter = (str, key) => JSON.parse(shift(str as string, -(key as number)));
4140

4241
const config: StorageConfig = {
4342
ttl: null,
@@ -61,12 +60,12 @@ const set = <T = unknown>(key: string, value: T, localConfig: Omit<StorageConfig
6160
let val = hasTTL ? { [APX]: value, ttl: Date.now() + (ttl as number) * 1e3 } : value;
6261

6362
if (encrypt) {
64-
const encrypterFn = localConfig.encrypter || config.encrypter || NOOP;
63+
const encrypterFn = (localConfig.encrypter || config.encrypter) as Encrypter;
6564
const secret = localConfig.secret ?? config.secret;
6665
if (hasTTL) {
67-
(val as Record<string, unknown>)[APX] = encrypterFn((val as Record<string, unknown>)[APX], secret) as string;
66+
(val as Record<string, unknown>)[APX] = encrypterFn((val as Record<string, unknown>)[APX], secret);
6867
} else {
69-
val = encrypterFn(val, secret) as T;
68+
val = encrypterFn(val, secret) as unknown as T;
7069
}
7170
}
7271

@@ -92,7 +91,7 @@ const get = <T = unknown>(key: string, localConfig: Omit<StorageConfig, 'storage
9291
hasTTL = isObject(item) && APX in item;
9392

9493
if (shouldDecrypt) {
95-
const decrypterFn = localConfig.decrypter || config.decrypter || NOOP;
94+
const decrypterFn = (localConfig.decrypter || config.decrypter) as Decrypter;
9695
const secret = localConfig.secret ?? config.secret;
9796
if (hasTTL) {
9897
item[APX] = decrypterFn(item[APX], secret) as string;
@@ -107,7 +106,7 @@ const get = <T = unknown>(key: string, localConfig: Omit<StorageConfig, 'storage
107106

108107
// if not using ttl, return immediately
109108
if (!hasTTL) {
110-
return item !== undefined ? item : str;
109+
return (item !== undefined ? item : str) as T | null;
111110
}
112111

113112
if (Date.now() > item.ttl) {

src/types.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export type Encrypter = (...args: unknown[]) => string;
2-
export type Decrypter = (...args: unknown[]) => string;
2+
export type Decrypter = (...args: unknown[]) => unknown;
33

44
export type Dictionary<T = unknown> = Record<string, T>;
55

0 commit comments

Comments
 (0)