Skip to content

Commit

Permalink
feat(storage): add getRemember
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Nov 1, 2018
1 parent 7425f6f commit ec695f6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const storage = {
*
* @param key 键名
* @param value 键值,当为函数时,取函数执行后的返回值
* @param expire 过期时间,内部会使用 `toDate` 格式化
* @param [expire] 过期时间,内部会使用 `toDate` 格式化
*/
set(key: string, value: any, expire?: string | number | Date): void {
value = result(value)
Expand Down Expand Up @@ -75,6 +75,20 @@ const storage = {
}
},

/**
* 获取并设置本地存储的值。
*
* @param key 要获取和设置的键名
* @param defaultValue 获取时的默认值,当为函数时,取函数执行后的返回值
* @param [expire] 设置时的过期时间,内部会使用 `toDate` 格式化
* @returns 获取到的键值
*/
getRemember(key: string, defaultValue: any, expire?: string | number | Date): any {
const value = storage.get(key, defaultValue)
storage.set(key, value, expire)
return value
},

/**
* 移除本地存储的值。
*
Expand Down
4 changes: 4 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,10 @@ describe('storage', () => {
expect(vtils.storage.get('===')).toBe(120)
expect(vtils.storage.get('====', () => 110)).toBe(110)
})
test('remember', () => {
expect(vtils.storage.getRemember('jjj', () => 1)).toBe(1)
expect(vtils.storage.get('jjj')).toBe(1)
})
})

describe('randomString', () => {
Expand Down

0 comments on commit ec695f6

Please sign in to comment.