@twin.org/standards-unece 0.0.3-next.43 → 0.0.3-next.45

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,6 @@
1
1
  // Copyright 2026 IOTA Stiftung.
2
2
  // SPDX-License-Identifier: Apache-2.0.
3
3
  import fs from "node:fs/promises";
4
- import path from "node:path";
5
- import { fileURLToPath } from "node:url";
6
4
  import { Compression, CompressionType, Guards, Is, ObjectHelper } from "@twin.org/core";
7
5
  import { UN_LOCODE_COUNTRIES } from "../models/locode/unLocodeCountries.js";
8
6
  import { UN_LOCODE_FUNCTIONS } from "../models/locode/unLocodeFunctions.js";
@@ -260,10 +258,8 @@ export class UnLocodes {
260
258
  return cached;
261
259
  }
262
260
  try {
263
- const thisFilename = fileURLToPath(import.meta.url);
264
- const thisDirname = path.dirname(thisFilename);
265
- const filePath = path.join(thisDirname, `../data/${folder}/`, `${normalized}.json.gz`);
266
- const compressed = await fs.readFile(filePath);
261
+ const fileUrl = new URL(`../data/${folder}/${normalized}.json.gz`, import.meta.url);
262
+ const compressed = await fs.readFile(fileUrl);
267
263
  const jsonBuffer = await Compression.decompress(compressed, CompressionType.Gzip);
268
264
  const records = ObjectHelper.fromBytes(jsonBuffer);
269
265
  cache[normalized] = records.map(record => mapMethod(normalized, record));
@@ -1 +1 @@
1
- {"version":3,"file":"unLocodes.js","sourceRoot":"","sources":["../../../src/helpers/unLocodes.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAClC,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,EAAE,EAAE,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAQxF,OAAO,EAAE,mBAAmB,EAAE,MAAM,uCAAuC,CAAC;AAE5E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U,EAAE,eAAe,CAAC,IAAI,CAAC,CAAC;YAClF,MAAM,OAAO,GAAG,YAAY,CAAC,SAAS,CAAM,UAAU,CAAC,CAAC;YACxD,KAAK,CAAC,UAAU,CAAC,GAAG,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,SAAS,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC,CAAC;QAC1E,CAAC;QAAC,MAAM,CAAC,CAAA,CAAC;QAEV,OAAO,KAAK,CAAC,UAAU,CAAC,IAAI,EAAE,CAAC;IAChC,CAAC","sourcesContent":["// Copyright 2026 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport fs from \"node:fs/promises\";\nimport path from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\nimport { Compression, CompressionType, Guards, Is, ObjectHelper } from \"@twin.org/core\";\nimport { nameof } from \"@twin.org/nameof\";\nimport type { IUnLocodeCountry } from \"../models/IUnLocodeCountry.js\";\nimport type { IUnLocodeCountrySubdivisionRecord } from \"../models/IUnLocodeCountrySubdivisionRecord.js\";\nimport type { IUnLocodeFunction } from \"../models/IUnLocodeFunction.js\";\nimport type { IUnLocodeLocation } from \"../models/IUnLocodeLocation.js\";\nimport type { IUnLocodeLocationRecord } from \"../models/IUnLocodeLocationRecord.js\";\nimport type { IUnLocodeSubdivision } from \"../models/IUnLocodeSubdivision.js\";\nimport { UN_LOCODE_COUNTRIES } from \"../models/locode/unLocodeCountries.js\";\nimport type { UnLocodeCountriesList } from \"../models/locode/unLocodeCountriesList.js\";\nimport { UN_LOCODE_FUNCTIONS } from \"../models/locode/unLocodeFunctions.js\";\nimport type { UnLocodeFunctionsList } from \"../models/locode/unLocodeFunctionsList.js\";\n\n/**\n * A class handling UN/LOCODEs.\n * @see https://vocabulary.uncefact.org/unlocode-about\n */\nexport class UnLocodes {\n\t/**\n\t * The class name.\n\t * @internal\n\t */\n\tpublic static readonly CLASS_NAME = nameof<UnLocodes>();\n\n\t/**\n\t * The URI prefix for UN/LOCODE locations.\n\t * @internal\n\t */\n\tpublic static readonly LOCODE_COUNTRY_URI_PREFIX = \"unlcdc:\";\n\n\t/**\n\t * The URI prefix for UN/LOCODE locations.\n\t * @internal\n\t */\n\tpublic static readonly LOCODE_LOCATION_URI_PREFIX = \"unlcd:\";\n\n\t/**\n\t * The URI prefix for UN/LOCODE sub-divisions.\n\t * @internal\n\t */\n\tpublic static readonly LOCODE_SUBDIVISION_URI_PREFIX = \"unlcds:\";\n\n\t/**\n\t * The URI prefix for UN/LOCODE functions.\n\t * @internal\n\t */\n\tpublic static readonly LOCODE_FUNCTION_URI_PREFIX = \"unlcdf:\";\n\n\t/**\n\t * Cache for loaded locations by country code.\n\t * @internal\n\t */\n\tprivate static readonly _locationsByCountry: { [country: string]: IUnLocodeLocation[] } = {};\n\n\t/**\n\t * Cache for loaded subdivisions by country code.\n\t * @internal\n\t */\n\tprivate static readonly _subdivisionsByCountry: { [country: string]: IUnLocodeSubdivision[] } =\n\t\t{};\n\n\t/**\n\t * Get the list of UN/LOCODE countries.\n\t * @see https://vocabulary.uncefact.org/unlocode-countries\n\t * @returns The list of UN/LOCODE countries.\n\t */\n\tpublic static async getCountries(): Promise<IUnLocodeCountry[]> {\n\t\treturn UN_LOCODE_COUNTRIES;\n\t}\n\n\t/**\n\t * Get the country by its uri.\n\t * @param countryUri The country URI.\n\t * @see https://vocabulary.uncefact.org/unlocode-countries\n\t * @returns The country information or undefined if not found.\n\t */\n\tpublic static async getCountryByUri(countryUri: string): Promise<IUnLocodeCountry | undefined> {\n\t\tGuards.stringValue(UnLocodes.CLASS_NAME, nameof(countryUri), countryUri);\n\n\t\treturn UN_LOCODE_COUNTRIES.find(country => country.uri === countryUri);\n\t}\n\n\t/**\n\t * Get the country by its value.\n\t * @param countryValue The country value.\n\t * @see https://vocabulary.uncefact.org/unlocode-countries\n\t * @returns The country information or undefined if not found.\n\t */\n\tpublic static async getCountryByValue(\n\t\tcountryValue: string\n\t): Promise<IUnLocodeCountry | undefined> {\n\t\tGuards.stringValue(UnLocodes.CLASS_NAME, nameof(countryValue), countryValue);\n\n\t\treturn UN_LOCODE_COUNTRIES.find(country => country.value === countryValue);\n\t}\n\n\t/**\n\t * Get the list of UN/LOCODE functions.\n\t * @see https://vocabulary.uncefact.org/unlocode-functions\n\t * @returns The list of UN/LOCODE functions.\n\t */\n\tpublic static async getFunctions(): Promise<IUnLocodeFunction[]> {\n\t\treturn UN_LOCODE_FUNCTIONS;\n\t}\n\n\t/**\n\t * Get the function by its uri.\n\t * @param functionUri The function URI.\n\t * @see https://vocabulary.uncefact.org/unlocode-functions\n\t * @returns The function information or undefined if not found.\n\t */\n\tpublic static async getFunctionByUri(\n\t\tfunctionUri: string\n\t): Promise<IUnLocodeFunction | undefined> {\n\t\tGuards.stringValue(UnLocodes.CLASS_NAME, nameof(functionUri), functionUri);\n\n\t\treturn UN_LOCODE_FUNCTIONS.find(func => func.uri === functionUri);\n\t}\n\n\t/**\n\t * Get the function by its value.\n\t * @param functionValue The function value.\n\t * @see https://vocabulary.uncefact.org/unlocode-functions\n\t * @returns The function information or undefined if not found.\n\t */\n\tpublic static async getFunctionByValue(\n\t\tfunctionValue: string\n\t): Promise<IUnLocodeFunction | undefined> {\n\t\tGuards.stringValue(UnLocodes.CLASS_NAME, nameof(functionValue), functionValue);\n\n\t\treturn UN_LOCODE_FUNCTIONS.find(func => func.value === functionValue);\n\t}\n\n\t/**\n\t * Get the location by its UN/LOCODE.\n\t * @param unLocode The UN/LOCODE value.\n\t * @see https://vocabulary.uncefact.org/unlocode-about\n\t * @returns The location information or undefined if not found.\n\t */\n\tpublic static async getLocationByCode(unLocode: string): Promise<IUnLocodeLocation | undefined> {\n\t\tGuards.stringValue(UnLocodes.CLASS_NAME, nameof(unLocode), unLocode);\n\n\t\tconst countryCode = unLocode.slice(0, 2);\n\t\tconst locationCode = unLocode.slice(2);\n\t\tif (!Is.stringValue(countryCode) || !Is.stringValue(locationCode)) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tconst locations = await UnLocodes.getLocations(countryCode);\n\t\treturn locations.find(location => location.locode === unLocode);\n\t}\n\n\t/**\n\t * Get the location by its UN/LOCODE uri.\n\t * @param unLocodeUri The UN/LOCODE URI.\n\t * @see https://vocabulary.uncefact.org/unlocode-about\n\t * @returns The location information or undefined if not found.\n\t */\n\tpublic static async getLocationByUri(\n\t\tunLocodeUri: string\n\t): Promise<IUnLocodeLocation | undefined> {\n\t\tGuards.stringValue(UnLocodes.CLASS_NAME, nameof(unLocodeUri), unLocodeUri);\n\n\t\tconst normalized = unLocodeUri.replace(UnLocodes.LOCODE_LOCATION_URI_PREFIX, \"\").trim();\n\n\t\treturn UnLocodes.getLocationByCode(normalized);\n\t}\n\n\t/**\n\t * Get the location by its label.\n\t * @param label The location label.\n\t * @see https://vocabulary.uncefact.org/unlocode-about\n\t * @returns The location information or undefined if not found.\n\t */\n\tpublic static async getLocationByLabel(label: string): Promise<IUnLocodeLocation | undefined> {\n\t\tGuards.stringValue(UnLocodes.CLASS_NAME, nameof(label), label);\n\n\t\tconst normalized = label.trim().toLowerCase();\n\t\tfor (const country of UN_LOCODE_COUNTRIES) {\n\t\t\tconst locations = await UnLocodes.getLocations(country.value);\n\t\t\tconst match = locations.find(\n\t\t\t\tlocation =>\n\t\t\t\t\tlocation.label.toLowerCase() === normalized ||\n\t\t\t\t\tlocation.labelWithDiacritics.toLowerCase() === normalized\n\t\t\t);\n\t\t\tif (match) {\n\t\t\t\treturn match;\n\t\t\t}\n\t\t}\n\n\t\treturn undefined;\n\t}\n\n\t/**\n\t * Get the subdivision by its code.\n\t * @param subdivisionCode The subdivision code (e.g., \"AD04\").\n\t * @returns The subdivision information or undefined if not found.\n\t */\n\tpublic static async getSubdivisionByCode(\n\t\tsubdivisionCode: string\n\t): Promise<IUnLocodeSubdivision | undefined> {\n\t\tGuards.stringValue(UnLocodes.CLASS_NAME, nameof(subdivisionCode), subdivisionCode);\n\n\t\tconst countryCode = subdivisionCode.slice(0, 2);\n\t\tconst code = subdivisionCode.slice(2);\n\t\tif (!Is.stringValue(countryCode) || !Is.stringValue(code)) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tconst subdivisions = await UnLocodes.getSubdivisions(countryCode);\n\t\treturn subdivisions.find(subdivision => subdivision.code === code);\n\t}\n\n\t/**\n\t * Get the subdivision by its URI.\n\t * @param subdivisionUri The subdivision URI (e.g., \"unlcds:AD04\").\n\t * @returns The subdivision information or undefined if not found.\n\t */\n\tpublic static async getSubdivisionByUri(\n\t\tsubdivisionUri: string\n\t): Promise<IUnLocodeSubdivision | undefined> {\n\t\tGuards.stringValue(UnLocodes.CLASS_NAME, nameof(subdivisionUri), subdivisionUri);\n\n\t\tconst normalized = subdivisionUri.replace(UnLocodes.LOCODE_SUBDIVISION_URI_PREFIX, \"\").trim();\n\n\t\treturn UnLocodes.getSubdivisionByCode(normalized);\n\t}\n\n\t/**\n\t * Get the locations for a given country code.\n\t * @param countryCode The country code.\n\t * @returns The list of locations for the country.\n\t * @throws Error if the country code is invalid or if the data file cannot be loaded.\n\t */\n\tpublic static async getLocations(countryCode: string): Promise<IUnLocodeLocation[]> {\n\t\treturn UnLocodes.loadCompressedFile<IUnLocodeLocationRecord, IUnLocodeLocation>(\n\t\t\t\"countries\",\n\t\t\tcountryCode,\n\t\t\t(normalized, record) => UnLocodes.mapCountryLocation(normalized, record),\n\t\t\tUnLocodes._locationsByCountry\n\t\t);\n\t}\n\n\t/**\n\t * Get the subdivisions for a given country code.\n\t * @param countryCode The country code.\n\t * @returns The list of subdivisions for the country.\n\t * @throws Error if the country code is invalid or if the data file cannot be loaded.\n\t */\n\tpublic static async getSubdivisions(countryCode: string): Promise<IUnLocodeSubdivision[]> {\n\t\treturn UnLocodes.loadCompressedFile<IUnLocodeCountrySubdivisionRecord, IUnLocodeSubdivision>(\n\t\t\t\"subdivisions\",\n\t\t\tcountryCode,\n\t\t\t(normalized, record) => UnLocodes.mapCountrySubdivision(normalized, record),\n\t\t\tUnLocodes._subdivisionsByCountry\n\t\t);\n\t}\n\n\t/**\n\t * Map a country location record to a location object.\n\t * @param countryCode\tThe country code.\n\t * @param record The country location record.\n\t * @returns The mapped location object.\n\t * @internal\n\t */\n\tprivate static mapCountryLocation(\n\t\tcountryCode: string,\n\t\trecord: IUnLocodeLocationRecord\n\t): IUnLocodeLocation {\n\t\tconst locode = `${countryCode}${record.locationCode}`;\n\t\tconst countryCodeUri = UN_LOCODE_COUNTRIES.find(country => country.value === countryCode)?.uri;\n\t\tconst countrySubdivisionUri = Is.stringValue(record.subdivisionCode)\n\t\t\t? `${UnLocodes.LOCODE_SUBDIVISION_URI_PREFIX}${countryCode}${record.subdivisionCode}`\n\t\t\t: \"\";\n\t\tconst functions = record.function\n\t\t\t? record.function.split(\"\").map(code => `${UnLocodes.LOCODE_FUNCTION_URI_PREFIX}${code}`)\n\t\t\t: [];\n\t\tconst location: IUnLocodeLocation = {\n\t\t\tlocode,\n\t\t\tlocodeUri: `${UnLocodes.LOCODE_LOCATION_URI_PREFIX}${locode}`,\n\t\t\tlabel: record.label,\n\t\t\t// If the label with diacritics is not provided, use the regular label as fallback\n\t\t\t// as we don't store them if they are the same\n\t\t\tlabelWithDiacritics: record.labelWithDiacritics ?? record.label,\n\t\t\tcountryCodeUri: countryCodeUri as UnLocodeCountriesList,\n\t\t\tcountrySubdivisionUri,\n\t\t\tfunctions: functions as UnLocodeFunctionsList[]\n\t\t};\n\t\tif (Is.number(record.lat) && Is.number(record.lng)) {\n\t\t\tlocation.geoCoordinates = {\n\t\t\t\tlatitude: record.lat,\n\t\t\t\tlongitude: record.lng\n\t\t\t};\n\t\t}\n\t\treturn location;\n\t}\n\n\t/**\n\t * Map a country subdivision record to a subdivision object.\n\t * @param countryCode The country code.\n\t * @param record The country subdivision record.\n\t * @returns The mapped subdivision object.\n\t * @internal\n\t */\n\tprivate static mapCountrySubdivision(\n\t\tcountryCode: string,\n\t\trecord: IUnLocodeCountrySubdivisionRecord\n\t): IUnLocodeSubdivision {\n\t\tconst countryCodeUri = UN_LOCODE_COUNTRIES.find(country => country.value === countryCode)?.uri;\n\t\tconst subdivision: IUnLocodeSubdivision = {\n\t\t\tcode: record.code,\n\t\t\tsubdivisionUri: `${UnLocodes.LOCODE_SUBDIVISION_URI_PREFIX}${countryCode}${record.code}`,\n\t\t\tlabel: record.label,\n\t\t\tcountryCodeUri,\n\t\t\ttype: record.type\n\t\t};\n\t\treturn subdivision;\n\t}\n\n\t/**\n\t * Load a compressed file.\n\t * @param folder The folder to load (e.g., \"countries\" or \"subdivisions\").\n\t * @param countryCode The country code.\n\t * @return The loaded data as an array of records.\n\t * @throws Error if the file cannot be loaded or decompressed.\n\t * @internal\n\t */\n\tprivate static async loadCompressedFile<R, M>(\n\t\tfolder: string,\n\t\tcountryCode: string,\n\t\tmapMethod: (countryCode: string, record: R) => M,\n\t\tcache: { [country: string]: M[] }\n\t): Promise<M[]> {\n\t\tGuards.stringValue(UnLocodes.CLASS_NAME, nameof(countryCode), countryCode);\n\n\t\tconst normalized = countryCode.replace(UnLocodes.LOCODE_COUNTRY_URI_PREFIX, \"\").toUpperCase();\n\t\tconst cached = cache[normalized];\n\t\tif (cached) {\n\t\t\treturn cached;\n\t\t}\n\n\t\ttry {\n\t\t\tconst thisFilename = fileURLToPath(import.meta.url);\n\t\t\tconst thisDirname = path.dirname(thisFilename);\n\t\t\tconst filePath = path.join(thisDirname, `../data/${folder}/`, `${normalized}.json.gz`);\n\t\t\tconst compressed = await fs.readFile(filePath);\n\t\t\tconst jsonBuffer = await Compression.decompress(compressed, CompressionType.Gzip);\n\t\t\tconst records = ObjectHelper.fromBytes<R[]>(jsonBuffer);\n\t\t\tcache[normalized] = records.map(record => mapMethod(normalized, record));\n\t\t} catch {}\n\n\t\treturn cache[normalized] ?? [];\n\t}\n}\n"]}
1
+ {"version":3,"file":"unLocodes.js","sourceRoot":"","sources":["../../../src/helpers/unLocodes.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,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AG,MAAM,WAAW,CAAC,UAAU,CAAC,UAAU,EAAE,eAAe,CAAC,IAAI,CAAC,CAAC;YAClF,MAAM,OAAO,GAAG,YAAY,CAAC,SAAS,CAAM,UAAU,CAAC,CAAC;YACxD,KAAK,CAAC,UAAU,CAAC,GAAG,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,SAAS,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC,CAAC;QAC1E,CAAC;QAAC,MAAM,CAAC,CAAA,CAAC;QAEV,OAAO,KAAK,CAAC,UAAU,CAAC,IAAI,EAAE,CAAC;IAChC,CAAC","sourcesContent":["// Copyright 2026 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport fs from \"node:fs/promises\";\nimport { Compression, CompressionType, Guards, Is, ObjectHelper } from \"@twin.org/core\";\nimport { nameof } from \"@twin.org/nameof\";\nimport type { IUnLocodeCountry } from \"../models/IUnLocodeCountry.js\";\nimport type { IUnLocodeCountrySubdivisionRecord } from \"../models/IUnLocodeCountrySubdivisionRecord.js\";\nimport type { IUnLocodeFunction } from \"../models/IUnLocodeFunction.js\";\nimport type { IUnLocodeLocation } from \"../models/IUnLocodeLocation.js\";\nimport type { IUnLocodeLocationRecord } from \"../models/IUnLocodeLocationRecord.js\";\nimport type { IUnLocodeSubdivision } from \"../models/IUnLocodeSubdivision.js\";\nimport { UN_LOCODE_COUNTRIES } from \"../models/locode/unLocodeCountries.js\";\nimport type { UnLocodeCountriesList } from \"../models/locode/unLocodeCountriesList.js\";\nimport { UN_LOCODE_FUNCTIONS } from \"../models/locode/unLocodeFunctions.js\";\nimport type { UnLocodeFunctionsList } from \"../models/locode/unLocodeFunctionsList.js\";\n\n/**\n * A class handling UN/LOCODEs.\n * @see https://vocabulary.uncefact.org/unlocode-about\n */\nexport class UnLocodes {\n\t/**\n\t * The class name.\n\t * @internal\n\t */\n\tpublic static readonly CLASS_NAME = nameof<UnLocodes>();\n\n\t/**\n\t * The URI prefix for UN/LOCODE locations.\n\t * @internal\n\t */\n\tpublic static readonly LOCODE_COUNTRY_URI_PREFIX = \"unlcdc:\";\n\n\t/**\n\t * The URI prefix for UN/LOCODE locations.\n\t * @internal\n\t */\n\tpublic static readonly LOCODE_LOCATION_URI_PREFIX = \"unlcd:\";\n\n\t/**\n\t * The URI prefix for UN/LOCODE sub-divisions.\n\t * @internal\n\t */\n\tpublic static readonly LOCODE_SUBDIVISION_URI_PREFIX = \"unlcds:\";\n\n\t/**\n\t * The URI prefix for UN/LOCODE functions.\n\t * @internal\n\t */\n\tpublic static readonly LOCODE_FUNCTION_URI_PREFIX = \"unlcdf:\";\n\n\t/**\n\t * Cache for loaded locations by country code.\n\t * @internal\n\t */\n\tprivate static readonly _locationsByCountry: { [country: string]: IUnLocodeLocation[] } = {};\n\n\t/**\n\t * Cache for loaded subdivisions by country code.\n\t * @internal\n\t */\n\tprivate static readonly _subdivisionsByCountry: { [country: string]: IUnLocodeSubdivision[] } =\n\t\t{};\n\n\t/**\n\t * Get the list of UN/LOCODE countries.\n\t * @see https://vocabulary.uncefact.org/unlocode-countries\n\t * @returns The list of UN/LOCODE countries.\n\t */\n\tpublic static async getCountries(): Promise<IUnLocodeCountry[]> {\n\t\treturn UN_LOCODE_COUNTRIES;\n\t}\n\n\t/**\n\t * Get the country by its uri.\n\t * @param countryUri The country URI.\n\t * @see https://vocabulary.uncefact.org/unlocode-countries\n\t * @returns The country information or undefined if not found.\n\t */\n\tpublic static async getCountryByUri(countryUri: string): Promise<IUnLocodeCountry | undefined> {\n\t\tGuards.stringValue(UnLocodes.CLASS_NAME, nameof(countryUri), countryUri);\n\n\t\treturn UN_LOCODE_COUNTRIES.find(country => country.uri === countryUri);\n\t}\n\n\t/**\n\t * Get the country by its value.\n\t * @param countryValue The country value.\n\t * @see https://vocabulary.uncefact.org/unlocode-countries\n\t * @returns The country information or undefined if not found.\n\t */\n\tpublic static async getCountryByValue(\n\t\tcountryValue: string\n\t): Promise<IUnLocodeCountry | undefined> {\n\t\tGuards.stringValue(UnLocodes.CLASS_NAME, nameof(countryValue), countryValue);\n\n\t\treturn UN_LOCODE_COUNTRIES.find(country => country.value === countryValue);\n\t}\n\n\t/**\n\t * Get the list of UN/LOCODE functions.\n\t * @see https://vocabulary.uncefact.org/unlocode-functions\n\t * @returns The list of UN/LOCODE functions.\n\t */\n\tpublic static async getFunctions(): Promise<IUnLocodeFunction[]> {\n\t\treturn UN_LOCODE_FUNCTIONS;\n\t}\n\n\t/**\n\t * Get the function by its uri.\n\t * @param functionUri The function URI.\n\t * @see https://vocabulary.uncefact.org/unlocode-functions\n\t * @returns The function information or undefined if not found.\n\t */\n\tpublic static async getFunctionByUri(\n\t\tfunctionUri: string\n\t): Promise<IUnLocodeFunction | undefined> {\n\t\tGuards.stringValue(UnLocodes.CLASS_NAME, nameof(functionUri), functionUri);\n\n\t\treturn UN_LOCODE_FUNCTIONS.find(func => func.uri === functionUri);\n\t}\n\n\t/**\n\t * Get the function by its value.\n\t * @param functionValue The function value.\n\t * @see https://vocabulary.uncefact.org/unlocode-functions\n\t * @returns The function information or undefined if not found.\n\t */\n\tpublic static async getFunctionByValue(\n\t\tfunctionValue: string\n\t): Promise<IUnLocodeFunction | undefined> {\n\t\tGuards.stringValue(UnLocodes.CLASS_NAME, nameof(functionValue), functionValue);\n\n\t\treturn UN_LOCODE_FUNCTIONS.find(func => func.value === functionValue);\n\t}\n\n\t/**\n\t * Get the location by its UN/LOCODE.\n\t * @param unLocode The UN/LOCODE value.\n\t * @see https://vocabulary.uncefact.org/unlocode-about\n\t * @returns The location information or undefined if not found.\n\t */\n\tpublic static async getLocationByCode(unLocode: string): Promise<IUnLocodeLocation | undefined> {\n\t\tGuards.stringValue(UnLocodes.CLASS_NAME, nameof(unLocode), unLocode);\n\n\t\tconst countryCode = unLocode.slice(0, 2);\n\t\tconst locationCode = unLocode.slice(2);\n\t\tif (!Is.stringValue(countryCode) || !Is.stringValue(locationCode)) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tconst locations = await UnLocodes.getLocations(countryCode);\n\t\treturn locations.find(location => location.locode === unLocode);\n\t}\n\n\t/**\n\t * Get the location by its UN/LOCODE uri.\n\t * @param unLocodeUri The UN/LOCODE URI.\n\t * @see https://vocabulary.uncefact.org/unlocode-about\n\t * @returns The location information or undefined if not found.\n\t */\n\tpublic static async getLocationByUri(\n\t\tunLocodeUri: string\n\t): Promise<IUnLocodeLocation | undefined> {\n\t\tGuards.stringValue(UnLocodes.CLASS_NAME, nameof(unLocodeUri), unLocodeUri);\n\n\t\tconst normalized = unLocodeUri.replace(UnLocodes.LOCODE_LOCATION_URI_PREFIX, \"\").trim();\n\n\t\treturn UnLocodes.getLocationByCode(normalized);\n\t}\n\n\t/**\n\t * Get the location by its label.\n\t * @param label The location label.\n\t * @see https://vocabulary.uncefact.org/unlocode-about\n\t * @returns The location information or undefined if not found.\n\t */\n\tpublic static async getLocationByLabel(label: string): Promise<IUnLocodeLocation | undefined> {\n\t\tGuards.stringValue(UnLocodes.CLASS_NAME, nameof(label), label);\n\n\t\tconst normalized = label.trim().toLowerCase();\n\t\tfor (const country of UN_LOCODE_COUNTRIES) {\n\t\t\tconst locations = await UnLocodes.getLocations(country.value);\n\t\t\tconst match = locations.find(\n\t\t\t\tlocation =>\n\t\t\t\t\tlocation.label.toLowerCase() === normalized ||\n\t\t\t\t\tlocation.labelWithDiacritics.toLowerCase() === normalized\n\t\t\t);\n\t\t\tif (match) {\n\t\t\t\treturn match;\n\t\t\t}\n\t\t}\n\n\t\treturn undefined;\n\t}\n\n\t/**\n\t * Get the subdivision by its code.\n\t * @param subdivisionCode The subdivision code (e.g., \"AD04\").\n\t * @returns The subdivision information or undefined if not found.\n\t */\n\tpublic static async getSubdivisionByCode(\n\t\tsubdivisionCode: string\n\t): Promise<IUnLocodeSubdivision | undefined> {\n\t\tGuards.stringValue(UnLocodes.CLASS_NAME, nameof(subdivisionCode), subdivisionCode);\n\n\t\tconst countryCode = subdivisionCode.slice(0, 2);\n\t\tconst code = subdivisionCode.slice(2);\n\t\tif (!Is.stringValue(countryCode) || !Is.stringValue(code)) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tconst subdivisions = await UnLocodes.getSubdivisions(countryCode);\n\t\treturn subdivisions.find(subdivision => subdivision.code === code);\n\t}\n\n\t/**\n\t * Get the subdivision by its URI.\n\t * @param subdivisionUri The subdivision URI (e.g., \"unlcds:AD04\").\n\t * @returns The subdivision information or undefined if not found.\n\t */\n\tpublic static async getSubdivisionByUri(\n\t\tsubdivisionUri: string\n\t): Promise<IUnLocodeSubdivision | undefined> {\n\t\tGuards.stringValue(UnLocodes.CLASS_NAME, nameof(subdivisionUri), subdivisionUri);\n\n\t\tconst normalized = subdivisionUri.replace(UnLocodes.LOCODE_SUBDIVISION_URI_PREFIX, \"\").trim();\n\n\t\treturn UnLocodes.getSubdivisionByCode(normalized);\n\t}\n\n\t/**\n\t * Get the locations for a given country code.\n\t * @param countryCode The country code.\n\t * @returns The list of locations for the country.\n\t * @throws Error if the country code is invalid or if the data file cannot be loaded.\n\t */\n\tpublic static async getLocations(countryCode: string): Promise<IUnLocodeLocation[]> {\n\t\treturn UnLocodes.loadCompressedFile<IUnLocodeLocationRecord, IUnLocodeLocation>(\n\t\t\t\"countries\",\n\t\t\tcountryCode,\n\t\t\t(normalized, record) => UnLocodes.mapCountryLocation(normalized, record),\n\t\t\tUnLocodes._locationsByCountry\n\t\t);\n\t}\n\n\t/**\n\t * Get the subdivisions for a given country code.\n\t * @param countryCode The country code.\n\t * @returns The list of subdivisions for the country.\n\t * @throws Error if the country code is invalid or if the data file cannot be loaded.\n\t */\n\tpublic static async getSubdivisions(countryCode: string): Promise<IUnLocodeSubdivision[]> {\n\t\treturn UnLocodes.loadCompressedFile<IUnLocodeCountrySubdivisionRecord, IUnLocodeSubdivision>(\n\t\t\t\"subdivisions\",\n\t\t\tcountryCode,\n\t\t\t(normalized, record) => UnLocodes.mapCountrySubdivision(normalized, record),\n\t\t\tUnLocodes._subdivisionsByCountry\n\t\t);\n\t}\n\n\t/**\n\t * Map a country location record to a location object.\n\t * @param countryCode\tThe country code.\n\t * @param record The country location record.\n\t * @returns The mapped location object.\n\t * @internal\n\t */\n\tprivate static mapCountryLocation(\n\t\tcountryCode: string,\n\t\trecord: IUnLocodeLocationRecord\n\t): IUnLocodeLocation {\n\t\tconst locode = `${countryCode}${record.locationCode}`;\n\t\tconst countryCodeUri = UN_LOCODE_COUNTRIES.find(country => country.value === countryCode)?.uri;\n\t\tconst countrySubdivisionUri = Is.stringValue(record.subdivisionCode)\n\t\t\t? `${UnLocodes.LOCODE_SUBDIVISION_URI_PREFIX}${countryCode}${record.subdivisionCode}`\n\t\t\t: \"\";\n\t\tconst functions = record.function\n\t\t\t? record.function.split(\"\").map(code => `${UnLocodes.LOCODE_FUNCTION_URI_PREFIX}${code}`)\n\t\t\t: [];\n\t\tconst location: IUnLocodeLocation = {\n\t\t\tlocode,\n\t\t\tlocodeUri: `${UnLocodes.LOCODE_LOCATION_URI_PREFIX}${locode}`,\n\t\t\tlabel: record.label,\n\t\t\t// If the label with diacritics is not provided, use the regular label as fallback\n\t\t\t// as we don't store them if they are the same\n\t\t\tlabelWithDiacritics: record.labelWithDiacritics ?? record.label,\n\t\t\tcountryCodeUri: countryCodeUri as UnLocodeCountriesList,\n\t\t\tcountrySubdivisionUri,\n\t\t\tfunctions: functions as UnLocodeFunctionsList[]\n\t\t};\n\t\tif (Is.number(record.lat) && Is.number(record.lng)) {\n\t\t\tlocation.geoCoordinates = {\n\t\t\t\tlatitude: record.lat,\n\t\t\t\tlongitude: record.lng\n\t\t\t};\n\t\t}\n\t\treturn location;\n\t}\n\n\t/**\n\t * Map a country subdivision record to a subdivision object.\n\t * @param countryCode The country code.\n\t * @param record The country subdivision record.\n\t * @returns The mapped subdivision object.\n\t * @internal\n\t */\n\tprivate static mapCountrySubdivision(\n\t\tcountryCode: string,\n\t\trecord: IUnLocodeCountrySubdivisionRecord\n\t): IUnLocodeSubdivision {\n\t\tconst countryCodeUri = UN_LOCODE_COUNTRIES.find(country => country.value === countryCode)?.uri;\n\t\tconst subdivision: IUnLocodeSubdivision = {\n\t\t\tcode: record.code,\n\t\t\tsubdivisionUri: `${UnLocodes.LOCODE_SUBDIVISION_URI_PREFIX}${countryCode}${record.code}`,\n\t\t\tlabel: record.label,\n\t\t\tcountryCodeUri,\n\t\t\ttype: record.type\n\t\t};\n\t\treturn subdivision;\n\t}\n\n\t/**\n\t * Load a compressed file.\n\t * @param folder The folder to load (e.g., \"countries\" or \"subdivisions\").\n\t * @param countryCode The country code.\n\t * @return The loaded data as an array of records.\n\t * @throws Error if the file cannot be loaded or decompressed.\n\t * @internal\n\t */\n\tprivate static async loadCompressedFile<R, M>(\n\t\tfolder: string,\n\t\tcountryCode: string,\n\t\tmapMethod: (countryCode: string, record: R) => M,\n\t\tcache: { [country: string]: M[] }\n\t): Promise<M[]> {\n\t\tGuards.stringValue(UnLocodes.CLASS_NAME, nameof(countryCode), countryCode);\n\n\t\tconst normalized = countryCode.replace(UnLocodes.LOCODE_COUNTRY_URI_PREFIX, \"\").toUpperCase();\n\t\tconst cached = cache[normalized];\n\t\tif (cached) {\n\t\t\treturn cached;\n\t\t}\n\n\t\ttry {\n\t\t\tconst fileUrl = new URL(`../data/${folder}/${normalized}.json.gz`, import.meta.url);\n\t\t\tconst compressed = await fs.readFile(fileUrl);\n\t\t\tconst jsonBuffer = await Compression.decompress(compressed, CompressionType.Gzip);\n\t\t\tconst records = ObjectHelper.fromBytes<R[]>(jsonBuffer);\n\t\t\tcache[normalized] = records.map(record => mapMethod(normalized, record));\n\t\t} catch {}\n\n\t\treturn cache[normalized] ?? [];\n\t}\n}\n"]}
package/docs/changelog.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # @twin.org/standards-unece - Changelog
2
2
 
3
+ ## [0.0.3-next.45](https://github.com/twinfoundation/standards/compare/standards-unece-v0.0.3-next.44...standards-unece-v0.0.3-next.45) (2026-03-04)
4
+
5
+
6
+ ### Miscellaneous Chores
7
+
8
+ * **standards-unece:** Synchronize repo versions
9
+
10
+ ## [0.0.3-next.44](https://github.com/twinfoundation/standards/compare/standards-unece-v0.0.3-next.43...standards-unece-v0.0.3-next.44) (2026-03-04)
11
+
12
+
13
+ ### Bug Fixes
14
+
15
+ * replace fileURLToPath usage as not available in UI ([5557dab](https://github.com/twinfoundation/standards/commit/5557dabeacf0432fd28e84d44bbff480e0d99b65))
16
+
3
17
  ## [0.0.3-next.43](https://github.com/twinfoundation/standards/compare/standards-unece-v0.0.3-next.42...standards-unece-v0.0.3-next.43) (2026-02-25)
4
18
 
5
19
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/standards-unece",
3
- "version": "0.0.3-next.43",
3
+ "version": "0.0.3-next.45",
4
4
  "description": "Models which define the structure of UN/CEFACT Standard",
5
5
  "repository": {
6
6
  "type": "git",