diff --git a/types/iso-3166-2/index.d.ts b/types/iso-3166-2/index.d.ts new file mode 100644 index 0000000000..42129fb444 --- /dev/null +++ b/types/iso-3166-2/index.d.ts @@ -0,0 +1,52 @@ +// Type definitions for iso-3166-2 0.6 +// Project: https://github.com/olahol/iso-3166-2.js +// Definitions by: Matt Rollins +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +export type InfoOrEmptyRecord = T | {}; + +export namespace CountryInfo { + interface Partial { + name: string; + sub: SubdivisionInfo.Map; + } + interface Full extends Partial { + code: string; + } + + interface Map { + // full data if this country has been retrieved with country() at least once + [code: string]: Full | Partial; + } +} +export type CountryInfo = CountryInfo.Full; + +export namespace SubdivisionInfo { + interface Partial { + type: string; + name: string; + } + interface Full extends Partial { + countryName: string; + countryCode: string; + code: string; + regionCode: string; + } + + interface Map { + // full data if this subdivision has been retrieved with subdivision() at least once + [code: string]: Full | Partial; + } +} +export type SubdivisionInfo = SubdivisionInfo.Full; + +export function subdivision(countryCodeOrFullSubdivisionCode: string, subdivisionCodeOrName?: string): InfoOrEmptyRecord; + +export function country(countryCodeOrName: string): InfoOrEmptyRecord; + +export const data: CountryInfo.Map; + +// map of alpha 3 codes to alpha 3 codes +export const codes: { + [alpha3: string]: string +}; diff --git a/types/iso-3166-2/iso-3166-2-tests.ts b/types/iso-3166-2/iso-3166-2-tests.ts new file mode 100644 index 0000000000..0a0bca345a --- /dev/null +++ b/types/iso-3166-2/iso-3166-2-tests.ts @@ -0,0 +1,50 @@ +import * as iso3166 from "iso-3166-2"; + +// subdivision() and country() return {} on failure, which is nearly impossible to separate from a valid response in TypeScript; +// until a PR can be accepted on the original repo, this workaround makes accessing the original type possible. +function wrap(rawResult: T|{}): T|null { + if (Object.keys(rawResult).length) + return rawResult as T; + else + return null; +} + +iso3166.subdivision("SE-O"); +iso3166.subdivision("US-IN"); + +iso3166.subdivision("SWE", "O"); +iso3166.subdivision("US", "Indiana"); + +const indiana = wrap(iso3166.subdivision("US-IN")); +if (indiana) { + const name: string = indiana.name; + const type: string = indiana.type; + const code: string = indiana.code; + const regionCode: string = indiana.regionCode; + const countryCode: string = indiana.countryCode; + const countryName: string = indiana.countryName; +} + +iso3166.country("US"); +iso3166.country("SE"); +iso3166.country("SWE"); + +iso3166.country("United States"); +iso3166.country("Sweden"); + +const sweden = wrap(iso3166.country("SE")); +if (sweden) { + const name: string = sweden.name; + const subNameO: string = sweden.sub["SE-O"].name; + const code: string = sweden.code; +} + +const unitedStates = iso3166.data["US"]; +{ + const name: string = unitedStates.name; + const subNameIN: string = unitedStates.sub["US-IN"].name; +} + +const alpha3 = "SWE"; +const alpha2 = "SE"; +const codesWork = (alpha2 === iso3166.codes[alpha3]); diff --git a/types/iso-3166-2/tsconfig.json b/types/iso-3166-2/tsconfig.json new file mode 100644 index 0000000000..7ed345dd25 --- /dev/null +++ b/types/iso-3166-2/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", + "iso-3166-2-tests.ts" + ] +} diff --git a/types/iso-3166-2/tslint.json b/types/iso-3166-2/tslint.json new file mode 100644 index 0000000000..b4b47a0378 --- /dev/null +++ b/types/iso-3166-2/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +}