Comparing version 2.12.0 to 2.13.0
@@ -200,7 +200,7 @@ ;(function(){ | ||
require.register("store/dist/store2.js", function(exports, require, module){ | ||
/*! store2 - v2.12.0 - 2020-08-12 | ||
* Copyright (c) 2020 Nathan Bubna; Licensed (MIT OR GPL-3.0) */ | ||
/*! store2 - v2.13.0 - 2021-12-18 | ||
* Copyright (c) 2021 Nathan Bubna; Licensed (MIT OR GPL-3.0) */ | ||
;(function(window, define) { | ||
var _ = { | ||
version: "2.12.0", | ||
version: "2.13.0", | ||
areas: {}, | ||
@@ -218,4 +218,4 @@ apis: {}, | ||
}, | ||
stringify: function(d) { | ||
return d === undefined || typeof d === "function" ? d+'' : JSON.stringify(d); | ||
stringify: function(d, fn) { | ||
return d === undefined || typeof d === "function" ? d+'' : JSON.stringify(d,fn||_.replace); | ||
}, | ||
@@ -296,3 +296,11 @@ parse: function(s, fn) { | ||
}, | ||
isFake: function(){ return this._area.name === 'fake'; }, | ||
isFake: function(force) { | ||
if (force) { | ||
this._real = this._area; | ||
this._area = _.storage('fake'); | ||
} else if (force === false) { | ||
this._area = this._real || this._area; | ||
} | ||
return this._area.name === 'fake'; | ||
}, | ||
toString: function() { | ||
@@ -345,7 +353,11 @@ return 'store'+(this._ns?'.'+this.namespace():'')+'['+this._id+']'; | ||
set: function(key, data, overwrite) { | ||
var d = this.get(key); | ||
var d = this.get(key), | ||
replacer; | ||
if (d != null && overwrite === false) { | ||
return data; | ||
} | ||
return _.set(this._area, this._in(key), _.stringify(data), overwrite) || d; | ||
if (typeof overwrite !== "boolean") { | ||
replacer = overwrite; | ||
} | ||
return _.set(this._area, this._in(key), _.stringify(data, replacer)) || d; | ||
}, | ||
@@ -362,3 +374,3 @@ setAll: function(data, overwrite) { | ||
}, | ||
add: function(key, data) { | ||
add: function(key, data, replacer) { | ||
var d = this.get(key); | ||
@@ -378,3 +390,3 @@ if (d instanceof Array) { | ||
} | ||
_.set(this._area, this._in(key), _.stringify(data)); | ||
_.set(this._area, this._in(key), _.stringify(data, replacer)); | ||
return data; | ||
@@ -381,0 +393,0 @@ }, |
@@ -1,6 +0,6 @@ | ||
/*! store2 - v2.12.0 - 2020-08-12 | ||
* Copyright (c) 2020 Nathan Bubna; Licensed (MIT OR GPL-3.0) */ | ||
/*! store2 - v2.13.0 - 2021-12-18 | ||
* Copyright (c) 2021 Nathan Bubna; Licensed (MIT OR GPL-3.0) */ | ||
;(function(window, define) { | ||
var _ = { | ||
version: "2.12.0", | ||
version: "2.13.0", | ||
areas: {}, | ||
@@ -18,4 +18,4 @@ apis: {}, | ||
}, | ||
stringify: function(d) { | ||
return d === undefined || typeof d === "function" ? d+'' : JSON.stringify(d); | ||
stringify: function(d, fn) { | ||
return d === undefined || typeof d === "function" ? d+'' : JSON.stringify(d,fn||_.replace); | ||
}, | ||
@@ -96,3 +96,11 @@ parse: function(s, fn) { | ||
}, | ||
isFake: function(){ return this._area.name === 'fake'; }, | ||
isFake: function(force) { | ||
if (force) { | ||
this._real = this._area; | ||
this._area = _.storage('fake'); | ||
} else if (force === false) { | ||
this._area = this._real || this._area; | ||
} | ||
return this._area.name === 'fake'; | ||
}, | ||
toString: function() { | ||
@@ -145,7 +153,11 @@ return 'store'+(this._ns?'.'+this.namespace():'')+'['+this._id+']'; | ||
set: function(key, data, overwrite) { | ||
var d = this.get(key); | ||
var d = this.get(key), | ||
replacer; | ||
if (d != null && overwrite === false) { | ||
return data; | ||
} | ||
return _.set(this._area, this._in(key), _.stringify(data), overwrite) || d; | ||
if (typeof overwrite !== "boolean") { | ||
replacer = overwrite; | ||
} | ||
return _.set(this._area, this._in(key), _.stringify(data, replacer)) || d; | ||
}, | ||
@@ -162,3 +174,3 @@ setAll: function(data, overwrite) { | ||
}, | ||
add: function(key, data) { | ||
add: function(key, data, replacer) { | ||
var d = this.get(key); | ||
@@ -178,3 +190,3 @@ if (d instanceof Array) { | ||
} | ||
_.set(this._area, this._in(key), _.stringify(data)); | ||
_.set(this._area, this._in(key), _.stringify(data, replacer)); | ||
return data; | ||
@@ -181,0 +193,0 @@ }, |
@@ -1,5 +0,5 @@ | ||
/*! store2 - v2.12.0 - 2020-08-12 | ||
* Copyright (c) 2020 Nathan Bubna; Licensed (MIT OR GPL-3.0) */ | ||
/*! store2 - v2.13.0 - 2021-12-18 | ||
* Copyright (c) 2021 Nathan Bubna; Licensed (MIT OR GPL-3.0) */ | ||
!function(a,b){var c={version:"2.12.0",areas:{},apis:{},inherit:function(a,b){for(var c in a)b.hasOwnProperty(c)||Object.defineProperty(b,c,Object.getOwnPropertyDescriptor(a,c));return b},stringify:function(a){return void 0===a||"function"==typeof a?a+"":JSON.stringify(a)},parse:function(a,b){try{return JSON.parse(a,b||c.revive)}catch(b){return a}},fn:function(a,b){c.storeAPI[a]=b;for(var d in c.apis)c.apis[d][a]=b},get:function(a,b){return a.getItem(b)},set:function(a,b,c){a.setItem(b,c)},remove:function(a,b){a.removeItem(b)},key:function(a,b){return a.key(b)},length:function(a){return a.length},clear:function(a){a.clear()},Store:function(a,b,d){var e=c.inherit(c.storeAPI,function(a,b,c){return 0===arguments.length?e.getAll():"function"==typeof b?e.transact(a,b,c):void 0!==b?e.set(a,b,c):"string"==typeof a||"number"==typeof a?e.get(a):"function"==typeof a?e.each(a):a?e.setAll(a,b):e.clear()});e._id=a;try{b.setItem("__store2_test","ok"),e._area=b,b.removeItem("__store2_test")}catch(a){e._area=c.storage("fake")}return e._ns=d||"",c.areas[a]||(c.areas[a]=e._area),c.apis[e._ns+e._id]||(c.apis[e._ns+e._id]=e),e},storeAPI:{area:function(a,b){var d=this[a];return d&&d.area||(d=c.Store(a,b,this._ns),this[a]||(this[a]=d)),d},namespace:function(a,b){if(!a)return this._ns?this._ns.substring(0,this._ns.length-1):"";var d=a,e=this[d];if(!(e&&e.namespace||(e=c.Store(this._id,this._area,this._ns+d+"."),this[d]||(this[d]=e),b)))for(var f in c.areas)e.area(f,c.areas[f]);return e},isFake:function(){return"fake"===this._area.name},toString:function(){return"store"+(this._ns?"."+this.namespace():"")+"["+this._id+"]"},has:function(a){return this._area.has?this._area.has(this._in(a)):!!(this._in(a)in this._area)},size:function(){return this.keys().length},each:function(a,b){for(var d=0,e=c.length(this._area);d<e;d++){var f=this._out(c.key(this._area,d));if(void 0!==f&&!1===a.call(this,f,this.get(f),b))break;e>c.length(this._area)&&(e--,d--)}return b||this},keys:function(a){return this.each(function(a,b,c){c.push(a)},a||[])},get:function(a,b){var d,e=c.get(this._area,this._in(a));return"function"==typeof b&&(d=b,b=null),null!==e?c.parse(e,d):null!=b?b:e},getAll:function(a){return this.each(function(a,b,c){c[a]=b},a||{})},transact:function(a,b,c){var d=this.get(a,c),e=b(d);return this.set(a,void 0===e?d:e),this},set:function(a,b,d){var e=this.get(a);return null!=e&&!1===d?b:c.set(this._area,this._in(a),c.stringify(b),d)||e},setAll:function(a,b){var c,d;for(var e in a)d=a[e],this.set(e,d,b)!==d&&(c=!0);return c},add:function(a,b){var d=this.get(a);if(d instanceof Array)b=d.concat(b);else if(null!==d){var e=typeof d;if(e===typeof b&&"object"===e){for(var f in b)d[f]=b[f];b=d}else b=d+b}return c.set(this._area,this._in(a),c.stringify(b)),b},remove:function(a,b){var d=this.get(a,b);return c.remove(this._area,this._in(a)),d},clear:function(){return this._ns?this.each(function(a){c.remove(this._area,this._in(a))},1):c.clear(this._area),this},clearAll:function(){var a=this._area;for(var b in c.areas)c.areas.hasOwnProperty(b)&&(this._area=c.areas[b],this.clear());return this._area=a,this},_in:function(a){return"string"!=typeof a&&(a=c.stringify(a)),this._ns?this._ns+a:a},_out:function(a){return this._ns?a&&0===a.indexOf(this._ns)?a.substring(this._ns.length):void 0:a}},storage:function(a){return c.inherit(c.storageAPI,{items:{},name:a})},storageAPI:{length:0,has:function(a){return this.items.hasOwnProperty(a)},key:function(a){var b=0;for(var c in this.items)if(this.has(c)&&a===b++)return c},setItem:function(a,b){this.has(a)||this.length++,this.items[a]=b},removeItem:function(a){this.has(a)&&(delete this.items[a],this.length--)},getItem:function(a){return this.has(a)?this.items[a]:null},clear:function(){for(var a in this.items)this.removeItem(a)}}},d=c.Store("local",function(){try{return localStorage}catch(a){}}());d.local=d,d._=c,d.area("session",function(){try{return sessionStorage}catch(a){}}()),d.area("page",c.storage("page")),"function"==typeof b&&void 0!==b.amd?b("store2",[],function(){return d}):"undefined"!=typeof module&&module.exports?module.exports=d:(a.store&&(c.conflict=a.store),a.store=d)}(this,this&&this.define); | ||
!function(a,b){var c={version:"2.13.0",areas:{},apis:{},inherit:function(a,b){for(var c in a)b.hasOwnProperty(c)||Object.defineProperty(b,c,Object.getOwnPropertyDescriptor(a,c));return b},stringify:function(a,b){return void 0===a||"function"==typeof a?a+"":JSON.stringify(a,b||c.replace)},parse:function(a,b){try{return JSON.parse(a,b||c.revive)}catch(b){return a}},fn:function(a,b){c.storeAPI[a]=b;for(var d in c.apis)c.apis[d][a]=b},get:function(a,b){return a.getItem(b)},set:function(a,b,c){a.setItem(b,c)},remove:function(a,b){a.removeItem(b)},key:function(a,b){return a.key(b)},length:function(a){return a.length},clear:function(a){a.clear()},Store:function(a,b,d){var e=c.inherit(c.storeAPI,function(a,b,c){return 0===arguments.length?e.getAll():"function"==typeof b?e.transact(a,b,c):void 0!==b?e.set(a,b,c):"string"==typeof a||"number"==typeof a?e.get(a):"function"==typeof a?e.each(a):a?e.setAll(a,b):e.clear()});e._id=a;try{b.setItem("__store2_test","ok"),e._area=b,b.removeItem("__store2_test")}catch(a){e._area=c.storage("fake")}return e._ns=d||"",c.areas[a]||(c.areas[a]=e._area),c.apis[e._ns+e._id]||(c.apis[e._ns+e._id]=e),e},storeAPI:{area:function(a,b){var d=this[a];return d&&d.area||(d=c.Store(a,b,this._ns),this[a]||(this[a]=d)),d},namespace:function(a,b){if(!a)return this._ns?this._ns.substring(0,this._ns.length-1):"";var d=a,e=this[d];if(!(e&&e.namespace||(e=c.Store(this._id,this._area,this._ns+d+"."),this[d]||(this[d]=e),b)))for(var f in c.areas)e.area(f,c.areas[f]);return e},isFake:function(a){return a?(this._real=this._area,this._area=c.storage("fake")):!1===a&&(this._area=this._real||this._area),"fake"===this._area.name},toString:function(){return"store"+(this._ns?"."+this.namespace():"")+"["+this._id+"]"},has:function(a){return this._area.has?this._area.has(this._in(a)):!!(this._in(a)in this._area)},size:function(){return this.keys().length},each:function(a,b){for(var d=0,e=c.length(this._area);d<e;d++){var f=this._out(c.key(this._area,d));if(void 0!==f&&!1===a.call(this,f,this.get(f),b))break;e>c.length(this._area)&&(e--,d--)}return b||this},keys:function(a){return this.each(function(a,b,c){c.push(a)},a||[])},get:function(a,b){var d,e=c.get(this._area,this._in(a));return"function"==typeof b&&(d=b,b=null),null!==e?c.parse(e,d):null!=b?b:e},getAll:function(a){return this.each(function(a,b,c){c[a]=b},a||{})},transact:function(a,b,c){var d=this.get(a,c),e=b(d);return this.set(a,void 0===e?d:e),this},set:function(a,b,d){var e,f=this.get(a);return null!=f&&!1===d?b:("boolean"!=typeof d&&(e=d),c.set(this._area,this._in(a),c.stringify(b,e))||f)},setAll:function(a,b){var c,d;for(var e in a)d=a[e],this.set(e,d,b)!==d&&(c=!0);return c},add:function(a,b,d){var e=this.get(a);if(e instanceof Array)b=e.concat(b);else if(null!==e){var f=typeof e;if(f===typeof b&&"object"===f){for(var g in b)e[g]=b[g];b=e}else b=e+b}return c.set(this._area,this._in(a),c.stringify(b,d)),b},remove:function(a,b){var d=this.get(a,b);return c.remove(this._area,this._in(a)),d},clear:function(){return this._ns?this.each(function(a){c.remove(this._area,this._in(a))},1):c.clear(this._area),this},clearAll:function(){var a=this._area;for(var b in c.areas)c.areas.hasOwnProperty(b)&&(this._area=c.areas[b],this.clear());return this._area=a,this},_in:function(a){return"string"!=typeof a&&(a=c.stringify(a)),this._ns?this._ns+a:a},_out:function(a){return this._ns?a&&0===a.indexOf(this._ns)?a.substring(this._ns.length):void 0:a}},storage:function(a){return c.inherit(c.storageAPI,{items:{},name:a})},storageAPI:{length:0,has:function(a){return this.items.hasOwnProperty(a)},key:function(a){var b=0;for(var c in this.items)if(this.has(c)&&a===b++)return c},setItem:function(a,b){this.has(a)||this.length++,this.items[a]=b},removeItem:function(a){this.has(a)&&(delete this.items[a],this.length--)},getItem:function(a){return this.has(a)?this.items[a]:null},clear:function(){for(var a in this.items)this.removeItem(a)}}},d=c.Store("local",function(){try{return localStorage}catch(a){}}());d.local=d,d._=c,d.area("session",function(){try{return sessionStorage}catch(a){}}()),d.area("page",c.storage("page")),"function"==typeof b&&void 0!==b.amd?b("store2",[],function(){return d}):"undefined"!=typeof module&&module.exports?module.exports=d:(a.store&&(c.conflict=a.store),a.store=d)}(this,this&&this.define); | ||
//# sourceMappingURL=store2.min.js.map |
@@ -1,53 +0,43 @@ | ||
declare namespace store { | ||
export const local: StoreAPI; | ||
export const session: StoreAPI; | ||
export const page: StoreAPI; | ||
export interface StoredData { | ||
[key: string]: any; | ||
} | ||
export type Replacer = (key: any, value: any) => string | String[] | number[]; | ||
export type Reviver = (key: string, value: any) => any; | ||
export type EachFn = (key: any, data: any) => false | any; | ||
export type TransactFn = (data: any) => any | undefined; | ||
export function area(id: string, area: globalThis.Storage): StoreAPI; | ||
export function set(key: any, data: any, overwrite?: boolean): any; | ||
export function setAll(data: Object, overwrite?: boolean): StoredData; | ||
export function add(key: any, data: any): any; | ||
export function get(key: any, alt?: any): any; | ||
export function getAll(fillObj?: StoredData): StoredData; | ||
export function transact(key: any, fn: (data: any) => any, alt?: any): StoreAPI; | ||
export function clear(): StoreAPI; | ||
export function has(key: any): boolean; | ||
export function remove(key: any, alt?: any): any; | ||
export function each(callback: (key: any, data: any) => false | any, value?: any): StoreAPI; | ||
export function keys(fillList?: string[]): string[]; | ||
export function size(): number; | ||
export function clearAll(): StoreAPI; | ||
export function isFake(): boolean; | ||
export function namespace(namespace: string, noSession?: true): StoreAPI; | ||
type BaseSet = (key: any, data: any) => any; | ||
type BaseGet = (key: any) => any; | ||
type BaseSetAll = (obj: Object) => StoredData; | ||
type BaseGetAll = () => StoredData; | ||
type BaseTransact = (fn: EachFn, value?: any) => StoredData; | ||
type BaseClear = (clear: false) => StoreBase; | ||
export type Base = BaseSet & BaseGet & BaseSetAll & BaseGetAll & BaseTransact & BaseClear; | ||
export interface StoreAPI { | ||
clear(): StoreAPI; | ||
clearAll(): StoreAPI; | ||
each(callback: (key: any, data: any) => false | any): StoreAPI; | ||
get(key: any, alt?: any): any; | ||
getAll(fillObj?: StoredData): StoredData; | ||
has(key: any): boolean; | ||
isFake(): boolean; | ||
keys(fillList?: string[]): string[]; | ||
namespace(namespace: string, noSession?: true): StoreAPI; | ||
remove(key: any, alt?: any): any; | ||
set(key: any, data: any, overwrite?: boolean): any; | ||
setAll(data: Object, overwrite?: boolean): StoredData; | ||
add(key: any, data: any): any; | ||
size(): number; | ||
transact(key: any, fn: (data: any) => any, alt?: any): StoreAPI; | ||
} | ||
export interface StoredData { | ||
[key: string]: any; | ||
} | ||
export interface StoreAPI { | ||
clear(): StoreBase; | ||
clearAll(): StoreBase; | ||
each(callback: EachFn): StoreBase; | ||
get(key: any, alt?: any|Reviver): any; | ||
getAll(fillObj?: StoredData): StoredData; | ||
has(key: any): boolean; | ||
isFake(force?: boolean): boolean; | ||
keys(fillList?: string[]): string[]; | ||
namespace(namespace: string, noSession?: true): store; | ||
remove(key: any, alt?: any|Reviver): any; | ||
set(key: any, data: any, overwrite?: boolean|Replacer): any; | ||
setAll(data: Object, overwrite?: boolean|Replacer): StoredData; | ||
add(key: any, data: any): any; | ||
size(): number; | ||
transact(key: any, fn: TransactFn, alt?: any|Reviver): StoreBase; | ||
} | ||
declare function store(key: any, fn: (data: any) => any, alt?: any): store.StoreAPI | ||
declare function store(key: any, data: any): any | ||
declare function store(clearIfFalsy: false | 0): store.StoreAPI | ||
declare function store(key: any): any | ||
declare function store(obj: Object): store.StoredData | ||
declare function store(eachFn: (key: any, data: any) => false | any, value?: any): store.StoredData | ||
export type StoreBase = StoreAPI & Base; | ||
export = store; | ||
export type store = StoreBase & { | ||
local: StoreBase; | ||
session: StoreBase; | ||
page: StoreBase; | ||
}; | ||
export default store; |
{ | ||
"name": "store2", | ||
"version": "2.12.0", | ||
"version": "2.13.0", | ||
"description": "Better localStorage", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -43,3 +43,3 @@ A feature-filled and friendly way to take advantage of localStorage and sessionStorage | ||
store.each(fn[, fill]); // === store(fn); optional call arg will be 3rd fn arg (e.g. for gathering values) | ||
store.add(key, data); // concats, merges, or adds new value into existing one | ||
store.add(key, data[, replacer]); // concats, merges, or adds new value into existing one | ||
store.keys([fillList]); // returns array of keys | ||
@@ -55,4 +55,2 @@ store.size(); // number of keys, not length of data | ||
All functions which take an optional ```alt``` parameter can also use that parameter to specify a "reviver" function. These receive each key and value (yes, nested ones too) as arguments and allow you to provide an alternate means of parsing that string. This is particularly useful for rich objects like ```Date``` types. See [MDN's JSON.parse docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse) for more information and examples. Alternately, you can set a global reviver to the ```store._.revive``` property to handle all ```get```, ```getAll```, and ```transact``` calls. | ||
Functions passed to ```transact``` will receive the current value for that key as an argument or | ||
@@ -82,2 +80,6 @@ a passed alternate if there is none. When the passed function is completed, transact will save the returned value | ||
All retrieval functions which take an optional ```alt``` parameter can also use that parameter to specify a "reviver" function. These receive each key and value (yes, nested ones too) as arguments and allow you to provide an alternate means of parsing that string. This is particularly useful for rich objects like ```Date``` types. See [MDN's JSON.parse docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse) for more information and examples. Alternately, you can set a global reviver to the ```store._.revive``` property to handle all ```get```, ```getAll```, ```remove```, and ```transact``` calls. | ||
Likewise, setter functions which take an optional ```overwrite``` parameter can also use that parameter to accept a "replacer" function that receives each key and value (yes, nested ones too) as arguments and allow you to provide an alternate means of stringifying the values. This is particularly useful for rich objects like ```Date``` types. See [MDN's JSON.stringify docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) for more information and examples. Alternately, you can set a global replacer to the ```store._.replacer``` property to handle all ```set```, ```setAll```, ```add```, and ```transact``` calls. | ||
For ```getAll``` and ```keys```, there is the option to pass in the object or list, respectively, | ||
@@ -123,7 +125,7 @@ that you want the results to be added to. This is instead of an empty list. | ||
The 'namespace' function is one of two "extra" functions that are also part of the "store API": | ||
The 'namespace' function is one of three "extra" functions that are also part of the "store API": | ||
```javascript | ||
store.namespace(prefix);// returns a new store API that prefixes all key-based functions | ||
store.isFake();// is this actually localStorage/sessionStorage or an in-memory fallback? | ||
store.isFake([force]);// test or set whether localStorage/sessionStorage or an in-memory, 'fake' storage is used | ||
``` | ||
@@ -135,2 +137,5 @@ | ||
```isFake(true|false)``` is particularly useful to force use of a temporary, fake storage in testing situations, | ||
to prevent cluttering actual storage. | ||
## Extensions | ||
@@ -150,2 +155,3 @@ These mostly could use further documentation and abuse...er...testing. | ||
* [store.dom.js][dom] - Declarative, persistent DOM element content via store. | ||
* [store.cookie.js][cookie] - Support for a cookie as a storage area: ```store.cookie('num',1)``` to make sharing with backend easier. | ||
@@ -159,4 +165,4 @@ #### Alpha - Either incomplete or unstable or both | ||
* [store.async.js][async] - Adds ```store.async``` duplicate to each store and namespace that performs functions asynchronously and returns a Promise that resolves when complete. | ||
* [store.cookies.js][cookies] - Support managing all cookies as a storage area with the store API (e.g. ```store.cookies.get('user')```) | ||
[old]: https://raw.github.com/nbubna/store/master/src/store.old.js | ||
@@ -174,2 +180,4 @@ [overflow]: https://raw.github.com/nbubna/store/master/src/store.overflow.js | ||
[async]: https://raw.github.com/nbubna/store/master/src/store.async.js | ||
[cookie]: https://raw.github.com/nbubna/store/master/src/store.cookie.js | ||
[cookies]: https://raw.github.com/nbubna/store/master/src/store.cookies.js | ||
@@ -231,2 +239,3 @@ #### Write Your Own Extension | ||
* 2020-08-12 [v2.12.0][] (public) - PRs for better Storage typing, better testKey, and dev dependency updates. | ||
* 2021-12-16 [v2.13.0][] (public) - Add ```store.set(key, value, replacerFn)```, ```store._replacer```, and ```isFake([force])``` to support stringifying rich types and easier testing. And cookie-based extensions for using store backed by a single 'store' cookie or store API for all cookies. | ||
@@ -255,1 +264,2 @@ [v2.0.3]: https://github.com/nbubna/store/tree/2.0.3 | ||
[v2.12.0]: https://github.com/nbubna/store/tree/2.12.0 | ||
[v2.13.0]: https://github.com/nbubna/store/tree/2.13.0 |
/** | ||
* Copyright (c) 2020 ESHA Research | ||
* Copyright (c) 2021 ESHA Research | ||
* Dual licensed under the MIT and GPL licenses: | ||
@@ -7,3 +7,5 @@ * http://www.opensource.org/licenses/mit-license.php | ||
* | ||
* Creates a store API that uses a single cookie as the backing storage. | ||
* Creates a store area that uses a single cookie as the backing storage. | ||
* This gives you the store API for a specific 'store' cookie that your backend | ||
* can access too. It could definitely use more testing. | ||
* | ||
@@ -25,5 +27,8 @@ * Status: BETA - unsupported, useful, needs testing | ||
}; | ||
C.all = function() { | ||
return C.read(C.name) || {}; | ||
}; | ||
C.read = function(name) { | ||
var match = document.cookie.match(new RegExp("(^| )"+(name||C.name)+"=([^;]+)")); | ||
return match ? C.decode(match[2]) : {}; | ||
return match ? C.decode(match[2]) : null; | ||
}; | ||
@@ -39,3 +44,3 @@ C.write = function(state, name) { | ||
var c = 0, | ||
state = C.read(); | ||
state = C.all(); | ||
for (var k in state) { | ||
@@ -48,3 +53,3 @@ if (state.hasOwnProperty(k) && i === c++) { | ||
setItem: function(k, v) { | ||
var state = C.read(); | ||
var state = C.all(); | ||
state[k] = v; | ||
@@ -54,7 +59,7 @@ C.write(state); | ||
getItem: function(k) { | ||
var state = C.read(); | ||
var state = C.all(); | ||
return state.hasOwnProperty(k) ? state[k] : null; | ||
}, | ||
has: function(k) { | ||
return C.read().hasOwnProperty(k); | ||
return C.all().hasOwnProperty(k); | ||
}, | ||
@@ -71,3 +76,3 @@ removeItem: function(k) { | ||
var ln = 0, | ||
state = C.read(); | ||
state = C.all(); | ||
for (var k in state) { | ||
@@ -74,0 +79,0 @@ if (state.hasOwnProperty(k)) { |
/** | ||
* Copyright (c) 2020 ESHA Research | ||
* Copyright (c) 2021 ESHA Research | ||
* Dual licensed under the MIT and GPL licenses: | ||
@@ -7,5 +7,8 @@ * http://www.opensource.org/licenses/mit-license.php | ||
* | ||
* Creates a store API that uses cookies for each key/value pair. | ||
* Creates a store area that uses a separate cookies for each key/value as the backing | ||
* storage. This effectively gives you the store API for all cookies.. | ||
* It could definitely use more testing. It also feels like it should better | ||
* integrated with the expire plugin before we call it BETA. | ||
* | ||
* Status: BETA - unsupported, useful, needs testing | ||
* Status: ALPHA - unsupported, useful, needs testing | ||
*/ | ||
@@ -12,0 +15,0 @@ ;(function(window, document, store, _) { |
/** | ||
* Copyright (c) 2018, ESHA Research | ||
* Copyright (c) 2021, ESHA Research | ||
* Dual licensed under the MIT and GPL licenses: | ||
@@ -22,4 +22,4 @@ * http://www.opensource.org/licenses/mit-license.php | ||
}, | ||
stringify: function(d) { | ||
return d === undefined || typeof d === "function" ? d+'' : JSON.stringify(d); | ||
stringify: function(d, fn) { | ||
return d === undefined || typeof d === "function" ? d+'' : JSON.stringify(d,fn||_.replace); | ||
}, | ||
@@ -100,3 +100,11 @@ parse: function(s, fn) { | ||
}, | ||
isFake: function(){ return this._area.name === 'fake'; }, | ||
isFake: function(force) { | ||
if (force) { | ||
this._real = this._area; | ||
this._area = _.storage('fake'); | ||
} else if (force === false) { | ||
this._area = this._real || this._area; | ||
} | ||
return this._area.name === 'fake'; | ||
}, | ||
toString: function() { | ||
@@ -149,7 +157,11 @@ return 'store'+(this._ns?'.'+this.namespace():'')+'['+this._id+']'; | ||
set: function(key, data, overwrite) { | ||
var d = this.get(key); | ||
var d = this.get(key), | ||
replacer; | ||
if (d != null && overwrite === false) { | ||
return data; | ||
} | ||
return _.set(this._area, this._in(key), _.stringify(data), overwrite) || d; | ||
if (typeof overwrite !== "boolean") { | ||
replacer = overwrite; | ||
} | ||
return _.set(this._area, this._in(key), _.stringify(data, replacer)) || d; | ||
}, | ||
@@ -166,3 +178,3 @@ setAll: function(data, overwrite) { | ||
}, | ||
add: function(key, data) { | ||
add: function(key, data, replacer) { | ||
var d = this.get(key); | ||
@@ -182,3 +194,3 @@ if (d instanceof Array) { | ||
} | ||
_.set(this._area, this._in(key), _.stringify(data)); | ||
_.set(this._area, this._in(key), _.stringify(data, replacer)); | ||
return data; | ||
@@ -185,0 +197,0 @@ }, |
Sorry, the diff of this file is not supported yet
138980
25
3023
256