Skip to content

giant-stone/iso3166

Repository files navigation

About

Generate ISO 3166-1, ISO 3166-3, ISO 4217 and ITU-T E.164 data in JSON, Go, TypeScript and Swift.

Go Go Report Card LICENSE GoDoc

Source code folder description

Folder Description
iso/ The definitions of the region entity and related structures.
iso/lang/ ISO 639-1
iso/region/ The helper functions of region.
cmd/iso/ The command-line tool is used for updating source data files and generating code.
gen/go/iso_data.go The output file generated by the Go code generator.
gen/json/iso_data.json The output file generated by the JSON code generator.
gen/ts/iso_data.ts The output file generated by the TypeScript code generator.
gen/swift/*.swift The output files generated by the Swift code generator.
test_data/ Sample data for testing.

NOTICE:iso/lang/ is based on emvi/iso-639-1

Update ISO data file

Build requirement: Go 1.20

(OPTIONAL) Fetch the source data file from the wikipedia website:

# OPTIONAL: set-up HTTP proxy
export HTTPS_PROXY=http://localhost:7890

go run cmd/iso/main.go -sync

Generate Go code from the source data file: go run cmd/iso/main.go -langs go:gen/go/

Generate JSON, Go, TypeScript and Swift codes from the source data file, and then merge it with the patch.json file:

go run cmd/iso/main.go -langs json:gen/json/,go:gen/go/,ts:gen/ts/,swift:gen/swift/ -patch patch.json -o

Use -compact if you want minified output instead of the default pretty formatting.

You could customize the patch JSON file to specify which fields you want to override, based on the field definitions in iso/entity.go under IEntity. The only required field is Alpha2Code.

Go Usage

Go code snippet:

package main

import (
	"encoding/json"
	"fmt"

	iso_data "github.com/giant-stone/iso3166/gen/go"
	"github.com/giant-stone/iso3166/iso"
	"github.com/giant-stone/iso3166/iso_helper/region"
)

func main() {

	for _, entity := range []iso.IEntity{
		region.NewFromCode("HK"),
		region.NewFromRegionInCN("香港"),
		region.NewFromCommonNameInAlphaNumeric("HongKong"),
		iso_data.HongKong,
	} {
		dat, _ := json.MarshalIndent(entity, "", "  ")
		fmt.Println(string(dat))
		fmt.Println(entity.GetAlpha2Code() == iso_data.HongKong.GetAlpha2Code())
	}
}

Output:

{
  "Alpha2Code": "HK",
  "ShortName": "Hong Kong",
  "ShortNameLowerCase": "",
  "FullName": "",
  "Alpha3Code": "HKG",
  "NumericCode": "344",
  "Remarks": null,
  "Independent": false,
  "Status": "",
  "Alpha4Code": "",
  "PeriodOfValidity": "",
  "Alias": [],
  "CommonName": "Hong Kong",
  "CommonNameInAlphaNumeric": "HongKong",
  "CallingCode": "852",
  "Capital": "",
  "CapitalInNative": "",
  "Languages": ["English", "Cantonese"],
  "RegionInCN": "中国香港",
  "RegionInNative": "香港",
  "AlphabeticCode": "HKD",
  "NumericCode4217": "344",
  "MinorUnit": 2,
  "Currency": "Hong Kong dollar",
  "Entities": null,
  "CurrencyInCN": "港元",
  "CurrencyInNative": "港幣"
}

See also iso_helper/region/region_test.go

TypeScript Usage

TypeScript code snippet:

import * as isoData from "./gen/ts/iso_data";

[isoData.RegionsFromCommonNameInAlphaNumeric["HongKong"], isoData.RegionsByCode["HK"], isoData.HongKong].forEach((entity) => {
  console.log(JSON.stringify(entity, undefined, "  "));
  console.log(entity.alpha2Code == isoData.HongKong.alpha2Code);
});

Output:

{
  "alpha2Code": "HK",
  "alpha3code": "HKG",
  "alpha4code": "",
  "independent": false,
  "numeric_code": "344",
  "short_name": "Hong Kong",
  "period_of_validity": "",
  "alias": "[]",
  "common_name": "Hong Kong",
  "calling_code": "852",
  "languages": ["English", "Cantonese"],
  "region_in_cn": "中国香港",
  "region_in_native": "香港",
  "alphabetic_code": "HKD",
  "numeric_code_4217": "344",
  "minor_unit": 2,
  "currency": "Hong Kong dollar",
  "currency_in_cn": "港元",
  "currency_in_native": "港幣"
}

About

ISO 3166 part 1 and part 3 data for Go, TypeScript and JSON

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors