Skip to content

Commit d0309eb

Browse files
DelagenSergey Vohmyanin
authored and
Sergey Vohmyanin
committed
feat: Module export make consistent between multiple bundles (jmdobry#29)
1 parent 9562e45 commit d0309eb

32 files changed

+147
-177
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ bower install --save cachefactory
2828
## Quick Start
2929

3030
```js
31-
import CacheFactory from 'cachefactory';
31+
import {CacheFactory} from 'cachefactory';
3232

3333
const cacheFactory = new CacheFactory();
3434
let cache;

build_examples/browserify/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// normally this would be var CacheFactory = require('cachefactory');
2-
var CacheFactory = require('../../');
2+
var CacheFactory = require('../../').CacheFactory;
33

44
var cacheFactory = new CacheFactory();
55
var cache = cacheFactory.createCache('my-cache');

build_examples/r.js/app.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
define('app', [
22
'cachefactory'
3-
], function (CacheFactory) {
4-
var cacheFactory = new CacheFactory();
3+
], function (CacheFactoryModule) {
4+
var cacheFactory = new CacheFactoryModule.CacheFactory();
55
return cacheFactory.createCache('my-cache');
66
});

build_examples/webpack/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// normally this would be var CacheFactory = require('cachefactory');
2-
var CacheFactory = require('../../');
2+
var CacheFactory = require('../../').CacheFactory;
33

44
var cacheFactory = new CacheFactory();
55
var cache = cacheFactory.createCache('my-cache');

build_examples/webpack_es6/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// normally this would be var import CacheFactory from 'cachefactory'
2-
import CacheFactory from '../../';
2+
import {CacheFactory} from '../../';
33

44
const cacheFactory = new CacheFactory();
55
let cache = cacheFactory.createCache('my-cache');

dist/cachefactory.d.ts

Lines changed: 89 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
declare class CacheFactory {
2-
static defaults: CacheFactory.CacheOptions;
3-
static utils: CacheFactory.Utils;
1+
export class CacheFactory {
42
clearAll(): void;
5-
createCache(id: string, options?: CacheFactory.CacheOptions): CacheFactory.Cache;
3+
createCache(id: string, options?: CacheOptions): Cache;
64
exists(id: string): boolean;
7-
get(id: string): CacheFactory.Cache;
8-
info(): CacheFactory.CacheFactoryInfo;
5+
get(id: string): Cache;
6+
info(): CacheFactoryInfo;
97
destroy(id: string): void;
108
destroyAll(): void;
119
disableAll(): void;
@@ -16,91 +14,90 @@ declare class CacheFactory {
1614
touchAll(): void;
1715
}
1816

19-
declare namespace CacheFactory {
20-
export class Cache {
21-
id: string;
22-
destroy(): void;
23-
disable(): void;
24-
enable(): void;
25-
get(key: string|number, options?: GetPutOptions): any;
26-
info(key: string|number): CacheInfo|ItemInfo;
27-
keys(): (string|number)[];
28-
keySet(): {[key: string]: string|number};
29-
put(key: string|number, value: any, options?: GetPutOptions): any;
30-
remove(key: string|number): any;
31-
removeAll(): void;
32-
removeExpired(): {[key: string]: any};
33-
setCacheFlushInterval(cacheFlushInterval: number): void;
34-
setCapacity(capacity: number): void;
35-
setDeleteOnExpire(deleteOnExpire: DeleteOnExpire, setRecycleFreq?: boolean): void;
36-
setMaxAge(maxAge: number): void;
37-
setOnExpire(onExpire: Function): any;
38-
setOptions(cacheOptions: CacheOptions, strict?: boolean): void;
39-
setRecycleFreq(recycleFreq: boolean): void;
40-
setStorageMode(storageMode: StorageMode, storageImpl?: StorageImpl): void;
41-
touch(key: string|number): void;
42-
values(): any[];
43-
}
44-
export type DeleteOnExpire = "none"|"passive"|"aggressive";
45-
export type StorageMode = "memory"|"localStorage"|"sessionStorage";
46-
export interface StorageImpl {
47-
setItem(key: string|number, value: any): void;
48-
getItem(key: string|number): any;
49-
removeItem(key: string|number): void;
50-
}
51-
export class BinaryHeap {
52-
constructor(weightFunc?: Function, compareFunc?: Function);
53-
peek(): any;
54-
pop(): any;
55-
push(node: any): void;
56-
remove(node: any): void;
57-
removeAll(): void;
58-
size(): number;
59-
}
60-
export interface Utils {
61-
equals(a: any, b: any): boolean;
62-
fromJson(value: string): any;
63-
isFunction(value: any): boolean;
64-
isNumber(value: any): boolean;
65-
isObject(value: any): boolean;
66-
isString(value: any): boolean;
67-
Promise?: PromiseConstructor;
68-
}
69-
export type OnExpireCallback=(key:string, value:any, done?:Function)=>void;
70-
export interface CacheOptions {
71-
cacheFlushInterval?: number;
72-
capacity?: number;
73-
deleteOnExpire?: DeleteOnExpire;
74-
enable?: boolean;
75-
maxAge?: number;
76-
onExpire?: OnExpireCallback;
77-
recycleFreq?: number;
78-
storageMode?: StorageMode;
79-
storageImpl?: StorageImpl;
80-
storagePrefix?: string;
81-
storeOnResolve?: boolean;
82-
storeOnReject?: boolean;
83-
}
84-
export interface ItemInfo {
85-
accessed?: number;
86-
created?: number;
87-
expires?: number;
88-
isExpired?: boolean;
89-
}
90-
export interface GetPutOptions extends ItemInfo {
91-
maxAge?: number;
92-
onExpire?: OnExpireCallback;
93-
storeOnResolve?: boolean;
94-
storeOnReject?: boolean;
95-
}
96-
export interface CacheInfo extends CacheOptions {
97-
id: string;
98-
size: number;
99-
}
100-
export interface CacheFactoryInfo extends CacheOptions {
101-
size: number;
102-
caches: {[id: string]: CacheInfo};
103-
}
17+
export class Cache {
18+
id: string;
19+
destroy(): void;
20+
disable(): void;
21+
enable(): void;
22+
get(key: string|number, options?: GetPutOptions): any;
23+
info(key: string|number): CacheInfo|ItemInfo;
24+
keys(): (string|number)[];
25+
keySet(): {[key: string]: string|number};
26+
put(key: string|number, value: any, options?: GetPutOptions): any;
27+
remove(key: string|number): any;
28+
removeAll(): void;
29+
removeExpired(): {[key: string]: any};
30+
setCacheFlushInterval(cacheFlushInterval: number): void;
31+
setCapacity(capacity: number): void;
32+
setDeleteOnExpire(deleteOnExpire: DeleteOnExpire, setRecycleFreq?: boolean): void;
33+
setMaxAge(maxAge: number): void;
34+
setOnExpire(onExpire: Function): any;
35+
setOptions(cacheOptions: CacheOptions, strict?: boolean): void;
36+
setRecycleFreq(recycleFreq: boolean): void;
37+
setStorageMode(storageMode: StorageMode, storageImpl?: StorageImpl): void;
38+
touch(key: string|number): void;
39+
values(): any[];
40+
}
41+
export type DeleteOnExpire = "none"|"passive"|"aggressive";
42+
export type StorageMode = "memory"|"localStorage"|"sessionStorage";
43+
export interface StorageImpl {
44+
setItem(key: string|number, value: any): void;
45+
getItem(key: string|number): any;
46+
removeItem(key: string|number): void;
47+
}
48+
export class BinaryHeap {
49+
constructor(weightFunc?: Function, compareFunc?: Function);
50+
peek(): any;
51+
pop(): any;
52+
push(node: any): void;
53+
remove(node: any): void;
54+
removeAll(): void;
55+
size(): number;
56+
}
57+
export interface Utils {
58+
equals(a: any, b: any): boolean;
59+
fromJson(value: string): any;
60+
isFunction(value: any): boolean;
61+
isNumber(value: any): boolean;
62+
isObject(value: any): boolean;
63+
isString(value: any): boolean;
64+
Promise?: PromiseConstructor;
65+
}
66+
export type OnExpireCallback=(key: string, value: any, done?: Function) => void;
67+
export interface CacheOptions {
68+
cacheFlushInterval?: number;
69+
capacity?: number;
70+
deleteOnExpire?: DeleteOnExpire;
71+
enable?: boolean;
72+
maxAge?: number;
73+
onExpire?: OnExpireCallback;
74+
recycleFreq?: number;
75+
storageMode?: StorageMode;
76+
storageImpl?: StorageImpl;
77+
storagePrefix?: string;
78+
storeOnResolve?: boolean;
79+
storeOnReject?: boolean;
80+
}
81+
export interface ItemInfo {
82+
accessed?: number;
83+
created?: number;
84+
expires?: number;
85+
isExpired?: boolean;
86+
}
87+
export interface GetPutOptions extends ItemInfo {
88+
maxAge?: number;
89+
onExpire?: OnExpireCallback;
90+
storeOnResolve?: boolean;
91+
storeOnReject?: boolean;
92+
}
93+
export interface CacheInfo extends CacheOptions {
94+
id: string;
95+
size: number;
96+
}
97+
export interface CacheFactoryInfo extends CacheOptions {
98+
size: number;
99+
caches: {[id: string]: CacheInfo};
104100
}
105101

106-
export = CacheFactory;
102+
export const defaults: CacheOptions;
103+
export const utils: Utils;

karma.conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module.exports = function (config) {
44
frameworks: ['mocha', 'chai', 'sinon'],
55
browsers: ['PhantomJS'],
66
files: [
7-
'node_modules/es6-promise/dist/es6-promise.js',
7+
'node_modules/es6-promise/dist/es6-promise.auto.js',
88
'node_modules/yabh/src/index.js',
99
'src/utils.js',
1010
'src/defaults.js',

src/CacheFactory.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import defaults from './defaults'
12
/**
23
* A instance of `CacheFactory` holds multiple caches, and provides methods for
34
* manipulating all of the caches at once.
@@ -182,8 +183,8 @@ export default class CacheFactory {
182183
keys.forEach((cacheId) => {
183184
info.caches[cacheId] = this.get(cacheId).info()
184185
})
185-
Object.keys(CacheFactory.defaults).forEach((key, value) => {
186-
info[key] = CacheFactory.defaults[key]
186+
Object.keys(defaults).forEach((key, value) => {
187+
info[key] = defaults[key]
187188
})
188189
return info
189190
}

src/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import utils from './utils'
2222
* @example <caption>Load into your app via AMD</caption>
2323
* define('myApp', ['cachefactory'], function (CacheFactory) { ... })
2424
*/
25-
export default CacheFactory
25+
export {CacheFactory}
2626

2727
/**
2828
* The `BinaryHeap` constructor function.
@@ -36,7 +36,7 @@ export default CacheFactory
3636
* @see https://github.com/jmdobry/yabh
3737
* @type {function}
3838
*/
39-
CacheFactory.BinaryHeap = BinaryHeap
39+
export {BinaryHeap}
4040

4141
/**
4242
* The {@link Cache} constructor function.
@@ -51,6 +51,7 @@ CacheFactory.BinaryHeap = BinaryHeap
5151
* @type {function}
5252
*/
5353
CacheFactory.Cache = Cache
54+
export {Cache}
5455

5556
/**
5657
* The default cache values. Modify this object to change the default values.
@@ -68,7 +69,7 @@ CacheFactory.Cache = Cache
6869
* @see Cache
6970
* @type {object}
7071
*/
71-
CacheFactory.defaults = defaults
72+
export {defaults}
7273

7374
/**
7475
* Utility functions used throughout this library.
@@ -85,4 +86,4 @@ CacheFactory.defaults = defaults
8586
* @memberof module:cachefactory
8687
* @type {object}
8788
*/
88-
CacheFactory.utils = utils
89+
export {utils}

test/_setup.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { assert } from 'chai'
2-
import CacheFactory from '../src/index'
2+
import { Cache, CacheFactory } from '../src/index'
33
import sinon from 'sinon'
44

55
window.pp = function pp (obj) {
@@ -29,6 +29,7 @@ assert.fail = function (msg) {
2929
// Setup global data once
3030
export {
3131
assert,
32+
Cache,
3233
CacheFactory,
3334
sinon
3435
}

test/unit/Cache/Cache.test.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import {
22
assert,
3-
CacheFactory,
3+
Cache,
44
CACHE_DEFAULTS,
55
TYPES_EXCEPT_STRING
66
} from '../../_setup'
77

8-
const { Cache } = CacheFactory
9-
108
describe('Cache', function () {
119
it('should construct an instance of Cache', function () {
1210
const cache = new Cache(this.testId)

test/unit/Cache/destroy.test.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import {
22
assert,
3-
CacheFactory
3+
Cache
44
} from '../../_setup'
55

6-
const { Cache } = CacheFactory
7-
86
describe('Cache#destroy', function () {
97
it('should destroy the cache', function () {
108
const mock = {

test/unit/Cache/disable.test.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import {
22
assert,
3-
CacheFactory
3+
Cache
44
} from '../../_setup'
55

6-
const { Cache } = CacheFactory
7-
86
describe('Cache#disable', function () {
97
it('should disable the cache', function () {
108
const cache = new Cache(this.testId)

test/unit/Cache/enable.test.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import {
22
assert,
3-
CacheFactory
3+
Cache
44
} from '../../_setup'
55

6-
const { Cache } = CacheFactory
7-
86
describe('Cache#enable', function () {
97
it('should enable the cache', function () {
108
const cache = new Cache(this.testId, {

test/unit/Cache/get.test.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import {
22
assert,
3-
CacheFactory,
3+
Cache,
44
TYPES_EXCEPT_STRING_OR_ARRAY_OR_NUMBER,
55
TYPES_EXCEPT_OBJECT,
66
TYPES_EXCEPT_FUNCTION
77
} from '../../_setup'
88

9-
const { Cache } = CacheFactory
10-
119
describe('Cache#get', function () {
1210
it('should do nothing if the cache is disabled', function () {
1311
const cache = new Cache(this.testId)

test/unit/Cache/info.test.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import {
22
assert,
3-
CacheFactory
3+
Cache
44
} from '../../_setup'
55

6-
const { Cache } = CacheFactory
7-
86
describe('Cache#info', function () {
97
it('should return the correct values.', function () {
108
const onExpire = function () {}

0 commit comments

Comments
 (0)