From 080a9476f34f806a01b8e8625b6acb7e237dc968 Mon Sep 17 00:00:00 2001 From: harry0000 Date: Wed, 13 Sep 2017 06:44:44 +0900 Subject: [PATCH] Update store.js type definitions to v2.0 --- types/storejs/index.d.ts | 41 +++++++++------- types/storejs/storejs-tests.ts | 80 +++++++++++++++++++++++-------- types/storejs/v1/index.d.ts | 28 +++++++++++ types/storejs/v1/storejs-tests.ts | 29 +++++++++++ types/storejs/v1/tsconfig.json | 23 +++++++++ 5 files changed, 165 insertions(+), 36 deletions(-) create mode 100644 types/storejs/v1/index.d.ts create mode 100644 types/storejs/v1/storejs-tests.ts create mode 100644 types/storejs/v1/tsconfig.json diff --git a/types/storejs/index.d.ts b/types/storejs/index.d.ts index 0068d94722..e4d03893ff 100644 --- a/types/storejs/index.d.ts +++ b/types/storejs/index.d.ts @@ -1,26 +1,35 @@ -// Type definitions for store.js 1.3 +// Type definitions for store.js 2.0 // Project: https://github.com/marcuswestin/store.js/ // Definitions by: Vincent Bortone +// harry0000 // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// store.js exposes a simple API for cross browser local storage +// Cross-browser storage for all use cases, used across the web. -interface StoreJSStatic { - set(key: string, value: any): any; - get(key: string): any; - remove(key: string): void; - clear(): void; - enabled: boolean; - disabled: boolean; - transact(key: string, defaultValue: any, transactionFn?: (val: any) => void): void; - getAll(): any; - serialize(value: any): string; - deserialize(value: string): any; - forEach(command: (key: string, value: any) => void): void; +interface StoreJsAPI { + readonly version: string; + readonly enabled: string; + get(key: string, optionalDefaultValue?: any): any; + set(key: string, value: any): any; + remove(key: string): void; + each(callback: (val: any, namespacedKey: string) => void): void; + clearAll(): void; + hasNamespace(namespace: string): boolean; + createStore(plugins?: any[], namespace?: string): StoreJsAPI; + addPlugin(plugin: any): void; + namespace(namespace: string): StoreJsAPI; } -declare var store: StoreJSStatic; +interface StoreJsEngine { + createStore(storages: any[], plugins?: any[], namespace?: string): StoreJsAPI; +} + +declare const store: StoreJsAPI; declare module 'store' { - export = store; + export = store; } +declare const engine: StoreJsEngine; +declare module 'store/src/store-engine' { + export = engine; +} diff --git a/types/storejs/storejs-tests.ts b/types/storejs/storejs-tests.ts index 13c937c3c9..03c3769f9b 100644 --- a/types/storejs/storejs-tests.ts +++ b/types/storejs/storejs-tests.ts @@ -1,31 +1,71 @@ // Tests for storagejs.d.ts import * as store from 'store'; +import * as engine from 'store/src/store-engine'; -// Store 'marcus' at 'username' -store.set('username', 'marcus'); +// https://github.com/marcuswestin/store.js/#api -// Get 'username' -var userName: any = store.get('username'); +// Store current user +store.set('user', { name:'Marcus' }); -var all: Object = store.getAll(); +// Get current user +store.get('user'); -// Remove 'username' -store.remove('username'); +// Remove current user +store.remove('user'); // Clear all keys -store.clear(); - -// Store an object literal - store.js uses JSON.stringify under the hood -store.set('user', { name: 'marcus', likes: 'javascript' }); - -// Get the stored object - store.js uses JSON.parse under the hood -var user: any = store.get('user'); -alert(user.name + ' likes ' + user.likes); - -// Get all stored values -store.getAll().user.name; +store.clearAll(); // Loop over all stored values -store.forEach(function(key, val) { - console.log(key, '==', val); +store.each(function(value, key) { + console.log(key, '==', value) }); + +// https://github.com/marcuswestin/store.js/#write-your-own-plugin + +// Example plugin that stores a version history of every value +declare global { + interface StoreJsAPI { + getHistory(key: string): any[]; + } +} + +const versionHistoryPlugin = function(this: StoreJsAPI) { + const historyStore = this.namespace('history'); + return { + set: function(super_fn: () => any, key: string, value: any) { + const history = historyStore.get(key) || []; + history.push(value); + historyStore.set(key, history); + return super_fn(); + }, + getHistory: function(key: string) { + return historyStore.get(key); + } + } +}; +store.addPlugin(versionHistoryPlugin); +store.set('foo', 'bar 1'); +store.set('foo', 'bar 2'); +store.getHistory('foo'); + +// https://github.com/marcuswestin/store.js/#make-your-own-build + +// Example custom build usage: +declare global { + interface StoreJsAPI { + // expirePlugin + set(key: string, value: any, expiration: number): any; + } +} + +var storages: any[] = [ + //require('store/storages/localStorage'), + //require('store/storages/cookieStorage') +]; +var plugins: any[] = [ + //require('store/plugins/defaults'), + //require('store/plugins/expire') +]; +var myStore = engine.createStore(storages, plugins); +myStore.set('foo', 'bar', new Date().getTime() + 3000); // Using expire plugin to expire in 3 seconds diff --git a/types/storejs/v1/index.d.ts b/types/storejs/v1/index.d.ts new file mode 100644 index 0000000000..0de288efa9 --- /dev/null +++ b/types/storejs/v1/index.d.ts @@ -0,0 +1,28 @@ +// Type definitions for store.js 1.3.20 +// Project: https://github.com/marcuswestin/store.js/ +// Definitions by: Vincent Bortone +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +// store.js exposes a simple API for cross browser local storage + +interface StoreJSStatic { + enabled: boolean; + disabled: boolean; + version: string; + set(key: string, value: any): any; + get(key: string, defaultVal?: any): any; + has(key: string): boolean; + remove(key: string): void; + clear(): void; + transact(key: string, defaultVal: any, transactionFn?: (val: any) => void): void; + getAll(): any; + forEach(command: (key: string, value: any) => void): void; + serialize(value: any): string; + deserialize(value: string): any; +} + +declare var store: StoreJSStatic; +declare module 'store' { + export = store; +} + diff --git a/types/storejs/v1/storejs-tests.ts b/types/storejs/v1/storejs-tests.ts new file mode 100644 index 0000000000..fd0e0f25c3 --- /dev/null +++ b/types/storejs/v1/storejs-tests.ts @@ -0,0 +1,29 @@ +// Tests for storagejs.d.ts +import * as store from 'store'; + +// Store 'marcus' at 'username' +store.set('username', 'marcus'); + +// Get 'username' +store.get('username'); + +// Remove 'username' +store.remove('username'); + +// Clear all keys +store.clear(); + +// Store an object literal - store.js uses JSON.stringify under the hood +store.set('user', { name: 'marcus', likes: 'javascript' }); + +// Get the stored object - store.js uses JSON.parse under the hood +var user = store.get('user'); +alert(user.name + ' likes ' + user.likes); + +// Get all stored values +store.getAll().user.name == 'marcus'; + +// Loop over all stored values +store.forEach(function(key, val) { + console.log(key, '==', val) +}); diff --git a/types/storejs/v1/tsconfig.json b/types/storejs/v1/tsconfig.json new file mode 100644 index 0000000000..92685d36da --- /dev/null +++ b/types/storejs/v1/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "storejs-tests.ts" + ] +} \ No newline at end of file