diff --git a/types/tocktimer/index.d.ts b/types/tocktimer/index.d.ts new file mode 100644 index 0000000000..20bd344c83 --- /dev/null +++ b/types/tocktimer/index.d.ts @@ -0,0 +1,89 @@ +// Type definitions for tocktimer 1.0 +// Project: https://github.com/mrchimp/tock +// Definitions by: Evan Shortiss +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare function t(opts?: t.TockOptions): t.Tock; + +declare namespace t { + interface TockOptions { + /** + * Defaults to 10 milliseconds. How often, in milliseconds, that the clock will tick. + */ + interval?: number; + + /** + * Defaults to false. If true, the clock will count down from a given time, otherwise it will count up from 0:00. + */ + countdown?: boolean; + + /** + * Callback function executed on each tick + */ + callback(): void; + + /** + * Callback function executed when the timer is complete + */ + complete(): void; + } + + class Tock { + /** + * Create a Tock instance + * @param opts + */ + constructor(opts?: TockOptions) + + /** + * Start the timer + * @param [time] {Number} Optional. Can be either a countdown value or a starting value. + * If a countdown timer then set time to count down from. + * If a starting value then set time to the desired start time to count up from. + */ + start: (time?: number) => void; + + /** + * Stop the clock and clear the timeout + */ + stop: () => void; + + /** + * Stop the clock if it's running, continue clock if paused + */ + pause: () => void; + + /** + * Reset times to zero. Countdown clocks need a duration to be passed to start() after reset() is called. + */ + reset: () => void; + + /** + * Returns the elapsed time in milliseconds + */ + lap: () => number; + + /** + * Convert number of milliseconds to a MM:SS time string. Won't handle times greater than 1 hour + * @param ms + */ + msToTime: (ms: number) => string; + + /** + * Convert number of milliseconds to timecode string + * @param ms + * @param showMs + */ + msToTimecode: (ms: number, showMs?: boolean) => string; + + /** + * Convert a time string to a number of milliseconds. Should be a duration as a string of form MM:SS, MM:SS:ms, MM:SS.ms, HH:MM:SS + * Alternatively a time in the future can be provided using the form yyyy-mm-dd HH:MM:SS.ms. The difference between this time and present will be returned. + * If the input cannot be recognized as one of the above then 0 is returned + * @param ms + */ + timeToMS: (time: string) => number; + } +} + +export = t; diff --git a/types/tocktimer/tocktimer-tests.ts b/types/tocktimer/tocktimer-tests.ts new file mode 100644 index 0000000000..85f911b8a9 --- /dev/null +++ b/types/tocktimer/tocktimer-tests.ts @@ -0,0 +1,28 @@ +import * as tock from 'tocktimer'; + +let timer: tock.Tock; + +let opts: tock.TockOptions; + +// Opts that conform to spec +opts = { + interval: 100, + countdown: true, + callback: () => { + // Tick tock...clock is ticking + }, + complete: () => { + // Ding ding...time's up + } +}; + +// Create a tock instance +timer = tock(); + +timer.lap(); +timer.msToTime(Date.now()); +timer.msToTimecode(Date.now()); +timer.pause(); +timer.reset(); +timer.stop(); +timer.timeToMS('12:51'); diff --git a/types/tocktimer/tsconfig.json b/types/tocktimer/tsconfig.json new file mode 100644 index 0000000000..131bc07cde --- /dev/null +++ b/types/tocktimer/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "tocktimer-tests.ts" + ] +} diff --git a/types/tocktimer/tslint.json b/types/tocktimer/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/tocktimer/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }