Merge pull request #677 from dflor003/master

Add typings for jQuery jStorage plugin
This commit is contained in:
Diullei Gomes
2013-06-21 20:42:23 -07:00
5 changed files with 291 additions and 1 deletions
+3 -1
View File
@@ -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))
@@ -125,8 +126,9 @@ List of Definitions
* [KoLite](https://github.com/CodeSeven/kolite) (by [Boris Yankov](https://github.com/borisyankov))
* [Leaflet](https://github.com/Leaflet/Leaflet) (by [Vladimir](https://github.com/rgripper))
* [Libxmljs](https://github.com/polotek/libxmljs) (by [François de Campredon](https://github.com/fdecampredon))
* [ladda](https://github.com/hakimel/Ladda) (by [Danil Flores](https://github.com/dflor003))
* [linq.js](http://linqjs.codeplex.com/) (by [Marcin Najder](https://github.com/marcinnajder))
* [Livestamp.js](https://github.com/mattbradley/livestampjs) (by [Vincent Bortone] (https://github.com/vbortone))
* [Livestamp.js](https://github.com/mattbradley/livestampjs) (by [Vincent Bortone](https://github.com/vbortone))
* [Marked](https://github.com/chjj/marked) (by [William Orr](https://github.com/worr))
* [Modernizr](http://modernizr.com/) (by [Boris Yankov](https://github.com/borisyankov) and [Theodore Brown](https://github.com/theodorejb/))
* [Moment.js](https://github.com/timrwood/moment) (by [Michael Lakerveld](https://github.com/Lakerfield))
+74
View File
@@ -0,0 +1,74 @@
/// <reference path="jstorage.d.ts" />
// 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<number>("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<number>("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<Date>("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();
+159
View File
@@ -0,0 +1,159 @@
// Type definitions for jStorage 0.3.0
// Project: http://www.jstorage.info/
// Definitions by: Danil Flores <https://github.com/dflor003/>
// 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 <TValue>(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 <TValue>(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<TValue>(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<TValue>(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;
}
+20
View File
@@ -0,0 +1,20 @@
/// <reference path="ladda.d.ts" />
// Test bind
Ladda.bind('button.ladda-button', { timeout: 42, callback: btn => alert('Clicked!!!') });
Ladda.bind('button.ladda-button');
Ladda.bind(document.createElement('button'), {});
Ladda.bind(document.createElement('button'));
// Test stop all
Ladda.stopAll();
// Test create
var btnElement = document.createElement('button');
var laddaBtn = Ladda.create(btnElement);
// Test operations via chaining
laddaBtn.start().stop().toggle().setProgress(42).enable().disable().start();
// Test isLoading
console.assert(laddaBtn.isLoading() === true);
+35
View File
@@ -0,0 +1,35 @@
// Type definitions for jStorage 0.4.0
// Project: https://github.com/hakimel/Ladda
// Definitions by: Danil Flores <https://github.com/dflor003/>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
declare module Ladda {
interface ILaddaButton {
start(): ILaddaButton;
stop(): ILaddaButton;
toggle(): ILaddaButton;
setProgress(progress: number): ILaddaButton;
enable(): ILaddaButton;
disable(): ILaddaButton;
isLoading(): boolean;
}
interface ILaddaOptions {
timeout?: number;
callback?: (instance: ILaddaButton) => void;
}
function bind(target: HTMLElement, options?: ILaddaOptions): void;
function bind(cssSelector: string, options?: ILaddaOptions): void;
function create(button: HTMLElement): ILaddaButton;
function stopAll(): void;
}