mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-08-11 20:40:20 +00:00
add iso-3166-2 typings (#18525)
* iso-3166-2 typings * requested changes for iso-3166-2
This commit is contained in:
committed by
Sheetal Nandi
parent
02ff752907
commit
82a8308296
Vendored
+52
@@ -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 <https://github.com/sicilica>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
|
||||
export type InfoOrEmptyRecord<T> = 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<SubdivisionInfo>;
|
||||
|
||||
export function country(countryCodeOrName: string): InfoOrEmptyRecord<CountryInfo>;
|
||||
|
||||
export const data: CountryInfo.Map;
|
||||
|
||||
// map of alpha 3 codes to alpha 3 codes
|
||||
export const codes: {
|
||||
[alpha3: string]: string
|
||||
};
|
||||
@@ -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<T>(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]);
|
||||
@@ -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"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"extends": "dtslint/dt.json"
|
||||
}
|
||||
Reference in New Issue
Block a user