Add "tocktimer" typings (#20017)

* add types for tocktimer

* use correct formatting

* add linting and update structure

* update for build issues
This commit is contained in:
Evan Shortiss
2017-09-26 01:07:01 -04:00
committed by Masahiro Wakame
parent 810a07b93e
commit dbb5456b01
4 changed files with 140 additions and 0 deletions

89
types/tocktimer/index.d.ts vendored Normal file
View File

@@ -0,0 +1,89 @@
// Type definitions for tocktimer 1.0
// Project: https://github.com/mrchimp/tock
// Definitions by: Evan Shortiss <https://github.com/evanshortiss>
// 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;

View File

@@ -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');

View File

@@ -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"
]
}

View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }