Added declaration of tweetdeck/valiant (#27385)

* Added declaration of tweetdeck/valiant

* fixed version notation and miss typo.
This commit is contained in:
whatasoda
2018-07-28 09:22:03 +09:00
committed by Andy
parent 1a1c9299a6
commit 2eda9e3a6f
4 changed files with 128 additions and 0 deletions

51
types/valiant/index.d.ts vendored Normal file
View File

@@ -0,0 +1,51 @@
// Type definitions for valiant 2.0
// Project: https://github.com/tweetdeck/valiant#readme
// Definitions by: whatasoda <https://github.com/whatasoda>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
export as namespace Valiant;
export function createInterval<T = number>(compareValues?: IntervalConstructor<T>['compareValues']): IntervalConstructor<T>;
export interface IntervalConstructor<T = number> {
new (from: Endpoint<T>, to: Endpoint<T>): Interval<T>;
prototype: Interval<T>;
empty: Interval<T>;
whole: Interval<T>;
negInf: Endpoint<T>;
posInf: Endpoint<T>;
negativeInfinity: Endpoint<T>;
positiveInfinity: Endpoint<T>;
adjacentEndpoint(endpoint: Endpoint<T>): Endpoint<T>;
compareEndpoints(a: Endpoint<T>, b: Endpoint<T>): number;
compareValues(a: T, b: T): number;
excEnd(value: T): Endpoint<T>;
exclusiveEndpoint(value: T): Endpoint<T>;
incEnd(value: T): Endpoint<T>;
inclusiveEndpoint(value: T): Endpoint<T>;
singleton(value: T): Interval<T>;
}
export interface Interval<T = number> {
constructor: IntervalConstructor;
empty: boolean;
from: Endpoint<T>;
to: Endpoint<T>;
hull(interval: Interval<T>): Interval<T>;
unify(interval: Interval<T>): Interval<T>;
isEmpty(): boolean;
equalTo(interval: Interval<T>): boolean;
contains(value: T): boolean;
isSubsetOf(interval: Interval<T>): boolean;
intersection(interval: Interval<T>): boolean;
contiguousWith(interval: Interval<T>): boolean;
fromComparator(a: Endpoint<T>, b: Endpoint<T>): number;
toComparator(a: Endpoint<T>, b: Endpoint<T>): number;
}
export interface Endpoint<T = number> {
finite: boolean;
inclusive: boolean;
value: T;
}

View File

@@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"valiant-tests.ts"
]
}

View File

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

View File

@@ -0,0 +1,53 @@
import { createInterval } from "valiant";
// To make an Interval, you need to create a constructor:
const Interval = createInterval();
// By default it will manage integers:
// (0,100] — the numbers 0 to 100, excluding 0
new Interval(
Interval.exclusiveEndpoint(0),
Interval.inclusiveEndpoint(100)
);
// You can provide a custom sort function to support a different data type:
const DateInterval = createInterval<Date>(function sortDates(a, b) {
return a.getTime() - b.getTime();
});
// [12 hours ago,now] — 12 hours ago until now
new DateInterval(
DateInterval.incEnd(
new Date(Date.now() - (1000 * 60 * 60 * 12))
),
DateInterval.incEnd(
new Date(Date.now())
)
);
// You can do calculations with two intervals:
const i = new Interval(Interval.incEnd(1), Interval.incEnd(3));
const j = new Interval(Interval.incEnd(2), Interval.incEnd(4));
const k = new Interval(Interval.incEnd(5), Interval.incEnd(6));
i.intersection(j); // 2, 3
i.hull(j); // 1, 4
i.contiguousWith(j); // true
i.unify(j); // 1, 4
i.intersection(k); // Interval.empty
i.hull(k); // 1, 6
i.contiguousWith(k); // false
i.unify(k); // Interval.empty
i.equalTo(j); // false
i.contains(2); // true
i.isSubsetOf(j); // false
// If there is no possible unification, the empty set (Interval.empty) results.
// There are two special intervals:
Interval.empty; // {}
Interval.whole; // -Infinity, Infinity
// You can also create an interval of value:
Interval.singleton(5); // [5,5]