Comparing version 1.0.1 to 2.0.0
@@ -7,3 +7,7 @@ # Change Log | ||
## [2.0.0] - 2017-08-13 | ||
- Changed `set` method: last argument now is an object, see documentation. | ||
- Added expiry date | ||
## [1.0.1] - 2017-08-12 | ||
- First release |
@@ -1,2 +0,2 @@ | ||
// [AIV] InCache Build version: 1.0.1 | ||
// [AIV] InCache Build version: 2.0.0 | ||
var incache = | ||
@@ -98,5 +98,15 @@ /******/ (function(modules) { // webpackBootstrap | ||
*/ | ||
var globalKey = '___incache___storage___global___key'; | ||
var GLOBAL_KEY = '___incache___storage___global___key___'; | ||
/** | ||
* Default options | ||
* @type {{silent: boolean, life: number}} | ||
* @ignore | ||
*/ | ||
var DEFAULT_OPTS = { | ||
silent: false, | ||
life: 0 | ||
}; | ||
/** | ||
* Root object | ||
@@ -107,3 +117,3 @@ * @ignore | ||
if (!root[globalKey]) root[globalKey] = {}; | ||
if (!root[GLOBAL_KEY]) root[GLOBAL_KEY] = {}; | ||
@@ -114,3 +124,3 @@ /** | ||
*/ | ||
var storage = root[globalKey]; | ||
var storage = root[GLOBAL_KEY]; | ||
@@ -125,3 +135,5 @@ var _onRemoved = function _onRemoved() {}; | ||
* @param value {any} | ||
* @param [silent=false] {boolean} if true no event will be triggered | ||
* @param [opts] {Object} options object | ||
* @param [opts.silent=false] {boolean} if true no event will be triggered | ||
* @param [opts.life=0] {number} seconds of life. If 0 not expire. | ||
* @returns {{isNew: boolean, createdOn: Date|null, updatedOn: Date|null, value: *}} | ||
@@ -131,5 +143,7 @@ * @example | ||
* incache.set('my object', {a: 1, b: 2}); | ||
* incache.set('my boolean', true); | ||
* incache.set('my boolean', true, {life: 2}); // Expires after 2 seconds | ||
*/ | ||
incache.set = function (key, value, silent) { | ||
incache.set = function (key, value) { | ||
var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var record = { | ||
@@ -139,12 +153,19 @@ isNew: true, | ||
updatedOn: null, | ||
expiresOn: null, | ||
value: value | ||
}; | ||
opts = helper.defaults(opts, DEFAULT_OPTS); | ||
if (opts.life && helper.is(opts.life, 'number')) { | ||
record.expiresOn = helper.addSecondsToNow(opts.life); | ||
} | ||
if (incache.has(key)) { | ||
record.isNew = false; | ||
record.updatedOn = new Date(); | ||
if (!silent) _onUpdated.call(undefined, key, record); | ||
if (!opts.silent) _onUpdated.call(undefined, key, record); | ||
} else { | ||
record.createdOn = new Date(); | ||
if (!silent) _onCreated.call(undefined, key, record); | ||
if (!opts.silent) _onCreated.call(undefined, key, record); | ||
} | ||
@@ -173,3 +194,3 @@ | ||
if (helper.is(records[i].key, 'undefined') || helper.is(records[i].value, 'undefined')) throw new Error('key and value properties are required'); | ||
incache.set(records[i].key, records[i].value, true); | ||
incache.set(records[i].key, records[i].value, { silent: true }); | ||
} | ||
@@ -189,3 +210,11 @@ }; | ||
return incache.has(key) ? onlyValue ? storage[key].value : storage[key] : null; | ||
if (incache.has(key)) { | ||
if (incache.expired(key)) { | ||
incache.remove(key, true); | ||
return null; | ||
} | ||
return onlyValue ? storage[key].value : storage[key]; | ||
} else { | ||
return null; | ||
} | ||
}; | ||
@@ -229,10 +258,33 @@ | ||
for (var key in storage) { | ||
if (storage.hasOwnProperty(key)) records.push({ | ||
key: key, | ||
value: storage[key].value | ||
}); | ||
}return records; | ||
if (storage.hasOwnProperty(key)) { | ||
if (incache.expired(key)) { | ||
incache.remove(key, true); | ||
} else { | ||
records.push({ | ||
key: key, | ||
value: storage[key].value | ||
}); | ||
} | ||
} | ||
} | ||
return records; | ||
}; | ||
/** | ||
* Check if record is expired | ||
* @param key {any} | ||
* @returns {boolean} | ||
*/ | ||
incache.expired = function (key) { | ||
if (storage[key] && storage[key].expiresOn) { | ||
var now = new Date(); | ||
var expiry = new Date(storage[key].expiresOn); | ||
return now > expiry; | ||
} else { | ||
return false; | ||
} | ||
}; | ||
/** | ||
* Remove all records | ||
@@ -245,3 +297,3 @@ */ | ||
*/ | ||
storage = root[globalKey] = {}; | ||
storage = root[GLOBAL_KEY] = {}; | ||
}; | ||
@@ -320,3 +372,3 @@ | ||
module.exports = incache; | ||
module.exports._global_key = globalKey; | ||
module.exports._global_key = GLOBAL_KEY; | ||
/* WEBPACK VAR INJECTION */}.call(exports, __webpack_require__(2), __webpack_require__(3))) | ||
@@ -588,2 +640,12 @@ | ||
/** | ||
* Adds seconds to current date | ||
* @param seconds {number} number of seconds to add | ||
* @returns {Date} | ||
*/ | ||
helper.addSecondsToNow = function (seconds) { | ||
var now = new Date(); | ||
return new Date(now.setSeconds(now.getSeconds() + seconds)); | ||
}; | ||
module.exports = helper; | ||
@@ -590,0 +652,0 @@ |
@@ -1,2 +0,2 @@ | ||
// [AIV] InCache Build version: 1.0.1 | ||
var incache=function(t){function e(r){if(n[r])return n[r].exports;var o=n[r]={i:r,l:!1,exports:{}};return t[r].call(o.exports,o,o.exports,e),o.l=!0,o.exports}var n={};return e.m=t,e.c=n,e.d=function(t,n,r){e.o(t,n)||Object.defineProperty(t,n,{configurable:!1,enumerable:!0,get:r})},e.n=function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(n,"a",n),n},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=0)}([function(t,e,n){"use strict";t.exports=n(1)},function(t,e,n){"use strict";(function(e,r){var o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},u=n(4),i={},c="___incache___storage___global___key",a="object"===(void 0===e?"undefined":o(e))&&void 0!==e.pid?r:window;a[c]||(a[c]={});var f=a[c],l=function(){},s=function(){},y=function(){};i.set=function(t,e,n){var r={isNew:!0,createdOn:null,updatedOn:null,value:e};return i.has(t)?(r.isNew=!1,r.updatedOn=new Date,n||y.call(void 0,t,r)):(r.createdOn=new Date,n||s.call(void 0,t,r)),f[t]=r,r},i.bulkSet=function(t){if(!u.is(t,"array"))throw new Error("records must be an array of object, e.g. {key: foo, value: bar}");for(var e=0;e<t.length;e++){if(u.is(t[e].key,"undefined")||u.is(t[e].value,"undefined"))throw new Error("key and value properties are required");i.set(t[e].key,t[e].value,!0)}},i.get=function(t){var e=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];return i.has(t)?e?f[t].value:f[t]:null},i.remove=function(t){var e=arguments.length>1&&void 0!==arguments[1]&&arguments[1];delete f[t],e||l.call(void 0,t)},i.bulkRemove=function(t){if(!u.is(t,"array"))throw new Error("keys must be an array of keys");for(var e=0;e<t.length;e++)i.remove(t[e],!0)},i.all=function(){var t=[];for(var e in f)f.hasOwnProperty(e)&&t.push({key:e,value:f[e].value});return t},i.clear=function(){f=a[c]={}},i.has=function(t){return f.hasOwnProperty(t)},i.onRemoved=function(t){l=t},i.onCreated=function(t){s=t},i.onUpdated=function(t){y=t},t.exports=i,t.exports._global_key=c}).call(e,n(2),n(3))},function(t,e,n){"use strict";function r(){throw new Error("setTimeout has not been defined")}function o(){throw new Error("clearTimeout has not been defined")}function u(t){if(s===setTimeout)return setTimeout(t,0);if((s===r||!s)&&setTimeout)return s=setTimeout,setTimeout(t,0);try{return s(t,0)}catch(e){try{return s.call(null,t,0)}catch(e){return s.call(this,t,0)}}}function i(t){if(y===clearTimeout)return clearTimeout(t);if((y===o||!y)&&clearTimeout)return y=clearTimeout,clearTimeout(t);try{return y(t)}catch(e){try{return y.call(null,t)}catch(e){return y.call(this,t)}}}function c(){m&&d&&(m=!1,d.length?h=d.concat(h):v=-1,h.length&&a())}function a(){if(!m){var t=u(c);m=!0;for(var e=h.length;e;){for(d=h,h=[];++v<e;)d&&d[v].run();v=-1,e=h.length}d=null,m=!1,i(t)}}function f(t,e){this.fun=t,this.array=e}function l(){}var s,y,p=t.exports={};!function(){try{s="function"==typeof setTimeout?setTimeout:r}catch(t){s=r}try{y="function"==typeof clearTimeout?clearTimeout:o}catch(t){y=o}}();var d,h=[],m=!1,v=-1;p.nextTick=function(t){var e=new Array(arguments.length-1);if(arguments.length>1)for(var n=1;n<arguments.length;n++)e[n-1]=arguments[n];h.push(new f(t,e)),1!==h.length||m||u(a)},f.prototype.run=function(){this.fun.apply(null,this.array)},p.title="browser",p.browser=!0,p.env={},p.argv=[],p.version="",p.versions={},p.on=l,p.addListener=l,p.once=l,p.off=l,p.removeListener=l,p.removeAllListeners=l,p.emit=l,p.prependListener=l,p.prependOnceListener=l,p.listeners=function(t){return[]},p.binding=function(t){throw new Error("process.binding is not supported")},p.cwd=function(){return"/"},p.chdir=function(t){throw new Error("process.chdir is not supported")},p.umask=function(){return 0}},function(t,e,n){"use strict";var r,o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t};r=function(){return this}();try{r=r||Function("return this")()||(0,eval)("this")}catch(t){"object"===("undefined"==typeof window?"undefined":o(window))&&(r=window)}t.exports=r},function(t,e,n){"use strict";var r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},o={};o.is=function(t,e){return Object.prototype.toString.call(t).toLowerCase()==="[object "+e+"]".toLowerCase()},o.defaults=function(t,e){for(var n in e)e.hasOwnProperty(n)&&(t.hasOwnProperty(n)?"object"===r(t[n])&&o.defaults(t[n],e[n]):t[n]=e[n]);return t},t.exports=o}]); | ||
// [AIV] InCache Build version: 2.0.0 | ||
var incache=function(e){function t(r){if(n[r])return n[r].exports;var o=n[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,t),o.l=!0,o.exports}var n={};return t.m=e,t.c=n,t.d=function(e,n,r){t.o(e,n)||Object.defineProperty(e,n,{configurable:!1,enumerable:!0,get:r})},t.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(n,"a",n),n},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=0)}([function(e,t,n){"use strict";e.exports=n(1)},function(e,t,n){"use strict";(function(t,r){var o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},i=n(4),u={},c="___incache___storage___global___key___",a={silent:!1,life:0},l="object"===(void 0===t?"undefined":o(t))&&void 0!==t.pid?r:window;l[c]||(l[c]={});var f=l[c],s=function(){},p=function(){},y=function(){};u.set=function(e,t){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},r={isNew:!0,createdOn:null,updatedOn:null,expiresOn:null,value:t};return n=i.defaults(n,a),n.life&&i.is(n.life,"number")&&(r.expiresOn=i.addSecondsToNow(n.life)),u.has(e)?(r.isNew=!1,r.updatedOn=new Date,n.silent||y.call(void 0,e,r)):(r.createdOn=new Date,n.silent||p.call(void 0,e,r)),f[e]=r,r},u.bulkSet=function(e){if(!i.is(e,"array"))throw new Error("records must be an array of object, e.g. {key: foo, value: bar}");for(var t=0;t<e.length;t++){if(i.is(e[t].key,"undefined")||i.is(e[t].value,"undefined"))throw new Error("key and value properties are required");u.set(e[t].key,e[t].value,{silent:!0})}},u.get=function(e){var t=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];return u.has(e)?u.expired(e)?(u.remove(e,!0),null):t?f[e].value:f[e]:null},u.remove=function(e){var t=arguments.length>1&&void 0!==arguments[1]&&arguments[1];delete f[e],t||s.call(void 0,e)},u.bulkRemove=function(e){if(!i.is(e,"array"))throw new Error("keys must be an array of keys");for(var t=0;t<e.length;t++)u.remove(e[t],!0)},u.all=function(){var e=[];for(var t in f)f.hasOwnProperty(t)&&(u.expired(t)?u.remove(t,!0):e.push({key:t,value:f[t].value}));return e},u.expired=function(e){if(f[e]&&f[e].expiresOn){return new Date>new Date(f[e].expiresOn)}return!1},u.clear=function(){f=l[c]={}},u.has=function(e){return f.hasOwnProperty(e)},u.onRemoved=function(e){s=e},u.onCreated=function(e){p=e},u.onUpdated=function(e){y=e},e.exports=u,e.exports._global_key=c}).call(t,n(2),n(3))},function(e,t,n){"use strict";function r(){throw new Error("setTimeout has not been defined")}function o(){throw new Error("clearTimeout has not been defined")}function i(e){if(s===setTimeout)return setTimeout(e,0);if((s===r||!s)&&setTimeout)return s=setTimeout,setTimeout(e,0);try{return s(e,0)}catch(t){try{return s.call(null,e,0)}catch(t){return s.call(this,e,0)}}}function u(e){if(p===clearTimeout)return clearTimeout(e);if((p===o||!p)&&clearTimeout)return p=clearTimeout,clearTimeout(e);try{return p(e)}catch(t){try{return p.call(null,e)}catch(t){return p.call(this,e)}}}function c(){m&&d&&(m=!1,d.length?h=d.concat(h):v=-1,h.length&&a())}function a(){if(!m){var e=i(c);m=!0;for(var t=h.length;t;){for(d=h,h=[];++v<t;)d&&d[v].run();v=-1,t=h.length}d=null,m=!1,u(e)}}function l(e,t){this.fun=e,this.array=t}function f(){}var s,p,y=e.exports={};!function(){try{s="function"==typeof setTimeout?setTimeout:r}catch(e){s=r}try{p="function"==typeof clearTimeout?clearTimeout:o}catch(e){p=o}}();var d,h=[],m=!1,v=-1;y.nextTick=function(e){var t=new Array(arguments.length-1);if(arguments.length>1)for(var n=1;n<arguments.length;n++)t[n-1]=arguments[n];h.push(new l(e,t)),1!==h.length||m||i(a)},l.prototype.run=function(){this.fun.apply(null,this.array)},y.title="browser",y.browser=!0,y.env={},y.argv=[],y.version="",y.versions={},y.on=f,y.addListener=f,y.once=f,y.off=f,y.removeListener=f,y.removeAllListeners=f,y.emit=f,y.prependListener=f,y.prependOnceListener=f,y.listeners=function(e){return[]},y.binding=function(e){throw new Error("process.binding is not supported")},y.cwd=function(){return"/"},y.chdir=function(e){throw new Error("process.chdir is not supported")},y.umask=function(){return 0}},function(e,t,n){"use strict";var r,o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e};r=function(){return this}();try{r=r||Function("return this")()||(0,eval)("this")}catch(e){"object"===("undefined"==typeof window?"undefined":o(window))&&(r=window)}e.exports=r},function(e,t,n){"use strict";var r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},o={};o.is=function(e,t){return Object.prototype.toString.call(e).toLowerCase()==="[object "+t+"]".toLowerCase()},o.defaults=function(e,t){for(var n in t)t.hasOwnProperty(n)&&(e.hasOwnProperty(n)?"object"===r(e[n])&&o.defaults(e[n],t[n]):e[n]=t[n]);return e},o.addSecondsToNow=function(e){var t=new Date;return new Date(t.setSeconds(t.getSeconds()+e))},e.exports=o}]); |
{ | ||
"name": "incache", | ||
"version": "1.0.1", | ||
"version": "2.0.0", | ||
"description": "Share singleton via global object, yes you can!", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -35,2 +35,5 @@ <div align="center"> | ||
store.clear(); | ||
// Expires after 2 seconds | ||
incache.set('my string', 'hello world', {life: 2}); | ||
``` | ||
@@ -37,0 +40,0 @@ |
@@ -34,2 +34,12 @@ const helper = {}; | ||
/** | ||
* Adds seconds to current date | ||
* @param seconds {number} number of seconds to add | ||
* @returns {Date} | ||
*/ | ||
helper.addSecondsToNow = (seconds) => { | ||
let now = new Date(); | ||
return new Date(now.setSeconds(now.getSeconds() + seconds)); | ||
}; | ||
module.exports = helper; |
@@ -13,5 +13,15 @@ const helper = require('./helper'); | ||
*/ | ||
const globalKey = '___incache___storage___global___key'; | ||
const GLOBAL_KEY = '___incache___storage___global___key___'; | ||
/** | ||
* Default options | ||
* @type {{silent: boolean, life: number}} | ||
* @ignore | ||
*/ | ||
const DEFAULT_OPTS = { | ||
silent: false, | ||
life: 0 | ||
}; | ||
/** | ||
* Root object | ||
@@ -22,4 +32,4 @@ * @ignore | ||
if (!root[globalKey]) | ||
root[globalKey] = {}; | ||
if (!root[GLOBAL_KEY]) | ||
root[GLOBAL_KEY] = {}; | ||
@@ -30,3 +40,3 @@ /** | ||
*/ | ||
let storage = root[globalKey]; | ||
let storage = root[GLOBAL_KEY]; | ||
@@ -44,3 +54,5 @@ let _onRemoved = () => { | ||
* @param value {any} | ||
* @param [silent=false] {boolean} if true no event will be triggered | ||
* @param [opts] {Object} options object | ||
* @param [opts.silent=false] {boolean} if true no event will be triggered | ||
* @param [opts.life=0] {number} seconds of life. If 0 not expire. | ||
* @returns {{isNew: boolean, createdOn: Date|null, updatedOn: Date|null, value: *}} | ||
@@ -50,5 +62,5 @@ * @example | ||
* incache.set('my object', {a: 1, b: 2}); | ||
* incache.set('my boolean', true); | ||
* incache.set('my boolean', true, {life: 2}); // Expires after 2 seconds | ||
*/ | ||
incache.set = (key, value, silent) => { | ||
incache.set = (key, value, opts = {}) => { | ||
let record = { | ||
@@ -58,13 +70,20 @@ isNew: true, | ||
updatedOn: null, | ||
expiresOn: null, | ||
value: value | ||
}; | ||
opts = helper.defaults(opts, DEFAULT_OPTS); | ||
if (opts.life && helper.is(opts.life, 'number')) { | ||
record.expiresOn = helper.addSecondsToNow(opts.life); | ||
} | ||
if (incache.has(key)) { | ||
record.isNew = false; | ||
record.updatedOn = new Date(); | ||
if(!silent) | ||
if (!opts.silent) | ||
_onUpdated.call(this, key, record); | ||
} else { | ||
record.createdOn = new Date(); | ||
if(!silent) | ||
if (!opts.silent) | ||
_onCreated.call(this, key, record); | ||
@@ -96,3 +115,3 @@ } | ||
throw new Error('key and value properties are required'); | ||
incache.set(records[i].key, records[i].value, true); | ||
incache.set(records[i].key, records[i].value, {silent: true}); | ||
} | ||
@@ -110,6 +129,11 @@ }; | ||
incache.get = (key, onlyValue = true) => { | ||
return incache.has(key) ? | ||
onlyValue ? | ||
storage[key].value : storage[key] | ||
: null; | ||
if (incache.has(key)) { | ||
if (incache.expired(key)) { | ||
incache.remove(key, true); | ||
return null; | ||
} | ||
return onlyValue ? storage[key].value : storage[key]; | ||
} else { | ||
return null; | ||
} | ||
}; | ||
@@ -126,3 +150,3 @@ | ||
delete storage[key]; | ||
if(!silent) | ||
if (!silent) | ||
_onRemoved.call(this, key); | ||
@@ -153,8 +177,14 @@ }; | ||
for (let key in storage) | ||
if (storage.hasOwnProperty(key)) | ||
records.push({ | ||
key: key, | ||
value: storage[key].value | ||
}); | ||
for (let key in storage) { | ||
if (storage.hasOwnProperty(key)) { | ||
if(incache.expired(key)) { | ||
incache.remove(key, true); | ||
} else { | ||
records.push({ | ||
key: key, | ||
value: storage[key].value | ||
}); | ||
} | ||
} | ||
} | ||
@@ -165,2 +195,17 @@ return records; | ||
/** | ||
* Check if record is expired | ||
* @param key {any} | ||
* @returns {boolean} | ||
*/ | ||
incache.expired = (key) => { | ||
if (storage[key] && storage[key].expiresOn) { | ||
let now = new Date(); | ||
let expiry = new Date(storage[key].expiresOn); | ||
return now > expiry; | ||
} else { | ||
return false; | ||
} | ||
}; | ||
/** | ||
* Remove all records | ||
@@ -173,3 +218,3 @@ */ | ||
*/ | ||
storage = root[globalKey] = {}; | ||
storage = root[GLOBAL_KEY] = {}; | ||
}; | ||
@@ -248,2 +293,2 @@ | ||
module.exports = incache; | ||
module.exports._global_key = globalKey; | ||
module.exports._global_key = GLOBAL_KEY; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
85994
851
86