Update store.js type definitions to v2.0

This commit is contained in:
harry0000
2017-09-13 06:44:44 +09:00
parent c698079f5a
commit 080a9476f3
5 changed files with 165 additions and 36 deletions
+25 -16
View File
@@ -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 <https://github.com/vbortone/>
// harry0000 <https://github.com/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;
}
+60 -20
View File
@@ -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
+28
View File
@@ -0,0 +1,28 @@
// Type definitions for store.js 1.3.20
// Project: https://github.com/marcuswestin/store.js/
// Definitions by: Vincent Bortone <https://github.com/vbortone/>
// 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;
}
+29
View File
@@ -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)
});
+23
View File
@@ -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"
]
}