Skip to content

Commit

Permalink
Merge pull request #133 from joshnuss/rename-writable
Browse files Browse the repository at this point in the history
Rename writable function
  • Loading branch information
joshnuss authored Jan 18, 2023
2 parents 4ce099f + 87d7b10 commit c5dcc3d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 20 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ npm install svelte-local-storage-store
Define the store:

```javascript
import { writable } from 'svelte-local-storage-store'
import { persisted } from 'svelte-local-storage-store'

// First param `preferences` is the local storage key.
// Second param is the initial value.
export const preferences = writable('preferences', {
export const preferences = persisted('preferences', {
theme: 'dark',
pane: '50%',
...
Expand All @@ -45,7 +45,7 @@ You can also optionally set the `serializer` or `storage` type:
import * as devalue from 'devalue'

// third parameter is options.
export const preferences = writable('local-storage-key', 'default-value', {
export const preferences = persisted('local-storage-key', 'default-value', {
serializer: devalue, // defaults to `JSON`
storage: 'session' // 'session' for sessionStorage, defaults to 'local'
})
Expand Down
4 changes: 4 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ function getStorage(type: StorageType) {
}

export function writable<T>(key: string, initialValue: T, options?: Options<T>): Writable<T> {
console.warn("writable() has been deprecated. Please use persisted() instead.\n\nchange:\n\nimport { writable } from 'svelte-local-storage-store'\n\nto:\n\nimport { persisted } from 'svelte-local-storage-store'")
return persisted<T>(key, initialValue, options)
}
export function persisted<T>(key: string, initialValue: T, options?: Options<T>): Writable<T> {
const serializer = options?.serializer ?? JSON
const storageType = options?.storage ?? 'local'
const browser = typeof(window) !== 'undefined' && typeof(document) !== 'undefined'
Expand Down
48 changes: 31 additions & 17 deletions test/localStorageStore.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
import { writable } from '../index'
import { persisted, writable } from '../index'
import { get } from 'svelte/store'

beforeEach(() => localStorage.clear())

describe('writable()', () => {
test('it works, but raises deprecation warning', () => {
console.warn = jest.fn()

localStorage.setItem('myKey2', '"existing"')

const store = writable('myKey2', 'initial')
const value = get(store)

expect(value).toEqual('existing')
expect(console.warn).toHaveBeenCalledWith(expect.stringMatching(/deprecated/))
})
})

describe('persisted()', () => {
test('uses initial value if nothing in local storage', () => {
const store = writable('myKey', 123)
const store = persisted('myKey', 123)
const value = get(store)

expect(value).toEqual(123)
Expand All @@ -14,7 +28,7 @@ describe('writable()', () => {
test('uses existing value if data already in local storage', () => {
localStorage.setItem('myKey2', '"existing"')

const store = writable('myKey2', 'initial')
const store = persisted('myKey2', 'initial')
const value = get(store)

expect(value).toEqual('existing')
Expand All @@ -24,7 +38,7 @@ describe('writable()', () => {
test('replaces old value', () => {
localStorage.setItem('myKey3', '"existing"')

const store = writable('myKey3', '')
const store = persisted('myKey3', '')
store.set('new-value')
const value = get(store)

Expand All @@ -33,7 +47,7 @@ describe('writable()', () => {
})

test('adds new value', () => {
const store = writable('myKey4', '')
const store = persisted('myKey4', '')
store.set('new-value')
const value = get(store)

Expand All @@ -46,7 +60,7 @@ describe('writable()', () => {
test('replaces old value', () => {
localStorage.setItem('myKey5', '123')

const store = writable('myKey5', 0)
const store = persisted('myKey5', 0)
store.update(n => n + 1)
const value = get(store)

Expand All @@ -55,7 +69,7 @@ describe('writable()', () => {
})

test('adds new value', () => {
const store = writable('myKey6', 123)
const store = persisted('myKey6', 123)
store.update(n => n + 1)
const value = get(store)

Expand All @@ -66,7 +80,7 @@ describe('writable()', () => {

describe('subscribe()', () => {
it('publishes updates', () => {
const store = writable('myKey7', 123)
const store = persisted('myKey7', 123)
const values: number[] = []
const unsub = store.subscribe((value : number) => {
if (value !== undefined) values.push(value)
Expand All @@ -81,7 +95,7 @@ describe('writable()', () => {
})

it('handles duplicate stores with the same key', () => {
const store1 = writable('same-key', 1)
const store1 = persisted('same-key', 1)
const values1: number[] = []

const unsub1 = store1.subscribe(value => {
Expand All @@ -90,7 +104,7 @@ describe('writable()', () => {

store1.set(2)

const store2 = writable('same-key', 99)
const store2 = persisted('same-key', 99)
const values2: number[] = []

const unsub2 = store2.subscribe(value => {
Expand All @@ -114,7 +128,7 @@ describe('writable()', () => {
type NumberDict = { [key: string] : number }

it('sets storage when key matches', () => {
const store = writable('myKey8', {a: 1})
const store = persisted('myKey8', {a: 1})
const values: NumberDict[] = []

const unsub = store.subscribe((value: NumberDict) => {
Expand All @@ -130,7 +144,7 @@ describe('writable()', () => {
})

it('sets store to null when value is null', () => {
const store = writable('myKey9', {a: 1})
const store = persisted('myKey9', {a: 1})
const values: NumberDict[] = []

const unsub = store.subscribe((value: NumberDict) => {
Expand All @@ -146,7 +160,7 @@ describe('writable()', () => {
})

it("doesn't update store when key doesn't match", () => {
const store = writable('myKey10', 1)
const store = persisted('myKey10', 1)
const values: number[] = []

const unsub = store.subscribe((value: number) => {
Expand All @@ -162,7 +176,7 @@ describe('writable()', () => {
})

it("doesn't update store when there are no subscribers", () => {
const store = writable('myKey', 1)
const store = persisted('myKey', 1)
const values: number[] = []

const event = new StorageEvent('storage', {key: 'myKey', newValue: '2'})
Expand All @@ -187,7 +201,7 @@ describe('writable()', () => {

const testSet = new Set([1, 2, 3])

const store = writable('myKey11', testSet, { serializer })
const store = persisted('myKey11', testSet, { serializer })
const value = get(store)

store.update(d => d.add(4))
Expand All @@ -202,12 +216,12 @@ describe('writable()', () => {

const value = 'foo'

const store = writable('myKey12', value, {
const store = persisted('myKey12', value, {
storage: 'session'
})

store.set('bar')

expect(window.sessionStorage.setItem).toHaveBeenCalled()
})
})
})

0 comments on commit c5dcc3d

Please sign in to comment.