Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2f53943

Browse files
author
Adrian Hurtado
committed
feat!: change store value manipulation syntax
1 parent 18139a4 commit 2f53943

File tree

4 files changed

+80
-69
lines changed

4 files changed

+80
-69
lines changed

‎src/store.js‎

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -138,18 +138,36 @@ function setUpdatingHandlers (options = {}) {
138138
// //////////////////////////
139139
// Auxiliar functions
140140

141-
function applyFunctionsToStore (functionsDeclarations) {
142-
return Object.keys(functionsDeclarations).reduce(
143-
(acc, key) => ({
144-
...acc,
145-
[key]: (uid, ...args) => set(uid, functionsDeclarations[key](get(uid), ...args)),
146-
}),
147-
{},
148-
)
141+
function valueAs (type) {
142+
return (uid) => {
143+
const applyToUid = (fn) => (...args) => fn(uid, ...args)
144+
const functionsDeclarations = type && type !== 'value' ? ValueFunctions[type] : {}
145+
return {
146+
get: applyToUid(get),
147+
set: applyToUid(set),
148+
resetToDefault: applyToUid(resetToDefault),
149+
resetToInitial: applyToUid(resetToInitial),
150+
reset: applyToUid(reset),
151+
remove: applyToUid(remove),
152+
...Object.keys(functionsDeclarations).reduce(
153+
(acc, key) => ({
154+
...acc,
155+
[key]: (...args) => set(uid, functionsDeclarations[key](get(uid), ...args)),
156+
}),
157+
{},
158+
),
159+
}
160+
}
149161
}
150162

151163
export default {
152164
state,
165+
getValue: get,
166+
setValue: set,
167+
resetToDefault,
168+
resetToInitial,
169+
reset,
170+
remove,
153171
setState,
154172
setDefaultState,
155173
setDefaultValue,
@@ -163,19 +181,12 @@ export default {
163181
resetAllToInitial,
164182
setUpdatingHandlers,
165183
removeAll,
166-
value: {
167-
get,
168-
set,
169-
resetToDefault,
170-
resetToInitial,
171-
reset,
172-
remove,
173-
},
174-
boolean: applyFunctionsToStore(ValueFunctions.boolean),
175-
number: applyFunctionsToStore(ValueFunctions.number),
176-
string: applyFunctionsToStore(ValueFunctions.string),
177-
array: applyFunctionsToStore(ValueFunctions.array),
178-
set: applyFunctionsToStore(ValueFunctions.set),
179-
object: applyFunctionsToStore(ValueFunctions.object),
180-
map: applyFunctionsToStore(ValueFunctions.map),
184+
value: valueAs('value'),
185+
boolean: valueAs('boolean'),
186+
number: valueAs('number'),
187+
string: valueAs('string'),
188+
array: valueAs('array'),
189+
set: valueAs('set'),
190+
object: valueAs('object'),
191+
map: valueAs('map'),
181192
}

‎src/tests/store.spec.js‎

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,24 @@ beforeEach(() => {
1010
describe('store', () => {
1111
it('value.set', () => {
1212
expect(VueValuesStore.state.myValue).toBe(undefined)
13-
VueValuesStore.value.set('myValue','foo')
13+
VueValuesStore.value('myValue').set('foo')
1414
expect(VueValuesStore.state.myValue).toBe('foo')
1515
})
1616
it('value.get', () => {
17-
expect(VueValuesStore.value.get('myValue')).toBe(undefined)
18-
VueValuesStore.value.set('myValue','foo')
19-
expect(VueValuesStore.value.get('myValue')).toBe('foo')
17+
expect(VueValuesStore.value('myValue').get()).toBe(undefined)
18+
VueValuesStore.value('myValue').set('foo')
19+
expect(VueValuesStore.value('myValue').get()).toBe('foo')
2020
})
2121
it('value.get: set it if not exist with a default value', () => {
2222
expect(VueValuesStore.state.myValue).toBe(undefined)
23-
expect(VueValuesStore.value.get('myValue','foo')).toBe('foo')
23+
expect(VueValuesStore.value('myValue').get('foo')).toBe('foo')
2424
expect(VueValuesStore.state.myValue).toBe('foo')
2525
})
2626
it('value.remove', () => {
27-
VueValuesStore.value.set('myValue','foo')
28-
expect(VueValuesStore.value.get('myValue')).toBe('foo')
29-
VueValuesStore.value.remove('myValue')
30-
expect(VueValuesStore.value.get('myValue')).toBe(undefined)
27+
VueValuesStore.value('myValue').set('foo')
28+
expect(VueValuesStore.value('myValue').get()).toBe('foo')
29+
VueValuesStore.value('myValue').remove()
30+
expect(VueValuesStore.value('myValue').get()).toBe(undefined)
3131
})
3232
it('setState', () => {
3333
expect(VueValuesStore.state.myValue1).toBe(undefined)
@@ -90,32 +90,32 @@ describe('store', () => {
9090
it('value.resetToDefault', () => {
9191
VueValuesStore.setDefaultValue('myValue', 'foo')
9292
expect(VueValuesStore.state.myValue).toBe(undefined)
93-
VueValuesStore.value.resetToDefault('myValue')
93+
VueValuesStore.value('myValue').resetToDefault()
9494
expect(VueValuesStore.state.myValue).toBe('foo')
9595
})
9696
it('value.resetToInitial', () => {
9797
VueValuesStore.setInitialValue('myValue', 'foo')
9898
expect(VueValuesStore.state.myValue).toBe(undefined)
99-
VueValuesStore.value.resetToInitial('myValue')
99+
VueValuesStore.value('myValue').resetToInitial()
100100
expect(VueValuesStore.state.myValue).toBe('foo')
101101
})
102102
it('value.reset (default priors to initial)', () => {
103103
VueValuesStore.setDefaultValue('myValue', 'foo')
104104
VueValuesStore.setInitialValue('myValue', 'bar')
105105
expect(VueValuesStore.state.myValue).toBe(undefined)
106-
VueValuesStore.value.reset('myValue')
106+
VueValuesStore.value('myValue').reset()
107107
expect(VueValuesStore.state.myValue).toBe('foo')
108108
})
109109
it('value.reset (initial if default is not present)', () => {
110110
VueValuesStore.setInitialValue('myValue', 'bar')
111111
expect(VueValuesStore.state.myValue).toBe(undefined)
112-
VueValuesStore.value.reset('myValue')
112+
VueValuesStore.value('myValue').reset()
113113
expect(VueValuesStore.state.myValue).toBe('bar')
114114
})
115115
it('value.reset (undefined if default nor initial are present)', () => {
116-
VueValuesStore.value.set('myValue','foo')
116+
VueValuesStore.value('myValue').set('foo')
117117
expect(VueValuesStore.state.myValue).toBe('foo')
118-
VueValuesStore.value.reset('myValue')
118+
VueValuesStore.value('myValue').reset()
119119
expect(VueValuesStore.state.myValue).toBe(undefined)
120120
})
121121

@@ -141,57 +141,57 @@ describe('store', () => {
141141
const onDelete = jest.fn()
142142
VueValuesStore.setDefaultState({ myValue: 'bar' })
143143
VueValuesStore.setUpdatingHandlers({ onSet, onDelete })
144-
VueValuesStore.value.set('myValue','foo')
144+
VueValuesStore.value('myValue').set('foo')
145145
expect(onSet).toHaveBeenCalledWith('myValue', 'foo')
146-
VueValuesStore.value.resetToDefault('myValue')
146+
VueValuesStore.value('myValue').resetToDefault()
147147
expect(onSet).toHaveBeenCalledWith('myValue', 'bar')
148-
VueValuesStore.value.remove('myValue')
148+
VueValuesStore.value('myValue').remove()
149149
expect(onDelete).toHaveBeenCalledWith('myValue')
150150
})
151151
it('setUpdatingHandlers: onSet controls the value is set', () => {
152152
const onSet = () => 'bar'
153153
VueValuesStore.setUpdatingHandlers({ onSet })
154-
VueValuesStore.value.set('myValue','foo')
154+
VueValuesStore.value('myValue').set('foo')
155155
expect(VueValuesStore.state.myValue).toBe('bar')
156156
})
157157

158158
it('boolean.toogle', () => {
159-
VueValuesStore.value.set('myValue',false)
160-
VueValuesStore.boolean.toggle('myValue')
159+
VueValuesStore.value('myValue').set(false)
160+
VueValuesStore.boolean('myValue').toggle()
161161
expect(VueValuesStore.state.myValue).toStrictEqual(true)
162-
VueValuesStore.boolean.toggle('myValue')
162+
VueValuesStore.boolean('myValue').toggle()
163163
expect(VueValuesStore.state.myValue).toStrictEqual(false)
164164
})
165165
it('number.increment', () => {
166-
VueValuesStore.value.set('myValue',0)
167-
VueValuesStore.number.increment('myValue')
166+
VueValuesStore.value('myValue').set(0)
167+
VueValuesStore.number('myValue').increment()
168168
expect(VueValuesStore.state.myValue).toStrictEqual(1)
169-
VueValuesStore.number.increment('myValue')
169+
VueValuesStore.number('myValue').increment()
170170
expect(VueValuesStore.state.myValue).toStrictEqual(2)
171171
})
172172
it('string.append', () => {
173-
VueValuesStore.value.set('myValue','foo')
174-
VueValuesStore.string.append('myValue','bar')
173+
VueValuesStore.value('myValue').set('foo')
174+
VueValuesStore.string('myValue').append('bar')
175175
expect(VueValuesStore.state.myValue).toStrictEqual('foobar')
176176
})
177177
it('array.append', () => {
178-
VueValuesStore.value.set('myValue',['foo'])
179-
VueValuesStore.array.append('myValue','bar')
178+
VueValuesStore.value('myValue').set(['foo'])
179+
VueValuesStore.array('myValue').append('bar')
180180
expect(VueValuesStore.state.myValue).toStrictEqual(['foo', 'bar'])
181181
})
182182
it('set.add', () => {
183-
VueValuesStore.value.set('myValue',new Set(['foo']))
184-
VueValuesStore.set.add('myValue','bar')
183+
VueValuesStore.value('myValue').set(new Set(['foo']))
184+
VueValuesStore.set('myValue').add('bar')
185185
expect(VueValuesStore.state.myValue).toStrictEqual(new Set(['foo', 'bar']))
186186
})
187187
it('object.setValue', () => {
188-
VueValuesStore.value.set('myValue',{ foo: 1 })
189-
VueValuesStore.object.setValue('myValue','bar', 2)
188+
VueValuesStore.value('myValue').set({ foo: 1 })
189+
VueValuesStore.object('myValue').setValue('bar', 2)
190190
expect(VueValuesStore.state.myValue).toStrictEqual({ foo: 1, bar: 2 })
191191
})
192192
it('map.setValue', () => {
193-
VueValuesStore.value.set('myValue',new Map([['foo', 1]]))
194-
VueValuesStore.map.setValue('myValue','bar', 2)
193+
VueValuesStore.value('myValue').set(new Map([['foo', 1]]))
194+
VueValuesStore.map('myValue').setValue('bar', 2)
195195
expect(VueValuesStore.state.myValue).toStrictEqual(new Map([['foo', 1], ['bar', 2]]))
196196
})
197197
})

‎src/tests/values.spec.js‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ function testGeneralValue ({
179179
if (isStoredValue) {
180180
it('Initializes with defaultValue from store', async () => {
181181
VueValuesStore.removeAll()
182-
VueValuesStore.value.set(uid,foo)
182+
VueValuesStore.value(uid).set(foo)
183183
const { getAttrs } = getUtilities()
184184
expect(getAttrs().value).toStrictEqual(foo)
185185
})
@@ -196,7 +196,7 @@ function testGeneralValue ({
196196
VueValuesStore.setDefaultValue(uid, foo)
197197
const { getAttrs } = getUtilities()
198198
expect(getAttrs().value).toStrictEqual(emptyValue)
199-
VueValuesStore.value.resetToDefault(uid)
199+
VueValuesStore.value(uid).resetToDefault()
200200
await nextTick()
201201
expect(getAttrs().value).toStrictEqual(foo)
202202
})
@@ -205,7 +205,7 @@ function testGeneralValue ({
205205
VueValuesStore.setInitialValue(uid, foo)
206206
const { getAttrs } = getUtilities()
207207
expect(getAttrs().value).toStrictEqual(emptyValue)
208-
VueValuesStore.value.resetToInitial(uid)
208+
VueValuesStore.value(uid).resetToInitial()
209209
await nextTick()
210210
expect(getAttrs().value).toStrictEqual(foo)
211211
})
@@ -215,7 +215,7 @@ function testGeneralValue ({
215215
VueValuesStore.setInitialValue(uid, bar)
216216
const { getAttrs } = getUtilities()
217217
expect(getAttrs().value).toStrictEqual(emptyValue)
218-
VueValuesStore.value.reset(uid)
218+
VueValuesStore.value(uid).reset()
219219
await nextTick()
220220
expect(getAttrs().value).toStrictEqual(foo)
221221
})
@@ -224,15 +224,15 @@ function testGeneralValue ({
224224
VueValuesStore.setInitialValue(uid, bar)
225225
const { getAttrs } = getUtilities()
226226
expect(getAttrs().value).toStrictEqual(emptyValue)
227-
VueValuesStore.value.reset(uid)
227+
VueValuesStore.value(uid).reset()
228228
await nextTick()
229229
expect(getAttrs().value).toStrictEqual(bar)
230230
})
231231
it('Reacts to a stored value change', async () => {
232232
VueValuesStore.removeAll()
233233
const { getAttrs } = getUtilities({ initialValue: foo })
234234
expect(getAttrs().value).toStrictEqual(foo)
235-
VueValuesStore.value.set(uid,bar)
235+
VueValuesStore.value(uid).set(bar)
236236
await nextTick()
237237
expect(getAttrs().value).toStrictEqual(bar)
238238
})

‎src/useStoredValue.js‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ export const storedValueProps = {
1212

1313
export default function useValue (props, context, { emptyValue = undefined }) {
1414
const reactiveValue = computed({
15-
get: () => VueValuesStore.value.get(props.uid,firstDefined(props.initialValue, props.defaultValue, emptyValue)),
16-
set: (newValue) => VueValuesStore.value.set(props.uid,newValue),
15+
get: () => VueValuesStore.value(props.uid).get(firstDefined(props.initialValue, props.defaultValue, emptyValue)),
16+
set: (newValue) => VueValuesStore.value(props.uid).set(newValue),
1717
})
1818

1919
const { functions, scopedProps } = useCommonValue(props, context, { reactiveValue }, { emptyValue })
@@ -23,22 +23,22 @@ export default function useValue (props, context, { emptyValue = undefined }) {
2323
if (props.defaultValue !== undefined) {
2424
set(props.defaultValue)
2525
} else {
26-
VueValuesStore.value.resetToDefault(props.uid)
26+
VueValuesStore.value(props.uid).resetToDefault()
2727
}
2828
}
2929
const resetToInitial = () => {
3030
if (props.initialValue !== undefined) {
3131
set(props.initialValue)
3232
} else {
33-
VueValuesStore.value.resetToInitial(props.uid)
33+
VueValuesStore.value(props.uid).resetToInitial()
3434
}
3535
}
3636
const reset = () => {
3737
const value = firstDefined(props.defaultValue, props.initialValue)
3838
if (value !== undefined) {
3939
set(value)
4040
} else {
41-
VueValuesStore.value.reset(props.uid)
41+
VueValuesStore.value(props.uid).reset()
4242
}
4343
}
4444

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /