Skip to content

Commit

Permalink
Fixed typeScript definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
ellenaua committed May 17, 2020
1 parent da94afe commit c3e6e4f
Show file tree
Hide file tree
Showing 10 changed files with 240 additions and 395 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### `0.5.30` _2020-05-16_
* Updated data to IANA TZDB `2020a`
* Fixed typescript definitions

### `0.5.29` _2020-05-16_
* Merged fix of es6 module loading issue https://github.com/moment/moment-timezone/commit/1fd42349189b24e15c60f162dc8c40b42db79dfe
Expand Down
2 changes: 1 addition & 1 deletion data/packed/latest.json
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@
"GG|Europe/London Europe/Guernsey",
"GH|Africa/Accra",
"GI|Europe/Gibraltar",
"GL|America/Godthab America/Danmarkshavn America/Scoresbysund America/Thule",
"GL|America/Nuuk America/Danmarkshavn America/Scoresbysund America/Thule",
"GM|Africa/Abidjan Africa/Banjul",
"GN|Africa/Abidjan Africa/Conakry",
"GP|America/Port_of_Spain America/Guadeloupe",
Expand Down
10 changes: 5 additions & 5 deletions data/unpacked/latest.json
Original file line number Diff line number Diff line change
Expand Up @@ -16317,9 +16317,7 @@
180
],
"population": 16583,
"countries": [
"GL"
]
"countries": []
},
{
"name": "America/Goose_Bay",
Expand Down Expand Up @@ -34416,7 +34414,9 @@
180
],
"population": 0,
"countries": []
"countries": [
"GL"
]
},
{
"name": "America/Ojinaga",
Expand Down Expand Up @@ -127758,7 +127758,7 @@
{
"name": "GL",
"zones": [
"America/Godthab",
"America/Nuuk",
"America/Danmarkshavn",
"America/Scoresbysund",
"America/Thule"
Expand Down
170 changes: 55 additions & 115 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,127 +1,67 @@
import * as moment from 'moment';

// require('moment-timezone') === require('moment')
export = moment;
import moment = require('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;
interface MomentZone {
name: string;
abbrs: string[];
untils: number[];
offsets: number[];
population: number;

abbr(timestamp: number): string;
offset(timestamp: number): number;
utcOffset(timestamp: number): number;
parse(timestamp: number): number;
}

type Zone = tz.Zone;

/** Parsed / unpacked zone data. */
interface UnpackedZone {
/** The uniquely identifying name of the time zone. */
interface MomentZoneOffset {
name: string;

/** zone abbreviations */
abbrs: Array<string>;

/** (measured in milliseconds) */
untils: Array<number | null>;

/** (measured in minutes) */
offsets: Array<number>;
offset: number;
}

/** Bundle of zone data and links for multiple timezones */
interface PackedZoneBundle {
version: string;
zones: Array<string>;
links: Array<string>;
interface MomentTimezone {
(): moment.Moment;
(timezone: string): moment.Moment;
(date: number, timezone: string): moment.Moment;
(date: number[], timezone: string): moment.Moment;
(date: string, timezone: string): moment.Moment;
(date: string, format: moment.MomentFormatSpecification, timezone: string): moment.Moment;
(date: string, format: moment.MomentFormatSpecification, strict: boolean, timezone: string): moment.Moment;
(date: string, format: moment.MomentFormatSpecification, language: string, timezone: string): moment.Moment;
(date: string, format: moment.MomentFormatSpecification, language: string, strict: boolean, timezone: string): moment.Moment;
(date: Date, timezone: string): moment.Moment;
(date: moment.Moment, timezone: string): moment.Moment;
(date: any, timezone: string): moment.Moment;

zone(timezone: string): MomentZone | null;

add(packedZoneString: string): void;
add(packedZoneString: string[]): void;

link(packedLinkString: string): void;
link(packedLinkString: string[]): void;

load(data: { version: string; links: string[]; zones: string[] }): void;

names(): string[];
zonesForCountry<T extends true>(country: string, with_offset: T): T extends true ? MomentZoneOffset[] : never;
zonesForCountry<T extends false>(country: string, with_offset?: T): T extends false ? string[] : never;
zonesForCountry(country: string, with_offset?: boolean): MomentZoneOffset[] | string[];
countries(): string[];
guess(ignoreCache?: boolean): string;

setDefault(timezone?: string): Moment;
}

/** Bundle of zone data and links for multiple timezones */
interface UnpackedZoneBundle {
version: string;
zones: Array<UnpackedZone>;
links: Array<string>;
interface Moment {
tz(): string | undefined;
tz(timezone: string, keepLocalTime?: boolean): moment.Moment;
zoneAbbr(): string;
zoneName(): string;
}

const tz: MomentTimezone;
}

// require("moment-timezone") === require("moment")
export = moment;
55 changes: 42 additions & 13 deletions moment-timezone-utils.d.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,70 @@
import * as moment from 'moment';

// require('moment-timezone') === require('moment')
export = moment;
import moment = require('moment');
import { MomentTimezone } from "./index";

declare module 'moment' {
namespace tz {

/** 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>;
}

/** extends MomentTimezone declared in index */
interface MomentTimezone {
/** Converts zone data in the unpacked format to the packed format. */
function pack(unpackedObject: UnpackedZone): string;
pack(unpackedObject: UnpackedZone): string;

/** Convert a base 10 number to a base 60 string. */
function packBase60(input: number, precision?: number): string;
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;
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;
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;
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;
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;
filterLinkPack(unpackedBundle: UnpackedZoneBundle, startAndEndYear: number): PackedZoneBundle;
}
}
}

// require("moment-timezone") === require("moment")
export = moment;
2 changes: 1 addition & 1 deletion tests/countries/countries.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ exports.countries = {
test.deepEqual(tz.zone('America/Fort_Wayne').countries(), []);
test.deepEqual(tz.zone('America/Fortaleza').countries(), ["BR"]);
test.deepEqual(tz.zone('America/Glace_Bay').countries(), ["CA"]);
test.deepEqual(tz.zone('America/Godthab').countries(), ["GL"]);
test.deepEqual(tz.zone('America/Godthab').countries(), []);
test.deepEqual(tz.zone('America/Goose_Bay').countries(), ["CA"]);
test.deepEqual(tz.zone('America/Grand_Turk').countries(), ["TC"]);
test.deepEqual(tz.zone('America/Grenada').countries(), ["GD"]);
Expand Down

0 comments on commit c3e6e4f

Please sign in to comment.