Skip to content

Commit

Permalink
feat: Module export make consistent between multiple bundles (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
Delagen authored and jmdobry committed Feb 26, 2017
1 parent 93919f5 commit c5b478b
Show file tree
Hide file tree
Showing 33 changed files with 188 additions and 216 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion build_examples/browserify/app.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down
4 changes: 2 additions & 2 deletions build_examples/r.js/app.js
Original file line number Diff line number Diff line change
@@ -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');
});
2 changes: 1 addition & 1 deletion build_examples/webpack/app.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down
2 changes: 1 addition & 1 deletion build_examples/webpack_es6/app.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down
206 changes: 102 additions & 104 deletions dist/cachefactory.d.ts
Original file line number Diff line number Diff line change
@@ -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<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[]
}
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;
2 changes: 1 addition & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
55 changes: 28 additions & 27 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/",
Expand Down Expand Up @@ -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"
}
}
5 changes: 3 additions & 2 deletions src/CacheFactory.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
}
Expand Down
9 changes: 5 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import utils from './utils'
* @example <caption>Load into your app via AMD</caption>
* define('myApp', ['cachefactory'], function (CacheFactory) { ... })
*/
export default CacheFactory
export {CacheFactory}

/**
* The `BinaryHeap` constructor function.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -68,7 +69,7 @@ CacheFactory.Cache = Cache
* @see Cache
* @type {object}
*/
CacheFactory.defaults = defaults
export {defaults}

/**
* Utility functions used throughout this library.
Expand All @@ -85,4 +86,4 @@ CacheFactory.defaults = defaults
* @memberof module:cachefactory
* @type {object}
*/
CacheFactory.utils = utils
export {utils}
3 changes: 2 additions & 1 deletion test/_setup.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -29,6 +29,7 @@ assert.fail = function (msg) {
// Setup global data once
export {
assert,
Cache,
CacheFactory,
sinon
}
Expand Down
Loading

0 comments on commit c5b478b

Please sign in to comment.