Skip to content

Commit

Permalink
feat(LocalStoragePlus): 添加静态方法支持
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Jan 24, 2022
1 parent 6eb4138 commit 0c20f39
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 14 deletions.
17 changes: 17 additions & 0 deletions src/utils/LocalStoragePlus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,21 @@ describe('LocalStoragePlus', () => {
}),
).toBe(null)
})

test('静态方法正常', () => {
expect(LocalStoragePlus.get<number>('s')).toBe(null)
expect(LocalStoragePlus.has('s')).toBe(false)
LocalStoragePlus.set<number>('s', 1)
expect(LocalStoragePlus.has('s')).toBe(true)
expect(LocalStoragePlus.get<number>('s')).toBe(1)
LocalStoragePlus.set<number>('s', s => (s || 0) + 2)
expect(LocalStoragePlus.get<number>('s')).toBe(3)
LocalStoragePlus.remove('s')
expect(LocalStoragePlus.has('s')).toBe(false)
LocalStoragePlus.set<number>('s', s => (s || 0) + 2)
expect(LocalStoragePlus.get<number>('s')).toBe(2)
LocalStoragePlus.clear()
expect(LocalStoragePlus.has('s')).toBe(false)
expect(LocalStoragePlus.get<number>('s')).toBe(null)
})
})
95 changes: 81 additions & 14 deletions src/utils/LocalStoragePlus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,38 @@ interface LocalStoragePlusRawData<T> {
* 本地存储增强。
*/
export class LocalStoragePlus<T extends JsonValue> {
private static prefix = 'plus:'

private key: string
/**
* 本地存储键名前缀。
*/
private static prefix = 'LSP:'

constructor(private options: LocalStoragePlusOptions) {
this.key = `${LocalStoragePlus.prefix}${options.key}`
/**
* 获取完全的键名。
*
* @param key 键名
*/
private static getFullKey(key: string): string {
return `${this.prefix}${key}`
}

/**
* 设置值
* 设置本地存储
*
* @param key 键
* @param value 值
* @param options 选项
*/
set(
static set<T extends JsonValue>(
key: string,
value: T | ((prevValue: T | null) => T),
options?: LocalStoragePlusSetOptions,
): void {
const { ttl, tag } = options || {}
const expiredAt = ttl && Date.now() + ttl
const nextValue =
typeof value === 'function' ? value(this.get({ tag })) : value
typeof value === 'function' ? value(this.get<T>(key, { tag })) : value
localStorage.setItem(
this.key,
this.getFullKey(key),
JSON.stringify({
v: nextValue,
e: expiredAt,
Expand All @@ -66,15 +74,19 @@ export class LocalStoragePlus<T extends JsonValue> {
}

/**
* 获取值
* 获取本地存储
*
* @param key 键
* @param options 选项
*/
get(options?: LocalStoragePlusGetOptions): T | null {
static get<T extends JsonValue>(
key: string,
options?: LocalStoragePlusGetOptions,
): T | null {
const { tag } = options || {}

try {
const rawText = localStorage.getItem(this.key)
const rawText = localStorage.getItem(this.getFullKey(key))
if (rawText != null) {
const rawData = JSON.parse(rawText) as LocalStoragePlusRawData<T>

Expand All @@ -92,19 +104,74 @@ export class LocalStoragePlus<T extends JsonValue> {
return null
}

/**
* 是否存在本地存储。
*
* @param key 键
* @param options 选项
*/
static has(key: string, options?: LocalStoragePlusGetOptions): boolean {
return this.get(key, options) !== null
}

/**
* 删除本地存储。
*
* @param key 键
*/
static remove(key: string): void {
localStorage.removeItem(this.getFullKey(key))
}

/**
* 清空本地存储。
*/
static clear(): void {
for (let i = 0, len = localStorage.length; i < len; i++) {
const key = localStorage.key(i)
if (key != null && key.indexOf(this.prefix) === 0) {
localStorage.removeItem(key)
}
}
}

constructor(private options: LocalStoragePlusOptions) {}

/**
* 设置值。
*
* @param value 值
* @param options 选项
*/
set(
value: T | ((prevValue: T | null) => T),
options?: LocalStoragePlusSetOptions,
): void {
return LocalStoragePlus.set<T>(this.options.key, value, options)
}

/**
* 获取值。
*
* @param options 选项
*/
get(options?: LocalStoragePlusGetOptions): T | null {
return LocalStoragePlus.get(this.options.key, options)
}

/**
* 是否存在值。
*
* @param options 选项
*/
has(options?: LocalStoragePlusGetOptions): boolean {
return this.get(options) !== null
return LocalStoragePlus.has(this.options.key, options)
}

/**
* 删除值。
*/
remove(): void {
localStorage.removeItem(this.key)
return LocalStoragePlus.remove(this.options.key)
}
}

0 comments on commit 0c20f39

Please sign in to comment.