Skip to content

Commit

Permalink
feat: data serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Mar 8, 2021
1 parent 8760b24 commit 3e96b26
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
- Unix-style mountable paths (multi driver)
- Default in-memory storage
- Tree-shakable and lightweight core
- Native aware value serialization and deserialization

WIP:

- JSON serialization and native types (destr)
- State compression
- State hydration
- Links (soft/junction)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@types/jest": "latest",
"@types/jsdom": "^16.2.6",
"@types/node": "latest",
"destr": "^1.1.0",
"eslint": "latest",
"jest": "latest",
"jiti": "latest",
Expand Down
12 changes: 8 additions & 4 deletions src/storage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import destr from 'destr'
import type { Storage, Driver } from './types'
import memory from './drivers/memory'
import { normalizeKey, asyncCall } from './utils'
import { normalizeKey, asyncCall, stringify } from './utils'

export function createStorage (): Storage {
const defaultStorage = memory()
Expand Down Expand Up @@ -34,11 +35,14 @@ export function createStorage (): Storage {
},
getItem (_key) {
const { key, driver } = getDriver(_key)
return asyncCall(driver.getItem, key)
return asyncCall(driver.getItem, key).then(val => destr(val))
},
setItem (_key, vlaue) {
setItem (_key, value) {
if (value === undefined) {
return storage.removeItem(_key)
}
const { key, driver } = getDriver(_key)
return asyncCall(driver.setItem, key, vlaue)
return asyncCall(driver.setItem, key, stringify(value))
},
removeItem (_key) {
const { key, driver } = getDriver(_key)
Expand Down
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export type StorageValue = string | null
export type StorageValue = null | string | String | number | Number | boolean | Boolean | object

export interface Driver {
hasItem: (key: string) => boolean | Promise<boolean>
getItem: (key: string) => StorageValue | Promise<StorageValue>
getItem: (key: string) => string | Promise<string>
setItem: (key: string, value: string) => void | Promise<void>
removeItem: (key: string) => void | Promise<void>
getKeys: () => string[] | Promise<string[]>
Expand All @@ -15,7 +15,7 @@ export type DriverFactory<OptsT = any> = (opts?: OptsT) => Driver
export interface Storage {
hasItem: (key: string) => Promise<boolean>
getItem: (key: string) => Promise<StorageValue>
setItem: (key: string, value: string) => Promise<void>
setItem: (key: string, value: StorageValue) => Promise<void>
removeItem: (key: string) => Promise<void>
getKeys: () => Promise<string[]>
clear: () => Promise<void>
Expand Down
9 changes: 9 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,12 @@ export function asyncCall<T extends (...args: any) => any>(fn: T, ...args: any[]
return Promise.reject(err)
}
}

export function isPrimitive (arg: any) {
const type = typeof arg
return arg === null || (type !== 'object' && type !== 'function')
}

export function stringify (arg: any) {
return isPrimitive(arg) ? (arg + '') : JSON.stringify(arg)
}
14 changes: 14 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ function testDriver (getParams: () => TestParams | Promise<TestParams>) {
expect(await storage.getKeys()).toMatchObject(['foo:bar'])
})

it('serialize (object)', async () => {
await storage.setItem('test.json', { json: 'works' })
expect(await storage.getItem('test.json')).toMatchObject({ json: 'works' })
})

it('serialize (primitive)', async () => {
await storage.setItem('true.json', true)
expect(await storage.getItem('true.json')).toBe(true)
})

it('verify', () => {
if (params.verify) {
return params.verify(params)
Expand All @@ -75,6 +85,10 @@ function testDriver (getParams: () => TestParams | Promise<TestParams>) {
await storage.removeItem('foo:bar')
expect(await storage.hasItem('foo:bar')).toBe(false)
expect(await storage.getItem('foo:bar')).toBe(null)
})

it('clear', async () => {
await storage.clear()
expect(await storage.getKeys()).toMatchObject([])
})
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1841,6 +1841,11 @@ delayed-stream@~1.0.0:
resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619"
integrity sha1-3zrhmayt+31ECqrgsp4icrJOxhk=

destr@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/destr/-/destr-1.1.0.tgz#2da6add6ba71e04fd0abfb1e642d4f6763235095"
integrity sha512-Ev/sqS5AzzDwlpor/5wFCDu0dYMQu/0x2D6XfAsQ0E7uQmamIgYJ6Dppo2T2EOFVkeVYWjc+PCLKaqZZ57qmLg==

detect-indent@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/detect-indent/-/detect-indent-6.0.0.tgz#0abd0f549f69fc6659a254fe96786186b6f528fd"
Expand Down

0 comments on commit 3e96b26

Please sign in to comment.