Skip to content

Commit 10cb912

Browse files
committed
Fix #43
1 parent e88123a commit 10cb912

File tree

8 files changed

+193
-4
lines changed

8 files changed

+193
-4
lines changed

dist/vue2-storage.common.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*!
22
* vue2-storage v5.0.0
3-
* (c) 2019 Yarkov Aleksey
3+
* (c) 2021 Yarkov Aleksey
44
* Released under the MIT License.
55
*/
66
'use strict';
@@ -170,6 +170,7 @@ class StorageError extends Error {
170170
}
171171
}
172172

173+
const availableDrivers = ['local', 'session', 'memory'];
173174
class Vue2Storage {
174175
constructor(config = {}) {
175176
this.setOptions(config);
@@ -203,6 +204,7 @@ class Vue2Storage {
203204
driver: StorageDriver.LOCAL,
204205
ttl: 0,
205206
};
207+
this.checkConfig(config);
206208
const options = objectAssign(defaultOptions, config);
207209
this.options = Object.freeze(options);
208210
}
@@ -292,6 +294,28 @@ class Vue2Storage {
292294
}
293295
return Object.keys(this.driver.storage);
294296
}
297+
checkConfig(config) {
298+
if (config.prefix !== undefined) {
299+
if (typeof config.prefix !== 'string') {
300+
this.throwError(new TypeError('Option "prefix" must be a string'));
301+
}
302+
}
303+
if (config.driver !== undefined) {
304+
if (!availableDrivers.includes(config.driver)) {
305+
this.throwError(new TypeError(`Option "driver" must be one of ${availableDrivers.join(', ')}`));
306+
}
307+
}
308+
if (config.ttl !== undefined) {
309+
if (typeof config.ttl !== 'number') {
310+
this.throwError(new TypeError('Option "ttl" must be a number'));
311+
}
312+
}
313+
if (config.replacer !== undefined) {
314+
if (typeof config.replacer !== 'function') {
315+
this.throwError(new TypeError('Option "replacer" must be a function'));
316+
}
317+
}
318+
}
295319
addPrefix(key) {
296320
return `${this.options.prefix || ''}${key}`;
297321
}
@@ -301,9 +325,15 @@ class Vue2Storage {
301325
}
302326
toJSON(data, options = {}) {
303327
const ttl = ('ttl' in options) ? options.ttl : this.options.ttl;
328+
const { replacer } = this.options;
304329
return JSON.stringify({
305330
value: data,
306331
ttl: ttl > 0 ? ttl + Date.now() : 0,
332+
}, (key, value) => {
333+
if (!replacer || key !== 'value') {
334+
return value;
335+
}
336+
return replacer(key, value);
307337
});
308338
}
309339
fromJSON(key) {

dist/vue2-storage.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export interface StorageOptions {
44
prefix?: string
55
driver?: StorageDriver
66
ttl?: number
7+
replacer?: <V extends any, R extends any>(key: string, value: V) => R
78
}
89

910
export interface SetterOptions {

dist/vue2-storage.esm.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*!
22
* vue2-storage v5.0.0
3-
* (c) 2019 Yarkov Aleksey
3+
* (c) 2021 Yarkov Aleksey
44
* Released under the MIT License.
55
*/
66
/*! *****************************************************************************
@@ -166,6 +166,7 @@ class StorageError extends Error {
166166
}
167167
}
168168

169+
const availableDrivers = ['local', 'session', 'memory'];
169170
class Vue2Storage {
170171
constructor(config = {}) {
171172
this.setOptions(config);
@@ -199,6 +200,7 @@ class Vue2Storage {
199200
driver: StorageDriver.LOCAL,
200201
ttl: 0,
201202
};
203+
this.checkConfig(config);
202204
const options = objectAssign(defaultOptions, config);
203205
this.options = Object.freeze(options);
204206
}
@@ -288,6 +290,28 @@ class Vue2Storage {
288290
}
289291
return Object.keys(this.driver.storage);
290292
}
293+
checkConfig(config) {
294+
if (config.prefix !== undefined) {
295+
if (typeof config.prefix !== 'string') {
296+
this.throwError(new TypeError('Option "prefix" must be a string'));
297+
}
298+
}
299+
if (config.driver !== undefined) {
300+
if (!availableDrivers.includes(config.driver)) {
301+
this.throwError(new TypeError(`Option "driver" must be one of ${availableDrivers.join(', ')}`));
302+
}
303+
}
304+
if (config.ttl !== undefined) {
305+
if (typeof config.ttl !== 'number') {
306+
this.throwError(new TypeError('Option "ttl" must be a number'));
307+
}
308+
}
309+
if (config.replacer !== undefined) {
310+
if (typeof config.replacer !== 'function') {
311+
this.throwError(new TypeError('Option "replacer" must be a function'));
312+
}
313+
}
314+
}
291315
addPrefix(key) {
292316
return `${this.options.prefix || ''}${key}`;
293317
}
@@ -297,9 +321,15 @@ class Vue2Storage {
297321
}
298322
toJSON(data, options = {}) {
299323
const ttl = ('ttl' in options) ? options.ttl : this.options.ttl;
324+
const { replacer } = this.options;
300325
return JSON.stringify({
301326
value: data,
302327
ttl: ttl > 0 ? ttl + Date.now() : 0,
328+
}, (key, value) => {
329+
if (!replacer || key !== 'value') {
330+
return value;
331+
}
332+
return replacer(key, value);
303333
});
304334
}
305335
fromJSON(key) {

dist/vue2-storage.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*!
22
* vue2-storage v5.0.0
3-
* (c) 2019 Yarkov Aleksey
3+
* (c) 2021 Yarkov Aleksey
44
* Released under the MIT License.
55
*/
66
(function (global, factory) {
@@ -172,6 +172,7 @@
172172
}
173173
}
174174

175+
const availableDrivers = ['local', 'session', 'memory'];
175176
class Vue2Storage {
176177
constructor(config = {}) {
177178
this.setOptions(config);
@@ -205,6 +206,7 @@
205206
driver: StorageDriver.LOCAL,
206207
ttl: 0,
207208
};
209+
this.checkConfig(config);
208210
const options = objectAssign(defaultOptions, config);
209211
this.options = Object.freeze(options);
210212
}
@@ -294,6 +296,28 @@
294296
}
295297
return Object.keys(this.driver.storage);
296298
}
299+
checkConfig(config) {
300+
if (config.prefix !== undefined) {
301+
if (typeof config.prefix !== 'string') {
302+
this.throwError(new TypeError('Option "prefix" must be a string'));
303+
}
304+
}
305+
if (config.driver !== undefined) {
306+
if (!availableDrivers.includes(config.driver)) {
307+
this.throwError(new TypeError(`Option "driver" must be one of ${availableDrivers.join(', ')}`));
308+
}
309+
}
310+
if (config.ttl !== undefined) {
311+
if (typeof config.ttl !== 'number') {
312+
this.throwError(new TypeError('Option "ttl" must be a number'));
313+
}
314+
}
315+
if (config.replacer !== undefined) {
316+
if (typeof config.replacer !== 'function') {
317+
this.throwError(new TypeError('Option "replacer" must be a function'));
318+
}
319+
}
320+
}
297321
addPrefix(key) {
298322
return `${this.options.prefix || ''}${key}`;
299323
}
@@ -303,9 +327,15 @@
303327
}
304328
toJSON(data, options = {}) {
305329
const ttl = ('ttl' in options) ? options.ttl : this.options.ttl;
330+
const { replacer } = this.options;
306331
return JSON.stringify({
307332
value: data,
308333
ttl: ttl > 0 ? ttl + Date.now() : 0,
334+
}, (key, value) => {
335+
if (!replacer || key !== 'value') {
336+
return value;
337+
}
338+
return replacer(key, value);
309339
});
310340
}
311341
fromJSON(key) {

dist/vue2-storage.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/storage.spec.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,5 +207,64 @@ drivers.forEach((driver) => {
207207
done();
208208
});
209209
});
210+
211+
describe('Replacer', () => {
212+
beforeEach(() => {
213+
vm.$storage.clear(true);
214+
});
215+
216+
it('Not transform data by default', (done) => {
217+
vm.$storage.set('test', 'test');
218+
expect(vm.$storage.get('test')).toEqual('test');
219+
done();
220+
});
221+
222+
describe('If replacer is function', () => {
223+
it('Transform data if set "object"', (done) => {
224+
vm.$storage.setOptions({
225+
replacer: (_key: string, value: any) => ({ ...value, b: 2 }),
226+
});
227+
vm.$storage.set('key', { a: 1 });
228+
expect(vm.$storage.get('key')).toEqual({ a: 1, b: 2 });
229+
done();
230+
});
231+
232+
it('Transform data if set "array"', (done) => {
233+
vm.$storage.setOptions({
234+
replacer: (_key: string, value: any) => value.map((v) => v + 1),
235+
});
236+
vm.$storage.set('key', [1, 2, 3]);
237+
expect(vm.$storage.get('key')).toEqual([2, 3, 4]);
238+
done();
239+
});
240+
241+
it('Transform data if set "string"', (done) => {
242+
vm.$storage.setOptions({
243+
replacer: (_key: string, value: any) => String(value).repeat(2),
244+
});
245+
vm.$storage.set('key', 'repeat');
246+
expect(vm.$storage.get('key')).toEqual('repeatrepeat');
247+
done();
248+
});
249+
250+
it('Transform data if set "number"', (done) => {
251+
vm.$storage.setOptions({
252+
replacer: (_key: string, value: any) => value * 2,
253+
});
254+
vm.$storage.set('key', 2);
255+
expect(vm.$storage.get('key')).toEqual(4);
256+
done();
257+
});
258+
259+
it('Transform data if set "boolean"', (done) => {
260+
vm.$storage.setOptions({
261+
replacer: (_key: string, value: any) => !value,
262+
});
263+
vm.$storage.set('key', true);
264+
expect(vm.$storage.get('key')).toEqual(false);
265+
done();
266+
});
267+
});
268+
});
210269
});
211270
});

0 commit comments

Comments
 (0)