Skip to content

ISO 3166 part 1 and part 3 data for Go, TypeScript and JSON 生成 ISO 3166 第一部分和第三部分数据文件,可用于 Go 、TypeScript 等语言项目,同时生成 JSON 数据文件,理论上可兼容任意语言和框架。

License

Notifications You must be signed in to change notification settings

giant-stone/iso3166

Repository files navigation

About

Generate ISO 3166-1, ISO 3166-3 and ISO 4217 data in JSON, Go and TypeScript.

Go

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.
test_data/ Sample data for testing.

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

Update ISO data file

Build dependencies: Go 1.22.x+

(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 main.go -gen go:go/go/iso.go

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

go run main.go -gen json:gen/json/iso_data.json,go:gen/go/iso_data.go,ts:gen/ts/iso_data.ts -patch patch.json

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",
  "CurrencyInCN": ""
}
true

...

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": "",
  "common_name": "Hong Kong",
  "calling_code": "852",
  "region_in_cn": "中国香港",
  "region_in_native": "香港",
  "alphabetic_code": "HKD",
  "numeric_code_4217": "344",
  "minor_unit": 2,
  "currency": "Hong Kong dollar",
  "currency_in_cn": ""
}
true

...

About

ISO 3166 part 1 and part 3 data for Go, TypeScript and JSON 生成 ISO 3166 第一部分和第三部分数据文件,可用于 Go 、TypeScript 等语言项目,同时生成 JSON 数据文件,理论上可兼容任意语言和框架。

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •