-
Notifications
You must be signed in to change notification settings - Fork 838
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Yet another PR adding TypeScript declarations (#530)
* Adds TypeScript declarations for moment-timezone * Remove unnecessary `moment.` qualification in typings * Move exported types to top-level so that they can all be imported as named imports - Zone class must exist at `tz.Zone` because that's where it's exposed at runtime, but the `Zone` interface is still re-exported as a named export * tsc invocation always uses locally installed tsc Co-authored-by: Elena Sharovar <[email protected]>
- Loading branch information
Showing
7 changed files
with
461 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import * as moment from 'moment'; | ||
|
||
// require('moment-timezone') === require('moment') | ||
export = moment; | ||
|
||
declare module 'moment' { | ||
interface Moment { | ||
/** Set the timezone and update the offset */ | ||
tz(zone: string): Moment; | ||
/** Return the timezone name or undefined if a zone has not yet been set */ | ||
tz(): string | undefined; | ||
|
||
/** Get the zone abbreviation. This is what moment.js uses when formatting the z token. */ | ||
zoneAbbr(): string; | ||
|
||
/** Get the zone long form name. This is what moment.js uses when formatting the zz token. */ | ||
zoneName(): string; | ||
} | ||
|
||
// Match normal moment constructor but with an extra timezone argument | ||
// Here's a copy-paste of the normal moment constructor's signature, from https://github.com/moment/moment/blob/develop/moment.d.ts#L1-L2 | ||
// declare function moment(inp?: moment.MomentInput, format?: moment.MomentFormatSpecification, strict?: boolean): moment.Moment; | ||
// declare function moment(inp?: moment.MomentInput, format?: moment.MomentFormatSpecification, language?: string, strict?: boolean): moment.Moment; | ||
|
||
// Should be sorted from tightest to loosest. TypeScript picks the first signature that matches, going top to bottom. | ||
|
||
/** create a moment with a time zone */ | ||
function tz(inp: MomentInput, format: MomentFormatSpecification, language: string, strict: boolean, zone: string): Moment; | ||
/** create a moment with a time zone */ | ||
function tz(inp: MomentInput, format: MomentFormatSpecification, language: string, zone: string): Moment; | ||
/** create a moment with a time zone */ | ||
function tz(inp: MomentInput, format: MomentFormatSpecification, strict: boolean, zone: string): Moment; | ||
/** create a moment with a time zone */ | ||
function tz(inp: MomentInput, format: MomentFormatSpecification, zone: string): Moment; | ||
/** create a moment with a time zone */ | ||
function tz(inp: MomentInput, zone: string): Moment; | ||
/** create a moment with a time zone */ | ||
function tz(zone?: string): Moment; | ||
|
||
namespace tz { | ||
/** Version of moment-timezone */ | ||
const version: string; | ||
|
||
/** | ||
* Change the default timezone of newly created Moment instances. | ||
* By default new instances are created in the local timezone. | ||
*/ | ||
function setDefault(zone: string): typeof moment; | ||
|
||
/** Reset the default timezone to local. */ | ||
function setDefault(): typeof moment; | ||
|
||
/** | ||
* Retrieve or guess the user's timezone. Uses the browser's Internationalization API if available. | ||
* Otherwise, guesses by sampling offsets from different points in time and comparing them to available zone data. | ||
*/ | ||
function guess(): string; | ||
|
||
interface Zone extends UnpackedZone {} | ||
class Zone { | ||
/** Get the abbreviation for a given timestamp from a Zone. */ | ||
abbr(timestamp: number): string; | ||
|
||
/** Get the offset for a given timestamp from a Zone. */ | ||
offset(timestamp: number): number; | ||
|
||
/** Parse an offset for a timestamp constructed from Date.UTC in that zone. */ | ||
parse(timestamp: number): number; | ||
} | ||
|
||
/** Return a timezone by name or null if timezone by that name is not loaded. */ | ||
function zone(name: string): Zone | null; | ||
|
||
/** Add zone data for a timezone. */ | ||
function add(packedZone: string): void; | ||
/** Add zone data for multiple timezones. */ | ||
function add(packedZones: Array<string>): void; | ||
|
||
/** Link two zone names to the same data */ | ||
function link(packedLink: string): void; | ||
/** Add multiple links at once */ | ||
function link(packedLinks: Array<string>): void; | ||
|
||
/** load a bundle of zone data and links */ | ||
function load(bundle: PackedZoneBundle): void; | ||
|
||
/** get a list of all available time zone names */ | ||
function names(): Array<string>; | ||
|
||
/** Convert a packed string to an unpacked zone data object */ | ||
function unpack(packedZone: string): UnpackedZone; | ||
/** Convert a base 60 string to a base 10 number. */ | ||
function unpackBase60(base60String: string): number; | ||
} | ||
|
||
type Zone = tz.Zone; | ||
|
||
/** Parsed / unpacked zone data. */ | ||
interface UnpackedZone { | ||
/** The uniquely identifying name of the time zone. */ | ||
name: string; | ||
|
||
/** zone abbreviations */ | ||
abbrs: Array<string>; | ||
|
||
/** (measured in milliseconds) */ | ||
untils: Array<number | null>; | ||
|
||
/** (measured in minutes) */ | ||
offsets: Array<number>; | ||
} | ||
|
||
/** Bundle of zone data and links for multiple timezones */ | ||
interface PackedZoneBundle { | ||
version: string; | ||
zones: Array<string>; | ||
links: Array<string>; | ||
} | ||
|
||
/** Bundle of zone data and links for multiple timezones */ | ||
interface UnpackedZoneBundle { | ||
version: string; | ||
zones: Array<UnpackedZone>; | ||
links: Array<string>; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import * as moment from 'moment'; | ||
|
||
// require('moment-timezone') === require('moment') | ||
export = moment; | ||
|
||
declare module 'moment' { | ||
namespace tz { | ||
/** Converts zone data in the unpacked format to the packed format. */ | ||
function pack(unpackedObject: UnpackedZone): string; | ||
|
||
/** Convert a base 10 number to a base 60 string. */ | ||
function packBase60(input: number, precision?: number): string; | ||
|
||
/** Create links out of two zones that share data. | ||
* @returns A new ZoneBundle with duplicate zone data replaced by links | ||
*/ | ||
function createLinks(unlinked: UnpackedZoneBundle): PackedZoneBundle; | ||
|
||
/** | ||
* Filter out data for years outside a certain range. | ||
* @return a new, filtered UnPackedZone object | ||
*/ | ||
function filterYears(unpackedZone: UnpackedZone, startYear: number, endYear: number): UnpackedZone; | ||
/** | ||
* Filter out data for years outside a certain range. | ||
* @return a new, filtered UnPackedZone object | ||
*/ | ||
function filterYears(unpackedZone: UnpackedZone, startAndEndYear: number): UnpackedZone; | ||
|
||
/** | ||
* Combines packing, link creation, and subsetting of years into one simple interface. | ||
* Pass in an unpacked bundle, start year, and end year and get a filtered, linked, packed bundle back. | ||
*/ | ||
function filterLinkPack(unpackedBundle: UnpackedZoneBundle, startYear: number, endYear: number): PackedZoneBundle; | ||
/** | ||
* Combines packing, link creation, and subsetting of years into one simple interface. | ||
* Pass in an unpacked bundle, start year, and end year and get a filtered, linked, packed bundle back. | ||
*/ | ||
function filterLinkPack(unpackedBundle: UnpackedZoneBundle, startAndEndYear: number): PackedZoneBundle; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.