Skip to content

Commit

Permalink
feat(storage): support function values
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Oct 31, 2018
1 parent 6242783 commit 163103b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/isFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
* @param value 要检查的值
* @returns 是(true)或者不是(false)一个函数
*/
export default function isFunction(value: any): boolean {
export default function isFunction(value: any): value is Function { // tslint:disable-line
return typeof value === 'function'
}
18 changes: 10 additions & 8 deletions src/storage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import inBrowser from './inBrowser'
import inWechatMiniProgram from './inWechatMiniProgram'
import noop from './noop'
import result from './result'

export interface StorageDriver {
get(key: string): any,
Expand Down Expand Up @@ -38,14 +39,14 @@ const storage = {
* 设置本地存储的值。
*
* @param key 键名
* @param value 键值
* @param value 键值,当为函数时,取函数执行后的返回值
* @param expire 过期时间
*/
set(key: string, value: any, expire?: Date): void {
if (value != null) {
storageDriver.set(key, JSON.stringify({
expire: expire && expire.getTime(),
value
value: result(value)
}))
}
},
Expand All @@ -54,18 +55,19 @@ const storage = {
* 获取本地存储的值。
*
* @param key 要获取的键名
* @param defaultValue 默认值,当为函数时,取函数执行后的返回值
* @returns 获取到的键值
*/
get(key: string): any {
get(key: string, defaultValue: any = null): any {
try {
const { expire, value = null } = JSON.parse(storageDriver.get(key))
const { expire, value = defaultValue } = JSON.parse(storageDriver.get(key))
if (expire && expire < new Date().getTime()) {
storage.remove(key)
return null
return result(defaultValue)
}
return value
} catch (_) {
return null
return result(value)
} catch (err) {
return result(defaultValue)
}
},

Expand Down
35 changes: 35 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,23 @@ describe('storage', () => {
done()
}, 1001)
})
test('get 可设置默认值', () => {
[
vtils.randomString(),
vtils.randomString(),
vtils.randomString(),
vtils.randomString(),
vtils.randomString()
].forEach(item => {
expect(vtils.storage.get(item)).toBeNull()
expect(vtils.storage.get(item, item)).toBe(item)
})
})
test('值支持函数返回值', () => {
vtils.storage.set('===', () => 120)
expect(vtils.storage.get('===')).toBe(120)
expect(vtils.storage.get('====', () => 110)).toBe(110)
})
})

describe('randomString', () => {
Expand All @@ -866,3 +883,21 @@ describe('jsonp', () => {
).toBeTruthy()
})
})

describe('result', () => {
test('非函数返回原值', () => {
[null, undefined, 1, false, {}, [], /ddd/, new Date()].forEach(item => {
expect(vtils.result(item)).toBe(item)
})
})
test('函数返回函数执行结果', () => {
[() => {}, (): any[] => [], () => 1, () => Date].forEach(item => {
expect(vtils.result(item)).toEqual(item())
})
})
test('函数可传参', () => {
[(x: number): number => x, (x: number, y: number): number => x + y].forEach(item => {
expect(vtils.result(item, 1, 2)).toEqual((item as any)(1, 2))
})
})
})

0 comments on commit 163103b

Please sign in to comment.