diff --git a/README.md b/README.md index e220f7bef..3cfeba4d1 100755 --- a/README.md +++ b/README.md @@ -113,6 +113,7 @@ List of Definitions * [jScrollPane](http://jscrollpane.kelvinluck.com) (by [Dániel Tar](https://github.com/qcz)) * [JSDeferred](http://cho45.stfuawsc.com/jsdeferred/) (by [Daisuke Mino](https://github.com/minodisk)) * [JSONEditorOnline](https://github.com/josdejong/jsoneditoronline) (by [Vincent Bortone](https://github.com/vbortone/)) +* [jStorage](http://www.jstorage.info/) (by [Danil Flores](https://github.com/dflor003/)) * [JWPlayer](http://developer.longtailvideo.com/trac/) (by [Martin Duparc](https://github.com/martinduparc/)) * [KeyboardJS](https://github.com/RobertWHurst/KeyboardJS) (by [Vincent Bortone](https://github.com/vbortone/)) * [Knockback](http://kmalakoff.github.com/knockback/) (by [Marcel Binot](https://github.com/docgit)) diff --git a/jstorage/jstorage-tests.ts b/jstorage/jstorage-tests.ts new file mode 100644 index 000000000..a8bd01af4 --- /dev/null +++ b/jstorage/jstorage-tests.ts @@ -0,0 +1,74 @@ +/// + +// Test set first overload +var storedValue = $.jStorage.set("testObj", { foo: 'bar' }); +console.assert(storedValue.foo === "bar"); + +// Test set second overload +$.jStorage.set("testNum", 42, { TTL: 65535 }); +var readValue = $.jStorage.get("testNum"); +console.assert(readValue + 5 === 47); + +// Test deleteKey +if ($.jStorage.deleteKey("testObj") === true) { + console.log('deleted'); +} + +// Test setTTL/getTTL +$.jStorage.setTTL("testNum", 100); +console.assert($.jStorage.getTTL("testNum") === 100); + +// Test flush +console.assert($.jStorage.flush() === true); + +// Test storageObj +var storeObj = $.jStorage.storageObj(); +console.assert(storeObj["testNum"] !== null); + +// Test index +var keys = $.jStorage.index(); +console.assert(keys.length > 0); + +// Test storageSize +var size = $.jStorage.storageSize(); +console.assert(size > 0); + +// Test currentBackend +var currentBackend = $.jStorage.currentBackend(); +console.assert(currentBackend != null && typeof currentBackend.getItem !== "undefined"); + +// Test storageAvailable +var isStorageAvailable = $.jStorage.storageAvailable(); +console.assert(isStorageAvailable === true); + +// Test listenKeyChange +$.jStorage.listenKeyChange("testNum", (key, value) => { + console.assert(key.length > 0); + console.assert(value != null); +} ); + +$.jStorage.listenKeyChange("testNum", (key, value) => { + console.assert(key === "testNum"); + console.assert(value + 10 > 0); +} ); + +// Test stopListening +$.jStorage.stopListening("testNum"); +$.jStorage.stopListening("testNum", () => { console.assert(); } ); + +// Test subscribe +$.jStorage.subscribe("ESPN", (channel, value) => { + console.assert(channel !== "ABC"); + console.assert(value !== null); +} ); + +$.jStorage.subscribe("ESPN", (channel, value) => { + console.assert(channel === "ESPN"); + console.assert(value.getDate() > Date.now()); +} ); + +// Test publish +$.jStorage.publish("ESPN", { date: new Date(2013, 4, 26, 7), game: "Miami Heat" }); + +// Test reinit +$.jStorage.reInit(); \ No newline at end of file diff --git a/jstorage/jstorage.d.ts b/jstorage/jstorage.d.ts new file mode 100644 index 000000000..0acc93fbd --- /dev/null +++ b/jstorage/jstorage.d.ts @@ -0,0 +1,159 @@ +// Type definitions for jStorage 0.3.0 +// Project: http://www.jstorage.info/ +// Definitions by: Danil Flores +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module $.jStorage { + + class IStorageOptions { + TTL: number; + } + + interface IJStorage { + [key: string]: any; + } + + /** + * Sets a key's value. + * + * @param key Key to set. If this value is not set or not + * a string an exception is raised. + * @param value Value to set. This can be any value that is JSON + * compatible (Numbers, Strings, Objects etc.). + * @param [options] - possible options to use + * @param [options.TTL] - optional TTL value + * @return the used value + */ + function set (key: string, value: TValue, options?: IStorageOptions): TValue; + + /** + * Looks up a key in cache + * + * @param key - Key to look up. + * @param defaultIfNotFound - Default value to return, if key didn't exist. + * @return the key value, default value or null + */ + function get (key: string, defaultIfNotFound?: TValue): TValue; + + /** + * Deletes a key from cache. + * + * @param key - Key to delete. + * @return true if key existed or false if it didn't + */ + function deleteKey(key: string): boolean; + + /** + * Sets a TTL for a key, or remove it if ttl value is 0 or below + * + * @param key - key to set the TTL for + * @param ttl - TTL timeout in milliseconds + * @return true if key existed or false if it didn't + */ + function setTTL(key: string, ttl: number): boolean; + + /** + * Gets remaining TTL (in milliseconds) for a key or 0 when no TTL has been set + * + * @param key Key to check + * @return Remaining TTL in milliseconds + */ + function getTTL(key: string): number; + + /** + * Deletes everything in cache. + * + * @return Always true + */ + function flush(): boolean; + + /** + * Returns a read-only copy of _storage + * + * @return Read-only copy of _storage + */ + function storageObj(): IJStorage + + /** + * Returns an index of all used keys as an array + * ['key1', 'key2',..'keyN'] + * + * @return Used keys + */ + function index(): string[]; + + /** + * How much space in bytes does the storage take? + * + * @return Storage size in chars (not the same as in bytes, + * since some chars may take several bytes) + */ + function storageSize(): number; + + /** + * Which backend is currently in use? + * + * @return Backend name + */ + function currentBackend(): Storage; + + /** + * Test if storage is available + * + * @return True if storage can be used + */ + function storageAvailable(): boolean; + + /** + * Register change listeners + * + * @param key Key name + * @param callback Function to run when the key changes + */ + function listenKeyChange(key: string, callback: (key: string, value: any) => void ): void; + + /** + * Register change listeners + * + * @param key Key name + * @param callback Function to run when the key changes + */ + function listenKeyChange(key: string, callback: (key: string, value: TValue) => void ): void; + + /** + * Remove change listeners + * + * @param key Key name to unregister listeners against + * @param [callback] If set, unregister the callback, if not - unregister all + */ + function stopListening(key: string, callback?: Function): void; + + /** + * Subscribe to a Publish/Subscribe event stream + * + * @param channel Channel name + * @param callback Function to run when the something is published to the channel + */ + function subscribe(channel: string, callback: (channel: string, value: any) => void ): void; + + /** + * Subscribe to a Publish/Subscribe event stream + * + * @param channel Channel name + * @param callback Function to run when the something is published to the channel + */ + function subscribe(channel: string, callback: (channel: string, value: TValue) => void ): void; + + /** + * Publish data to an event stream + * + * @param channel Channel name + * @param payload Payload to deliver + */ + function publish(channel: string, payload: any): void; + + /** + * Reloads the data from browser storage + */ + function reInit(): void; +} \ No newline at end of file