diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index c1a30bb05..306bea98e 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -1119,6 +1119,7 @@ This document generated by [dt-contributors-generator](https://github.com/vvakam * [:link:](simple-cw-node/simple-cw-node.d.ts) [simple-cw-node](https://github.com/astronaughts/simple-cw-node) by [vvakame](https://github.com/vvakame) * [:link:](simplebar/simplebar.d.ts) [simplebar.js](https://github.com/Grsmto/simplebar) by [Gregor Woiwode](https://github.com/gregonnet) * [:link:](jquery.simplemodal/jquery.simplemodal.d.ts) [SimpleModal](http://www.ericmmartin.com/projects/simplemodal) by [Friedrich von Never](https://github.com/ForNeVeR) +* [:link:](simpleStorage/simplestorage.js.d.ts) [simpleStorage](https://github.com/andris9/simpleStorage) by [Áxel Costas Pena](https://github.com/axelcostaspena) * [:link:](sinon/sinon.d.ts) [Sinon](http://sinonjs.org) by [William Sears](https://github.com/mrbigdog2u) * [:link:](sinon-chai/sinon-chai.d.ts) [sinon-chai](https://github.com/domenic/sinon-chai) by [Kazi Manzur Rashid](https://github.com/kazimanzurrashid), [Jed Mao](https://github.com/jedmao) * [:link:](sinon-chrome/sinon-chrome.d.ts) [Sinon-Chrome](https://github.com/vitalets/sinon-chrome) by [Tim Perry](https://github.com/pimterry) diff --git a/simplestorage.js/simplestorage.js-tests.ts b/simplestorage.js/simplestorage.js-tests.ts new file mode 100644 index 000000000..1e9f4202a --- /dev/null +++ b/simplestorage.js/simplestorage.js-tests.ts @@ -0,0 +1,17 @@ +/// + +var versionTest: string = simpleStorage.version; +var canUseTest: boolean = simpleStorage.canUse(); +var simpleStorageTest1: boolean|Error = simpleStorage.set("string", 7); +var simpleStorageTest2: boolean|Error = simpleStorage.set("string", 7, {}); +var simpleStorageTest3: boolean|Error = simpleStorage.set("string", 7, { TTL: 7 }); +var simpleStorageTest4: boolean|Error = simpleStorage.set("string", undefined); +var simpleStorageTest5: boolean|Error = simpleStorage.set("string", undefined, {}); +var simpleStorageTest6: boolean|Error = simpleStorage.set("string", undefined, { TTL: 7 }); +var getTest: any = simpleStorage.get("string"); +var deleteKeyTest: boolean|Error = simpleStorage.deleteKey("string"); +var setTTLTest: boolean|Error = simpleStorage.setTTL("string", 7); +var getTTLTest: number|boolean = simpleStorage.getTTL("string"); +var flushTest: boolean|Error = simpleStorage.flush(); +var indexTest: [string]|boolean = simpleStorage.index(); +var storageSizeTest: number = simpleStorage.storageSize(); diff --git a/simplestorage.js/simplestorage.js.d.ts b/simplestorage.js/simplestorage.js.d.ts new file mode 100644 index 000000000..bda285aac --- /dev/null +++ b/simplestorage.js/simplestorage.js.d.ts @@ -0,0 +1,110 @@ +// Type definitions for simpleStorage v0.1.3 +// Project: https://github.com/andris9/simpleStorage +// Definitions by: Áxel Costas Pena +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module simplestoragejs { + + /** + * {@link simpleStorage} API is a subset of {@link http://www.jstorage.info/|jStorage} with slight modifications, so for most cases it should work out of the box if you are converting from {@link http://www.jstorage.info/|jStorage}. Main difference is between return values - if an action failed because of an error (storage full, storage not available, invalid data used etc.), you get the error object as the return value. {@link http://www.jstorage.info/|jStorage} never indicated anything if an error occurred. + * @see https://github.com/andris9/simpleStorage#usage + */ + export interface SimpleStorage { + + version: string; + + /** + * Check if local storage can be used. + * Returns true if storage is available. + * @see https://github.com/andris9/simpleStorage#canuse + */ + canUse(): boolean; + + /** + * Store or update a value in local storage. + * Returns true if value was stored, false if value was not stored or {@link Error} object if value was not stored because of an error. + * @param key The key for the value. + * @param value Value to be stored (can be any JSONeable value). + * @param [options] Optional options object. + * @see https://github.com/andris9/simpleStorage#setkey-value-options + */ + set(key: string, value: any, options?: SetOptions): boolean|Error; + + /** + * Retrieve a value from local storage. + * Returns the value for a key or undefined if the key was not found. + * @param key The key to be retrieved. + * @see https://github.com/andris9/simpleStorage#getkey + */ + get(key: string): any; + + /** + * Removes a value from local storage. + * Returns true if the value was deleted, false if the value was not found or {@link Error} object if value was not deleted because of an error. + * @param key The key to be deleted. + * @see https://github.com/andris9/simpleStorage#deletekeykey + */ + deleteKey(key: string): boolean|Error; + + /** + * Set a millisecond timeout. When the timeout is reached, the key is removed automatically from local storage. + * Returns true if ttl was set, false if value was not found or {@link Error} object if ttl was not set because of an error. + * @param key The key to be updated. + * @param ttl Timeout in milliseconds. If the value is 0, timeout is cleared from the key. + * @see https://github.com/andris9/simpleStorage#setttlkey-ttl + */ + setTTL(key: string, ttl: number): boolean|Error; + + /** + * Retrieve remaining milliseconds for a key with TTL. + * Returns the finite number of remaining milliseconds, Infinity if TTL is not set for the selected key or false if the selected key does not exist or is expired. + * @param key The key to be checked. + * @see https://github.com/andris9/simpleStorage#getttlkey + */ + getTTL(key: string): number|boolean; + + /** + * Clear all values. + * Returns true if storage was flushed or {@link Error} object if storage was not flushed because of an error. + * @see https://github.com/andris9/simpleStorage#flush + */ + flush(): boolean|Error; + + /** + * Retrieve all used keys as an array. + * Returns an array of keys. + * @see https://github.com/andris9/simpleStorage#index + */ + index(): [string]|boolean; + + /** + * Get used storage in symbol count. + * @see https://github.com/andris9/simpleStorage#storagesize + */ + storageSize(): number; + } + + /** + * @see https://github.com/andris9/simpleStorage#setkey-value-options + */ + export interface SetOptions { + /** + * Sets the time-to-live (TTL) value in milliseconds for the given key/value. + */ + TTL?: number; + } + +} + +declare module "simpleStorage" { + export = simpleStorage; +} + +/** + * Cross-browser key-value store database to store data locally in the browser. + * {@link simpleStorage} is a fork of {@link http://www.jstorage.info/|jStorage} that only includes the minimal set of features. Basically it is a wrapper for native {@link JSON} + {@link WindowLocalStorage.localStorage|localStorage} with some TTL magic mixed in. + * The module has no dependencies, you can use it as a standalone script (introduces {@link simpleStorage} global) or as an AMD module. All modern browsers (including mobile) are supported, older browsers (IE7, Firefox 3) are not. + * {@link simpleStorage} is very small - about 1kB in size when minimized and gzipped. + * @see https://github.com/andris9/simpleStorage#simplestorage + */ +declare var simpleStorage:simplestoragejs.SimpleStorage;