diff --git a/README.md b/README.md index f8fad2f..d5efbf7 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ bower install --save cachefactory ## Quick Start ```js -import CacheFactory from 'cachefactory'; +import {CacheFactory} from 'cachefactory'; const cacheFactory = new CacheFactory(); let cache; diff --git a/build_examples/browserify/app.js b/build_examples/browserify/app.js index ad65d94..3ef9342 100644 --- a/build_examples/browserify/app.js +++ b/build_examples/browserify/app.js @@ -1,5 +1,5 @@ // normally this would be var CacheFactory = require('cachefactory'); -var CacheFactory = require('../../'); +var CacheFactory = require('../../').CacheFactory; var cacheFactory = new CacheFactory(); var cache = cacheFactory.createCache('my-cache'); diff --git a/build_examples/r.js/app.js b/build_examples/r.js/app.js index a9510a9..a147604 100644 --- a/build_examples/r.js/app.js +++ b/build_examples/r.js/app.js @@ -1,6 +1,6 @@ define('app', [ 'cachefactory' -], function (CacheFactory) { - var cacheFactory = new CacheFactory(); +], function (CacheFactoryModule) { + var cacheFactory = new CacheFactoryModule.CacheFactory(); return cacheFactory.createCache('my-cache'); }); diff --git a/build_examples/webpack/app.js b/build_examples/webpack/app.js index ad65d94..3ef9342 100644 --- a/build_examples/webpack/app.js +++ b/build_examples/webpack/app.js @@ -1,5 +1,5 @@ // normally this would be var CacheFactory = require('cachefactory'); -var CacheFactory = require('../../'); +var CacheFactory = require('../../').CacheFactory; var cacheFactory = new CacheFactory(); var cache = cacheFactory.createCache('my-cache'); diff --git a/build_examples/webpack_es6/app.js b/build_examples/webpack_es6/app.js index 08fae61..b4a4b2b 100644 --- a/build_examples/webpack_es6/app.js +++ b/build_examples/webpack_es6/app.js @@ -1,5 +1,5 @@ // normally this would be var import CacheFactory from 'cachefactory' -import CacheFactory from '../../'; +import {CacheFactory} from '../../'; const cacheFactory = new CacheFactory(); let cache = cacheFactory.createCache('my-cache'); diff --git a/dist/cachefactory.d.ts b/dist/cachefactory.d.ts index 406fff6..8890922 100644 --- a/dist/cachefactory.d.ts +++ b/dist/cachefactory.d.ts @@ -1,105 +1,103 @@ -declare module 'cachefactory' { - type DeleteOnExpire = 'none'|'passive'|'aggressive' - type StorageMode = 'memory'|'localStorage'|'sessionStorage' - interface StorageImpl { - setItem: Function, - getItem: Function, - removeItem: Function - } - class BinaryHeap { - new(weightFunc?: Function, compareFunc?: Function): BinaryHeap - peek(): any - pop(): any - push(node: any): void - remove(node: any): void - removeAll(): void - size(): number - } - interface Utils { - equals(a: any, b: any): boolean - fromJson(value: string): any - isFunction(value: any): boolean - isNumber(value: any): boolean - isObject(value: any): boolean - isString(value: any): boolean - Promise?: PromiseConstructor - } - interface CacheOptions { - cacheFlushInterval?: number - capacity?: number - deleteOnExpire?: DeleteOnExpire - enable?: boolean - maxAge?: number - onExpire?: Function - recycleFreq?: number - storageMode?: StorageMode - storageImpl?: StorageImpl - storagePrefix?: string - storeOnResolve?: boolean - storeOnReject?: boolean - } - interface ItemInfo { - accessed?: number - created?: number - expires?: number - isExpired?: boolean - } - interface GetPutOptions extends ItemInfo { - maxAge?: number - onExpire?: Function - storeOnResolve?: boolean - storeOnReject?: boolean - } - interface CacheInfo extends CacheOptions { - id: string - size: number - } - class Cache { - id: string - destroy(): void - disable(): void - enable(): void - get(key: string|number, options?: GetPutOptions): any - info(key: string|number): CacheInfo|ItemInfo - keys(): Array - keySet(): { [key: string]: string|number } - put(key: string|number, value: any, options?: GetPutOptions): any - remove(key: string|number): any - removeAll(): void - removeExpired(): { [key: string]: any } - setCacheFlushInterval(cacheFlushInterval: number): void - setCapacity(capacity: number): void - setDeleteOnExpire(deleteOnExpire: DeleteOnExpire, setRecycleFreq?: boolean): void - setMaxAge(maxAge: number): void - setOnExpire(onExpire: Function): any - setOptions(cacheOptions: CacheOptions, strict?: boolean): void - setRecycleFreq(recycleFreq: boolean): void - setStorageMode(storageMode: StorageMode, storageImpl?: StorageImpl): void - touch(key: string|number): void - values(): any[] - } - interface CacheFactoryInfo extends CacheOptions { - size: number - caches: { [id: string]: CacheInfo } - } - class CacheFactory { - static BinaryHeap: typeof BinaryHeap - static Cache: typeof Cache - static defaults: CacheOptions - static utils: Utils - clearAll(): void - createCache(id: string, options?: CacheOptions): Cache - exists(id: string): boolean - get(id: string): Cache - info(): CacheFactoryInfo - destroy(id: string): void - destroyAll(): void - disableAll(): void - enabledAll(): void - keys(): string[] - keySet(): { [id: string]: string } - removeExpiredFromAll(): { [id: string]: { [key: string]: any } } - touchAll(): void - } - export = CacheFactory +export class CacheFactory { + clearAll(): void; + createCache(id: string, options?: CacheOptions): Cache; + exists(id: string): boolean; + get(id: string): Cache; + info(): CacheFactoryInfo; + destroy(id: string): void; + destroyAll(): void; + disableAll(): void; + enabledAll(): void; + keys(): string[]; + keySet(): {[id: string]: string}; + removeExpiredFromAll(): {[id: string]: {[key: string]: any}}; + touchAll(): void; } + +export class Cache { + id: string; + destroy(): void; + disable(): void; + enable(): void; + get(key: string|number, options?: GetPutOptions): any; + info(key: string|number): CacheInfo|ItemInfo; + keys(): (string|number)[]; + keySet(): {[key: string]: string|number}; + put(key: string|number, value: any, options?: GetPutOptions): any; + remove(key: string|number): any; + removeAll(): void; + removeExpired(): {[key: string]: any}; + setCacheFlushInterval(cacheFlushInterval: number): void; + setCapacity(capacity: number): void; + setDeleteOnExpire(deleteOnExpire: DeleteOnExpire, setRecycleFreq?: boolean): void; + setMaxAge(maxAge: number): void; + setOnExpire(onExpire: Function): any; + setOptions(cacheOptions: CacheOptions, strict?: boolean): void; + setRecycleFreq(recycleFreq: boolean): void; + setStorageMode(storageMode: StorageMode, storageImpl?: StorageImpl): void; + touch(key: string|number): void; + values(): any[]; +} +export type DeleteOnExpire = "none"|"passive"|"aggressive"; +export type StorageMode = "memory"|"localStorage"|"sessionStorage"; +export interface StorageImpl { + setItem(key: string|number, value: any): void; + getItem(key: string|number): any; + removeItem(key: string|number): void; +} +export class BinaryHeap { + constructor(weightFunc?: Function, compareFunc?: Function); + peek(): any; + pop(): any; + push(node: any): void; + remove(node: any): void; + removeAll(): void; + size(): number; +} +export interface Utils { + equals(a: any, b: any): boolean; + fromJson(value: string): any; + isFunction(value: any): boolean; + isNumber(value: any): boolean; + isObject(value: any): boolean; + isString(value: any): boolean; + Promise?: PromiseConstructor; +} +export type OnExpireCallback=(key: string, value: any, done?: Function) => void; +export interface CacheOptions { + cacheFlushInterval?: number; + capacity?: number; + deleteOnExpire?: DeleteOnExpire; + enable?: boolean; + maxAge?: number; + onExpire?: OnExpireCallback; + recycleFreq?: number; + storageMode?: StorageMode; + storageImpl?: StorageImpl; + storagePrefix?: string; + storeOnResolve?: boolean; + storeOnReject?: boolean; +} +export interface ItemInfo { + accessed?: number; + created?: number; + expires?: number; + isExpired?: boolean; +} +export interface GetPutOptions extends ItemInfo { + maxAge?: number; + onExpire?: OnExpireCallback; + storeOnResolve?: boolean; + storeOnReject?: boolean; +} +export interface CacheInfo extends CacheOptions { + id: string; + size: number; +} +export interface CacheFactoryInfo extends CacheOptions { + size: number; + caches: {[id: string]: CacheInfo}; +} + +export const defaults: CacheOptions; +export const utils: Utils; diff --git a/karma.conf.js b/karma.conf.js index 0a3523b..90300a6 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -4,7 +4,7 @@ module.exports = function (config) { frameworks: ['mocha', 'chai', 'sinon'], browsers: ['PhantomJS'], files: [ - 'node_modules/es6-promise/dist/es6-promise.js', + 'node_modules/es6-promise/dist/es6-promise.auto.js', 'node_modules/yabh/src/index.js', 'src/utils.js', 'src/defaults.js', diff --git a/package.json b/package.json index 87a53e2..c1ff515 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "homepage": "https://github.com/jmdobry/CacheFactory", "main": "dist/cachefactory.js", "jsnext:main": "dist/cachefactory.es2015.js", + "module": "dist/cachefactory.es2015.js", "typings": "dist/cachefactory.d.ts", "files": [ "dist/", @@ -57,32 +58,32 @@ "ci": "npm test && cat ./coverage/PhantomJS*/lcov.info | codecov" }, "devDependencies": { - "babel-core": "6.14.0", - "babel-eslint": "6.1.2", - "babel-plugin-transform-es2015-modules-umd": "6.12.0", - "babel-preset-es2015": "6.14.0", - "babel-preset-es2015-rollup": "1.2.0", - "chai": "3.5.0", - "codecov": "1.0.1", - "es6-promise": "3.3.1", - "jsdoc": "3.4.1", - "karma": "1.3.0", - "karma-babel-preprocessor": "6.0.1", - "karma-chai": "0.1.0", - "karma-chrome-launcher": "2.0.0", - "karma-coverage": "1.1.1", - "karma-mocha": "1.1.1", - "karma-phantomjs-launcher": "1.0.2", - "karma-sinon": "1.0.5", - "karma-sourcemap-loader": "0.3.7", - "minami": "1.1.1", - "mocha": "3.0.2", - "phantomjs-prebuilt": "2.1.12", - "rollup": "0.35.11", - "rollup-plugin-babel": "2.6.1", - "sinon": "1.17.5", - "standard": "8.1.0", - "uglify-js": "2.7.3", - "yabh": "1.2.0" + "babel-core": "^6.21.0", + "babel-eslint": "^7.1.1", + "babel-plugin-transform-es2015-modules-umd": "^6.18.0", + "babel-preset-es2015": "^6.18.0", + "babel-preset-es2015-rollup": "^3.0.0", + "chai": "^3.5.0", + "codecov": "^1.0.1", + "es6-promise": "^4.0.5", + "jsdoc": "^3.4.1", + "karma": "^1.3.0", + "karma-babel-preprocessor": "^6.0.1", + "karma-chai": "^0.1.0", + "karma-chrome-launcher": "^2.0.0", + "karma-coverage": "^1.1.1", + "karma-mocha": "^1.1.1", + "karma-phantomjs-launcher": "^1.0.2", + "karma-sinon": "^1.0.5", + "karma-sourcemap-loader": "^0.3.7", + "minami": "^1.1.1", + "mocha": "^3.0.2", + "phantomjs-prebuilt": "^2.1.12", + "rollup": "^0.41.4", + "rollup-plugin-babel": "^2.6.1", + "sinon": "^1.17.5", + "standard": "^8.1.0", + "uglify-js": "^2.7.3", + "yabh": "^1.2.0" } } diff --git a/src/CacheFactory.js b/src/CacheFactory.js index 7a4950a..6358d63 100644 --- a/src/CacheFactory.js +++ b/src/CacheFactory.js @@ -1,3 +1,4 @@ +import defaults from './defaults' /** * A instance of `CacheFactory` holds multiple caches, and provides methods for * manipulating all of the caches at once. @@ -182,8 +183,8 @@ export default class CacheFactory { keys.forEach((cacheId) => { info.caches[cacheId] = this.get(cacheId).info() }) - Object.keys(CacheFactory.defaults).forEach((key, value) => { - info[key] = CacheFactory.defaults[key] + Object.keys(defaults).forEach((key, value) => { + info[key] = defaults[key] }) return info } diff --git a/src/index.js b/src/index.js index 280d370..8f9ec90 100644 --- a/src/index.js +++ b/src/index.js @@ -22,7 +22,7 @@ import utils from './utils' * @example Load into your app via AMD * define('myApp', ['cachefactory'], function (CacheFactory) { ... }) */ -export default CacheFactory +export {CacheFactory} /** * The `BinaryHeap` constructor function. @@ -36,7 +36,7 @@ export default CacheFactory * @see https://github.com/jmdobry/yabh * @type {function} */ -CacheFactory.BinaryHeap = BinaryHeap +export {BinaryHeap} /** * The {@link Cache} constructor function. @@ -51,6 +51,7 @@ CacheFactory.BinaryHeap = BinaryHeap * @type {function} */ CacheFactory.Cache = Cache +export {Cache} /** * The default cache values. Modify this object to change the default values. @@ -68,7 +69,7 @@ CacheFactory.Cache = Cache * @see Cache * @type {object} */ -CacheFactory.defaults = defaults +export {defaults} /** * Utility functions used throughout this library. @@ -85,4 +86,4 @@ CacheFactory.defaults = defaults * @memberof module:cachefactory * @type {object} */ -CacheFactory.utils = utils +export {utils} diff --git a/test/_setup.js b/test/_setup.js index 5748af0..2437525 100644 --- a/test/_setup.js +++ b/test/_setup.js @@ -1,5 +1,5 @@ import { assert } from 'chai' -import CacheFactory from '../src/index' +import { Cache, CacheFactory } from '../src/index' import sinon from 'sinon' window.pp = function pp (obj) { @@ -29,6 +29,7 @@ assert.fail = function (msg) { // Setup global data once export { assert, + Cache, CacheFactory, sinon } diff --git a/test/unit/Cache/Cache.test.js b/test/unit/Cache/Cache.test.js index 924c500..73984df 100644 --- a/test/unit/Cache/Cache.test.js +++ b/test/unit/Cache/Cache.test.js @@ -1,12 +1,10 @@ import { assert, - CacheFactory, + Cache, CACHE_DEFAULTS, TYPES_EXCEPT_STRING } from '../../_setup' -const { Cache } = CacheFactory - describe('Cache', function () { it('should construct an instance of Cache', function () { const cache = new Cache(this.testId) diff --git a/test/unit/Cache/destroy.test.js b/test/unit/Cache/destroy.test.js index e64724f..e1da739 100644 --- a/test/unit/Cache/destroy.test.js +++ b/test/unit/Cache/destroy.test.js @@ -1,10 +1,8 @@ import { assert, - CacheFactory + Cache } from '../../_setup' -const { Cache } = CacheFactory - describe('Cache#destroy', function () { it('should destroy the cache', function () { const mock = { diff --git a/test/unit/Cache/disable.test.js b/test/unit/Cache/disable.test.js index 60c38a7..acfce69 100644 --- a/test/unit/Cache/disable.test.js +++ b/test/unit/Cache/disable.test.js @@ -1,10 +1,8 @@ import { assert, - CacheFactory + Cache } from '../../_setup' -const { Cache } = CacheFactory - describe('Cache#disable', function () { it('should disable the cache', function () { const cache = new Cache(this.testId) diff --git a/test/unit/Cache/enable.test.js b/test/unit/Cache/enable.test.js index 25fbfd5..705193d 100644 --- a/test/unit/Cache/enable.test.js +++ b/test/unit/Cache/enable.test.js @@ -1,10 +1,8 @@ import { assert, - CacheFactory + Cache } from '../../_setup' -const { Cache } = CacheFactory - describe('Cache#enable', function () { it('should enable the cache', function () { const cache = new Cache(this.testId, { diff --git a/test/unit/Cache/get.test.js b/test/unit/Cache/get.test.js index 8edb196..4a509bc 100644 --- a/test/unit/Cache/get.test.js +++ b/test/unit/Cache/get.test.js @@ -1,13 +1,11 @@ import { assert, - CacheFactory, + Cache, TYPES_EXCEPT_STRING_OR_ARRAY_OR_NUMBER, TYPES_EXCEPT_OBJECT, TYPES_EXCEPT_FUNCTION } from '../../_setup' -const { Cache } = CacheFactory - describe('Cache#get', function () { it('should do nothing if the cache is disabled', function () { const cache = new Cache(this.testId) diff --git a/test/unit/Cache/info.test.js b/test/unit/Cache/info.test.js index 5b1e44d..42dc3a2 100644 --- a/test/unit/Cache/info.test.js +++ b/test/unit/Cache/info.test.js @@ -1,10 +1,8 @@ import { assert, - CacheFactory + Cache } from '../../_setup' -const { Cache } = CacheFactory - describe('Cache#info', function () { it('should return the correct values.', function () { const onExpire = function () {} diff --git a/test/unit/Cache/keySet.test.js b/test/unit/Cache/keySet.test.js index 2a84c05..50dc8ce 100644 --- a/test/unit/Cache/keySet.test.js +++ b/test/unit/Cache/keySet.test.js @@ -1,10 +1,8 @@ import { assert, - CacheFactory + Cache } from '../../_setup' -const { Cache } = CacheFactory - describe('Cache#keySet', function () { it('should return the set of keys of all items in the cache.', function () { const itemKeys = ['item1', 'item2', 'item3'] diff --git a/test/unit/Cache/keys.test.js b/test/unit/Cache/keys.test.js index 620f8d9..baef370 100644 --- a/test/unit/Cache/keys.test.js +++ b/test/unit/Cache/keys.test.js @@ -1,10 +1,8 @@ import { assert, - CacheFactory + Cache } from '../../_setup' -const { Cache } = CacheFactory - describe('Cache#keys', function () { it('should return the array of keys of all items in the cache.', function () { const itemKeys = ['item1', 'item2', 'item3'] diff --git a/test/unit/Cache/put.test.js b/test/unit/Cache/put.test.js index 7b3d0bd..5074b1a 100644 --- a/test/unit/Cache/put.test.js +++ b/test/unit/Cache/put.test.js @@ -1,11 +1,9 @@ import { assert, - CacheFactory, + Cache, TYPES_EXCEPT_STRING_OR_NUMBER } from '../../_setup' -const { Cache } = CacheFactory - describe('Cache#put', function () { it('should do nothing if the cache is disabled', function () { const cache = new Cache(this.testId, { enabled: false }) diff --git a/test/unit/Cache/remove.test.js b/test/unit/Cache/remove.test.js index 8c21bda..9af6b4a 100644 --- a/test/unit/Cache/remove.test.js +++ b/test/unit/Cache/remove.test.js @@ -1,10 +1,8 @@ import { assert, - CacheFactory + Cache } from '../../_setup' -const { Cache } = CacheFactory - describe('Cache#remove', function () { it('should remove the item with the specified key.', function () { const cache = new Cache(this.testId) diff --git a/test/unit/Cache/removeAll.test.js b/test/unit/Cache/removeAll.test.js index ce2dc23..9189ce1 100644 --- a/test/unit/Cache/removeAll.test.js +++ b/test/unit/Cache/removeAll.test.js @@ -1,10 +1,8 @@ import { assert, - CacheFactory + Cache } from '../../_setup' -const { Cache } = CacheFactory - describe('Cache#removeAll', function () { it('should remove all items in the cache.', function () { const cache = new Cache('DSCache.removeAll.cache') diff --git a/test/unit/Cache/removeExpired.test.js b/test/unit/Cache/removeExpired.test.js index de66529..e98a650 100644 --- a/test/unit/Cache/removeExpired.test.js +++ b/test/unit/Cache/removeExpired.test.js @@ -1,10 +1,8 @@ import { assert, - CacheFactory + Cache } from '../../_setup' -const { Cache } = CacheFactory - describe('Cache#removeExpired', function () { it('should remove all expired items when deleteOnExpire is "none"', function (done) { const cache = new Cache(this.testId, { diff --git a/test/unit/Cache/setCacheFlushInterval.test.js b/test/unit/Cache/setCacheFlushInterval.test.js index f23d609..66cf480 100644 --- a/test/unit/Cache/setCacheFlushInterval.test.js +++ b/test/unit/Cache/setCacheFlushInterval.test.js @@ -1,11 +1,9 @@ import { assert, - CacheFactory, + Cache, TYPES_EXCEPT_NUMBER } from '../../_setup' -const { Cache } = CacheFactory - describe('Cache#setCacheFlushInterval', function () { it('should set cacheFlushInterval', function () { const cache = new Cache(this.testId) diff --git a/test/unit/Cache/setCapacity.test.js b/test/unit/Cache/setCapacity.test.js index f323da3..e166d6c 100644 --- a/test/unit/Cache/setCapacity.test.js +++ b/test/unit/Cache/setCapacity.test.js @@ -1,11 +1,9 @@ import { assert, - CacheFactory, + Cache, TYPES_EXCEPT_NUMBER } from '../../_setup' -const { Cache } = CacheFactory - describe('Cache#setCapacity', function () { it('should set capacity', function () { const cache = new Cache(this.testId) diff --git a/test/unit/Cache/setDeleteOnExpire.test.js b/test/unit/Cache/setDeleteOnExpire.test.js index f037bdf..4013b3d 100644 --- a/test/unit/Cache/setDeleteOnExpire.test.js +++ b/test/unit/Cache/setDeleteOnExpire.test.js @@ -1,11 +1,9 @@ import { assert, - CacheFactory, + Cache, TYPES_EXCEPT_STRING } from '../../_setup' -const { Cache } = CacheFactory - describe('Cache#setDeleteOnExpire', function () { it('should set deleteOnExpire', function () { const cache = new Cache(this.testId) diff --git a/test/unit/Cache/setMaxAge.test.js b/test/unit/Cache/setMaxAge.test.js index 857aa56..564875d 100644 --- a/test/unit/Cache/setMaxAge.test.js +++ b/test/unit/Cache/setMaxAge.test.js @@ -1,11 +1,9 @@ import { assert, - CacheFactory, + Cache, TYPES_EXCEPT_NUMBER } from '../../_setup' -const { Cache } = CacheFactory - describe('Cache#setMaxAge', function () { it('should set maxAge', function () { const cache = new Cache(this.testId) diff --git a/test/unit/Cache/setOnExpire.test.js b/test/unit/Cache/setOnExpire.test.js index 8a9d64f..31057f4 100644 --- a/test/unit/Cache/setOnExpire.test.js +++ b/test/unit/Cache/setOnExpire.test.js @@ -1,11 +1,9 @@ import { assert, - CacheFactory, + Cache, TYPES_EXCEPT_FUNCTION } from '../../_setup' -const { Cache } = CacheFactory - describe('Cache#setOnExpire', function () { it('should set onExpire', function () { function onExpire () {} diff --git a/test/unit/Cache/setOptions.test.js b/test/unit/Cache/setOptions.test.js index 8c42faa..684ff35 100644 --- a/test/unit/Cache/setOptions.test.js +++ b/test/unit/Cache/setOptions.test.js @@ -1,11 +1,9 @@ import { assert, - CacheFactory, + Cache, TYPES_EXCEPT_OBJECT } from '../../_setup' -const { Cache } = CacheFactory - describe('Cache#setOptions', function () { it('should throw an error if "options" is not an object', function () { const cache = new Cache(this.testId) diff --git a/test/unit/Cache/setRecycleFreq.test.js b/test/unit/Cache/setRecycleFreq.test.js index 0918b62..8f638ab 100644 --- a/test/unit/Cache/setRecycleFreq.test.js +++ b/test/unit/Cache/setRecycleFreq.test.js @@ -1,12 +1,10 @@ import { assert, - CacheFactory, + Cache, CACHE_DEFAULTS, TYPES_EXCEPT_NUMBER } from '../../_setup' -const { Cache } = CacheFactory - describe('Cache#setRecycleFreq', function () { it('should set recycleFreq if deleteOneExpire is "aggressive"', function () { const cache = new Cache(this.testId, { deleteOnExpire: 'aggressive' }) diff --git a/test/unit/Cache/setStorageMode.test.js b/test/unit/Cache/setStorageMode.test.js index 4fec466..502aec5 100644 --- a/test/unit/Cache/setStorageMode.test.js +++ b/test/unit/Cache/setStorageMode.test.js @@ -1,20 +1,20 @@ import { assert, - CacheFactory, + Cache, TYPES_EXCEPT_OBJECT, TYPES_EXCEPT_STRING } from '../../_setup' describe('Cache#setStorageMode', function () { it('should set storageMode', function () { - const cache = new CacheFactory.Cache(this.testId) + const cache = new Cache(this.testId) cache.setStorageMode('localStorage') assert.strictEqual(cache.storageMode, 'localStorage') assert.strictEqual(cache.$$storage(), localStorage) }) it('should set storageImpl', function () { - const cache = new CacheFactory.Cache(this.testId) + const cache = new Cache(this.testId) const polyfill = { setItem: () => {}, getItem: () => {}, @@ -26,7 +26,7 @@ describe('Cache#setStorageMode', function () { }) it('should throw a TypeError if storageMode is not a string', function () { - const cache = new CacheFactory.Cache(this.testId) + const cache = new Cache(this.testId) TYPES_EXCEPT_STRING.forEach(function (value) { if (value === null || value === undefined) { return @@ -38,14 +38,14 @@ describe('Cache#setStorageMode', function () { }) it('should throw an Error if storageMode is not an allowed value', function () { - const cache = new CacheFactory.Cache(this.testId) + const cache = new Cache(this.testId) assert.throws(() => { cache.setStorageMode('foo') }, Error, '"storageMode" must be "memory", "localStorage", or "sessionStorage"!') }) it('should throw a TypeError if storageImpl is not an object', function () { - const cache = new CacheFactory.Cache(this.testId) + const cache = new Cache(this.testId) TYPES_EXCEPT_OBJECT.forEach(function (value) { if (!value) { return @@ -58,21 +58,21 @@ describe('Cache#setStorageMode', function () { it('should throw an Error if storageImpl down not implement setItem', function () { assert.throws(() => { - const cache = new CacheFactory.Cache(this.testId) + const cache = new Cache(this.testId) cache.setStorageMode('localStorage', {}) }, Error, '"storageImpl" must implement "setItem(key, value)"!') }) it('should throw an Error if storageImpl down not implement getItem', function () { assert.throws(() => { - const cache = new CacheFactory.Cache(this.testId) + const cache = new Cache(this.testId) cache.setStorageMode('localStorage', { setItem: () => {} }) }, Error, '"storageImpl" must implement "getItem(key)"!') }) it('should throw an Error if storageImpl down not implement removeItem', function () { assert.throws(() => { - const cache = new CacheFactory.Cache(this.testId) + const cache = new Cache(this.testId) cache.setStorageMode('localStorage', { setItem: () => {}, getItem: () => {} }) }, Error, '"storageImpl" must implement "removeItem(key)"!') }) @@ -89,7 +89,7 @@ describe('Cache#setStorageMode', function () { const orig = localStorage.setItem localStorage.setItem = setItem if (localStorage.setItem === setItem) { - const cache = new CacheFactory.Cache('CacheFactory.cache', options) + const cache = new Cache('CacheFactory.cache', options) assert.isDefined(cache) assert.strictEqual(cache.info().id, 'CacheFactory.cache') assert.strictEqual(cache.info().deleteOnExpire, options.deleteOnExpire) @@ -113,7 +113,7 @@ describe('Cache#setStorageMode', function () { const orig = sessionStorage.setItem sessionStorage.setItem = setItem if (sessionStorage.setItem === setItem) { - const cache = new CacheFactory.Cache('CacheFactory.cache', options) + const cache = new Cache('CacheFactory.cache', options) assert.isDefined(cache) assert.strictEqual(cache.info().id, 'CacheFactory.cache') assert.strictEqual(cache.info().deleteOnExpire, options.deleteOnExpire) diff --git a/test/unit/CacheFactory/clearAll.test.js b/test/unit/CacheFactory/clearAll.test.js index c7bc275..ae9c086 100644 --- a/test/unit/CacheFactory/clearAll.test.js +++ b/test/unit/CacheFactory/clearAll.test.js @@ -1,15 +1,15 @@ import { assert, - CacheFactory + Cache } from '../../_setup' describe('CacheFactory#clearAll', function () { beforeEach(function () { - sinon.stub(CacheFactory.Cache.prototype, 'removeAll') + sinon.stub(Cache.prototype, 'removeAll') }) afterEach(function () { - CacheFactory.Cache.prototype.removeAll.restore() + Cache.prototype.removeAll.restore() }) it('should call removeAll on all caches', function () { diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..45295ad --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,8 @@ +{ + "compilerOptions": { + "lib": [ + "dom", + "es2015" + ] + } +}