From 9e95e21fc2103b1e4c12542b815c96b27e6686aa Mon Sep 17 00:00:00 2001 From: Erik Golinelli Date: Tue, 14 May 2024 11:52:53 +0200 Subject: [PATCH 1/3] lint --- bin/clean.mjs | 22 +++++++++++----------- src/data/countries-iso.ts | 20 ++++++++++---------- src/getCountry.ts | 6 ++---- src/getKeyValue.ts | 6 ++++-- src/getLanguage.ts | 18 +++++++++--------- tests/iso.test.ts | 2 +- 6 files changed, 37 insertions(+), 37 deletions(-) diff --git a/bin/clean.mjs b/bin/clean.mjs index 8bf4d83..97add59 100644 --- a/bin/clean.mjs +++ b/bin/clean.mjs @@ -1,20 +1,20 @@ //!/usr/bin/env node -import fs from 'fs' +import fs from "node:fs"; const libDirectoryPath = "./lib"; -console.log(`🗑️ Removing old files from ${libDirectoryPath}`); +console.log(`🗑 Removing old files from ${libDirectoryPath}`); //check if the directory exists if (!fs.existsSync(libDirectoryPath)) { - console.log("Directory not found... skipping"); + console.log("Directory not found... skipping"); } else { - await fs.rm(libDirectoryPath, { recursive: true, force: true }, (err) => { - if (err) { - // File deletion failed - console.error(err.message) - return - } - console.log(`Directory ${libDirectoryPath} deleted successfully`) - }) + await fs.rm(libDirectoryPath, { recursive: true, force: true }, (err) => { + if (err) { + // File deletion failed + console.error(err.message); + return; + } + console.log(`Directory ${libDirectoryPath} deleted successfully`); + }); } diff --git a/src/data/countries-iso.ts b/src/data/countries-iso.ts index 2d457a2..3158400 100644 --- a/src/data/countries-iso.ts +++ b/src/data/countries-iso.ts @@ -134,7 +134,7 @@ export const countriesIso = { BH: { languages: ["ar"], name: "Bahrain", - original: "‏البحرين", + original: "البحرين", iso3: "BHR", }, BI: { @@ -212,7 +212,7 @@ export const countriesIso = { BY: { languages: ["be", "ru"], name: "Belarus", - original: "Белару́сь", + original: "Беларусь", iso3: "BLR", }, BZ: { @@ -386,7 +386,7 @@ export const countriesIso = { EG: { languages: ["ar"], name: "Egypt", - original: "مصر‎", + original: "مصر�", iso3: "EGY", }, EH: { @@ -614,7 +614,7 @@ export const countriesIso = { IL: { languages: ["he", "ar"], name: "Israel", - original: "יִשְׂרָאֵל", + original: "ישראל", iso3: "ISR", }, IM: { @@ -626,7 +626,7 @@ export const countriesIso = { IN: { languages: ["hi", "en"], name: "India", - original: "भारत", + original: "भरत", iso3: "IND", }, IO: { @@ -812,7 +812,7 @@ export const countriesIso = { LY: { languages: ["ar"], name: "Libya", - original: "‏ليبيا", + original: "ليبيا", iso3: "LBY", }, MA: { @@ -854,7 +854,7 @@ export const countriesIso = { MH: { languages: ["en", "mh"], name: "Marshall Islands", - original: "M̧ajeļ", + original: "Majeļ", iso3: "MHL", }, MK: { @@ -872,7 +872,7 @@ export const countriesIso = { MM: { languages: ["my"], name: "Myanmar (Burma)", - original: "မြန်မာ", + original: "မနမာ", iso3: "MMR", }, MN: { @@ -1004,7 +1004,7 @@ export const countriesIso = { NP: { languages: ["ne"], name: "Nepal", - original: "नेपाल", + original: "नपल", iso3: "NPL", }, NR: { @@ -1472,7 +1472,7 @@ export const countriesIso = { YE: { languages: ["ar"], name: "Yemen", - original: "اليَمَن", + original: "اليمن", iso3: "YEM", }, YT: { diff --git a/src/getCountry.ts b/src/getCountry.ts index daf88a5..e8fee22 100644 --- a/src/getCountry.ts +++ b/src/getCountry.ts @@ -121,7 +121,7 @@ export function getCountry( } if (fields.includes("locale")) { - const localeCode = country.languages.map( + countryData.locale = country.languages.map( (langIso) => formatIso( langIso as ISOLangCode, @@ -129,11 +129,10 @@ export function getCountry( "locale", ) ?? countryIso, ) as string[]; - countryData.locale = localeCode; } if (fields.includes("language-code")) { - const isoCodes = country.languages?.map( + countryData["language-code"] = country.languages?.map( (langIso) => formatIso( langIso as ISOLangCode, @@ -141,7 +140,6 @@ export function getCountry( "language-code", ) ?? countryIso, ) as string[]; - countryData["language-code"] = isoCodes; } // If the requested fields is the code, return the iso country code diff --git a/src/getKeyValue.ts b/src/getKeyValue.ts index e021b9e..fe4d15f 100644 --- a/src/getKeyValue.ts +++ b/src/getKeyValue.ts @@ -20,9 +20,11 @@ export function getKeyValue( const unpackData = (data: unknown, field?: string): string[] => { if (typeof data === "string") { return [data]; - } else if (Array.isArray(data)) { + } + if (Array.isArray(data)) { return data.flatMap((item) => unpackData(item, field)); - } else if (typeof data === "object" && data !== null) { + } + if (typeof data === "object" && data !== null) { if (field && field in data) { return [data[field as keyof typeof data]]; } diff --git a/src/getLanguage.ts b/src/getLanguage.ts index 94344b8..a2236ef 100644 --- a/src/getLanguage.ts +++ b/src/getLanguage.ts @@ -44,7 +44,8 @@ export function getLanguage( // if the fields are iso, return the isoCode otherwise create the array of fields to return if (fields === "iso2") { return isoCode; - } else if (fields === "all") { + } + if (fields === "all") { fields = ["iso2", "name", "original", "iso3"]; } else if (typeof fields === "string") { fields = [fields as LanguageDataFields]; @@ -54,14 +55,13 @@ export function getLanguage( const collected: Partial = {}; if (Object.keys(fields).length === 1) { return language[Object.values(fields)[0] as LanguageFields]; - } else { - for (const field of fields) { - const key = field; - if (key === "iso2") { - collected["iso2"] = isoCode as ISOLangCode; - } else { - collected[key as LanguageFields] = language[key as LanguageFields]; - } + } + for (const field of fields) { + const key = field; + if (key === "iso2") { + collected.iso2 = isoCode as ISOLangCode; + } else { + collected[key as LanguageFields] = language[key as LanguageFields]; } } diff --git a/tests/iso.test.ts b/tests/iso.test.ts index 33aa1dd..83fa9eb 100644 --- a/tests/iso.test.ts +++ b/tests/iso.test.ts @@ -107,7 +107,7 @@ describe("ISO", () => { it("should return a field for the provided original name", () => { const isoCode = "Italia"; const result = getIso(isoCode, "country", "name"); - const expectedOriginal = countriesIso["IT"].name; + const expectedOriginal = countriesIso.IT.name; expect(result).toEqual(expectedOriginal); }); From 4c915c1671419d4c688f8041fcd99c13f918dc5b Mon Sep 17 00:00:00 2001 From: Erik Golinelli Date: Tue, 14 May 2024 11:53:28 +0200 Subject: [PATCH 2/3] build enhancements --- package.json | 12 +++++----- tsconfig.json | 61 +++++++++++++++++++++++---------------------------- 2 files changed, 33 insertions(+), 40 deletions(-) diff --git a/package.json b/package.json index c6879b7..16b9711 100644 --- a/package.json +++ b/package.json @@ -66,12 +66,13 @@ }, "scripts": { "prepare": "husky install", - "lint": "npx @biomejs/biome check --apply src tests", + "lint": "npx @biomejs/biome check --apply src tests/*.ts", "clean": "node bin/clean.mjs", "watch": "npx esbuild src/index.ts --outfile=lib/browser/isoToLanguage.js --global-name=isoToLanguage --allow-overwrite --watch --bundle --sourcemap", - "build": "pnpm run clean && concurrently pnpm:build-* ", - "build-node": "tsup src/**/*.ts lib", - "build-browser": "npx esbuild src/index.ts --outfile=lib/browser/isoToLanguage.js --global-name=isoToLanguage --allow-overwrite --bundle --minify --sourcemap", + "build": "pnpm run clean && concurrently pnpm:build-*", + "build-node": "tsup", + "build-dataset": "esbuild src/data/*.ts --outdir=lib/data/ --minify", + "build-browser": "esbuild src/index.ts --outfile=lib/browser/isoToLanguage.js --global-name=isoToLanguage --allow-overwrite --bundle --minify --sourcemap", "test": "vitest --coverage" }, "tsup": { @@ -80,14 +81,13 @@ "bundle": false, "treeshake": true, "minify": true, - "clean": true, "shims": true, "cjsInterop": true, "target": "node16", "moduleName": "isoToLanguage", "format": ["esm", "cjs"], "tsconfig": "./tsconfig.json", - "entry": ["src/**/*.ts"], + "entry": ["src/*.ts"], "outDir": "lib" } } diff --git a/tsconfig.json b/tsconfig.json index 75c5c11..09dddac 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,36 +1,29 @@ { - "compilerOptions": { - "target": "ES2022", - "module": "ES2022", - "moduleResolution": "node", // Search under node_modules for non-relative imports. - // Process & infer types from .js files. - "pretty": true, - "sourceMap": true, - "strict": true, // Enable the strictest settings like strictNullChecks & noImplicitAny. - "isolatedModules": true, // Disallow features that require cross-file information for emitting. - "allowSyntheticDefaultImports": true, - "skipLibCheck": true, - "noImplicitAny": true, - "strictNullChecks": true, - "declaration": true, - "forceConsistentCasingInFileNames": true, - "rootDir": "src", - "outDir": "lib", - "esModuleInterop": true, - // "verbatimModuleSyntax": true, - "typeRoots": [ - "node_modules/@types/" - ] - }, - "include": [ - "src/**/*" - ], - "exclude": [ - "node_modules", - "coverage", - "docs", - "example", - "lib", - "src/isoToLanguage.ts" - ] + "compilerOptions": { + "target": "ES2022", + "module": "ES2022", + "moduleResolution": "node", + "pretty": true, + "sourceMap": true, + "strict": true, + "isolatedModules": true, + "allowSyntheticDefaultImports": true, + "skipLibCheck": true, + "noImplicitAny": true, + "strictNullChecks": true, + "declaration": true, + "forceConsistentCasingInFileNames": true, + "rootDir": "src", + "outDir": "lib", + "esModuleInterop": true + }, + "include": ["src/**/*", "data/**/*"], + "exclude": [ + "node_modules", + "coverage", + "docs", + "example", + "lib", + "src/isoToLanguage.ts" + ] } From cbb872a7e0faa84f0a45705c9579812a47e92982 Mon Sep 17 00:00:00 2001 From: Erik Golinelli Date: Tue, 14 May 2024 12:01:06 +0200 Subject: [PATCH 3/3] cleanup --- .prettierrc | 10 ---------- babel.config.json | 13 ------------- package.json | 10 +--------- 3 files changed, 1 insertion(+), 32 deletions(-) delete mode 100644 .prettierrc delete mode 100644 babel.config.json diff --git a/.prettierrc b/.prettierrc deleted file mode 100644 index 2d11a12..0000000 --- a/.prettierrc +++ /dev/null @@ -1,10 +0,0 @@ -{ - "trailingComma": "es5", - "tabWidth": 4, - "semi": false, - "singleQuote": true, - "printWidth": 100, - "bracketSameLine": false, - "bracketSpacing": true, - "arrowParens": "always" -} diff --git a/babel.config.json b/babel.config.json deleted file mode 100644 index b05fea5..0000000 --- a/babel.config.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "presets": [ - [ - "@babel/preset-env", - { - "targets": { - "node": "current" - } - } - ], - "@babel/preset-typescript" - ] -} diff --git a/package.json b/package.json index 16b9711..2db691f 100644 --- a/package.json +++ b/package.json @@ -32,15 +32,7 @@ "ISO 3166-1", "language tag" ], - "files": [ - "lib/", - "tests/", - "*.json", - ".gitignore", - ".eslintrc.js", - "LICENSE", - "README.md" - ], + "files": ["lib/", "tests/", "*.json", ".gitignore", "LICENSE", "*.md"], "author": "Erik Golinelli ", "repository": { "type": "git",