Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better build #10

Merged
merged 3 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions .prettierrc

This file was deleted.

13 changes: 0 additions & 13 deletions babel.config.json

This file was deleted.

22 changes: 11 additions & 11 deletions bin/clean.mjs
Original file line number Diff line number Diff line change
@@ -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`);
});
}
22 changes: 7 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/erikyo>",
"repository": {
"type": "git",
Expand All @@ -66,12 +58,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": {
Expand All @@ -80,14 +73,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"
}
}
20 changes: 10 additions & 10 deletions src/data/countries-iso.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export const countriesIso = {
BH: {
languages: ["ar"],
name: "Bahrain",
original: "البحرين",
original: "البحرين",
iso3: "BHR",
},
BI: {
Expand Down Expand Up @@ -212,7 +212,7 @@ export const countriesIso = {
BY: {
languages: ["be", "ru"],
name: "Belarus",
original: "Белару́сь",
original: "Беларусь",
iso3: "BLR",
},
BZ: {
Expand Down Expand Up @@ -386,7 +386,7 @@ export const countriesIso = {
EG: {
languages: ["ar"],
name: "Egypt",
original: "مصر",
original: "مصر",
iso3: "EGY",
},
EH: {
Expand Down Expand Up @@ -614,7 +614,7 @@ export const countriesIso = {
IL: {
languages: ["he", "ar"],
name: "Israel",
original: "יִשְׂרָאֵל",
original: "ישראל",
iso3: "ISR",
},
IM: {
Expand All @@ -626,7 +626,7 @@ export const countriesIso = {
IN: {
languages: ["hi", "en"],
name: "India",
original: "भारत",
original: "भरत",
iso3: "IND",
},
IO: {
Expand Down Expand Up @@ -812,7 +812,7 @@ export const countriesIso = {
LY: {
languages: ["ar"],
name: "Libya",
original: "ليبيا",
original: "ليبيا",
iso3: "LBY",
},
MA: {
Expand Down Expand Up @@ -854,7 +854,7 @@ export const countriesIso = {
MH: {
languages: ["en", "mh"],
name: "Marshall Islands",
original: "M̧ajeļ",
original: "Majeļ",
iso3: "MHL",
},
MK: {
Expand All @@ -872,7 +872,7 @@ export const countriesIso = {
MM: {
languages: ["my"],
name: "Myanmar (Burma)",
original: "မြန်မာ",
original: "မနမာ",
iso3: "MMR",
},
MN: {
Expand Down Expand Up @@ -1004,7 +1004,7 @@ export const countriesIso = {
NP: {
languages: ["ne"],
name: "Nepal",
original: "नेपाल",
original: "नपल",
iso3: "NPL",
},
NR: {
Expand Down Expand Up @@ -1472,7 +1472,7 @@ export const countriesIso = {
YE: {
languages: ["ar"],
name: "Yemen",
original: "اليَمَن",
original: "اليمن",
iso3: "YEM",
},
YT: {
Expand Down
6 changes: 2 additions & 4 deletions src/getCountry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,27 +121,25 @@ export function getCountry(
}

if (fields.includes("locale")) {
const localeCode = country.languages.map(
countryData.locale = country.languages.map(
(langIso) =>
formatIso(
langIso as ISOLangCode,
countryIso as ISOCountryCode,
"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,
countryIso as ISOCountryCode,
"language-code",
) ?? countryIso,
) as string[];
countryData["language-code"] = isoCodes;
}

// If the requested fields is the code, return the iso country code
Expand Down
6 changes: 4 additions & 2 deletions src/getKeyValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]];
}
Expand Down
18 changes: 9 additions & 9 deletions src/getLanguage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -54,14 +55,13 @@ export function getLanguage(
const collected: Partial<LanguageData> = {};
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];
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/iso.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
61 changes: 27 additions & 34 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
Loading