diff --git a/package.json b/package.json index c66abd4..e235bf9 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,8 @@ "version": "1.0.0", "description": "light and fast module to convert iso country and languages codes into country names, there is no unnecessary stuff only the bare essentials", "scripts": { + "prepare": "husky install", + "clean": "node bin/clean.mjs", "module-cjs": "swc src -d lib/cjs -C module.type=commonjs --config-file .swcrc-cjs", "module-esm": "swc src -d lib/esm -C module.type=es6 --config-file .swcrc-esm", "module-umd": "npx spack", @@ -10,9 +12,7 @@ "build": "pnpm run clean && concurrently pnpm:module-* && pnpm run types", "types": "tsc -p tsconfig.json --module Node16 --emitDeclarationOnly --declaration --declarationDir ./lib/@types --skipLibCheck", "lint": "eslint ./src --fix", - "clean": "node bin/clean.mjs", - "test": "jest", - "prepare": "husky install" + "test": "jest" }, "license": "ISC", "main": "./lib/esm/index.js", @@ -56,11 +56,13 @@ "@swc/cli": "^0.1.63", "@swc/core": "1.2.239", "@swc/jest": "^0.2.29", + "@jest/globals": "^29.7.0", "@types/jest": "^29.5.11", "@types/node": "^20.10.4", "@types/webpack": "^5.28.5", "@typescript-eslint/eslint-plugin": "^6.14.0", "@typescript-eslint/parser": "^6.14.0", + "concurrently": "^8.2.2", "eslint": "^8.55.0", "eslint-config-standard": "^17.1.0", "eslint-plugin-n": "^16.3.1", @@ -68,11 +70,13 @@ "husky": "^8.0.3", "jest": "^29.7.0", "lint-staged": "^15.2.0", - "concurrently": "^8.2.2", "prettier": "^2.8.8", "typescript": "^5.3.3" }, "lint-staged": { + "tests/**/*.{ts}": [ + "prettier --write" + ], "src/**/*.{ts}": [ "eslint --fix", "prettier --write", diff --git a/tests/getAll.test.ts b/tests/getAll.test.ts index 8b8a7cf..0ebee6c 100644 --- a/tests/getAll.test.ts +++ b/tests/getAll.test.ts @@ -88,7 +88,9 @@ describe('getAll', () => { expect(result).toEqual(expect.arrayContaining(expected as unknown[])) } else { // For other types, test objects - expect(result).toMatchObject(expected as Record | Record[]) + expect(result).toMatchObject( + expected as Record | Record[] + ) } }) }) diff --git a/tests/getAllLanguageCodesByISO.test.ts b/tests/getAllLanguageCodesByISO.test.ts index 8c21d77..e0ce39b 100644 --- a/tests/getAllLanguageCodesByISO.test.ts +++ b/tests/getAllLanguageCodesByISO.test.ts @@ -1,13 +1,14 @@ import { describe, expect } from '@jest/globals' import { getAllLanguagesByISO } from '../src/' import { isoCountries } from '../src/countries' +import { ISOCode } from '../src/types' describe('getAllLanguagesByISO', () => { it('should return all languages associated with the given ISO codes', () => { const isoCodes = ['AD', 'AE', 'AF'] const result = getAllLanguagesByISO(isoCodes) const expectedLanguages = Array.from( - new Set(isoCodes.flatMap((isoCode) => isoCountries[isoCode].languages)) + new Set(isoCodes.flatMap((isoCode) => isoCountries[isoCode as ISOCode].languages)) ) expect(result).toEqual(expectedLanguages) diff --git a/tests/getAllLanguagesByISO.test.ts b/tests/getAllLanguagesByISO.test.ts index a4a6b39..997f7da 100644 --- a/tests/getAllLanguagesByISO.test.ts +++ b/tests/getAllLanguagesByISO.test.ts @@ -9,7 +9,9 @@ describe('getAllLanguageCodesByISO', () => { const expectedLanguageCodes = Array.from( new Set( isoCodes.flatMap((isoCode) => { - return isoCountries[isoCode].languages.map((language) => `${language}-${isoCode}`) + return isoCountries[isoCode].languages.map( + (language) => `${language}-${isoCode}` + ) }) ) ) @@ -23,7 +25,9 @@ describe('getAllLanguageCodesByISO', () => { const expectedLanguageCodes = Array.from( new Set( isoCodes.flatMap((isoCode) => { - return isoCountries[isoCode].languages.map((language) => `${language}_${isoCode}`) + return isoCountries[isoCode].languages.map( + (language) => `${language}_${isoCode}` + ) }) ) ) diff --git a/tests/getAsKey.test.ts b/tests/getAsKey.test.ts index 145425c..f378d16 100644 --- a/tests/getAsKey.test.ts +++ b/tests/getAsKey.test.ts @@ -2,98 +2,101 @@ import { describe, expect } from '@jest/globals' import { getAsKey } from '../src/' describe('getAsKey', () => { - describe('getAsKey', () => { - it('should return an object with languages as keys', () => { - const result = getAsKey('language') - const expectedKeys = Object.keys(result) - - expect(expectedKeys.length).toBeGreaterThan(10) - }) - - it('should return an object with names as keys', () => { - const result = getAsKey('name') - const expectedKeys = Object.keys(result) - - expect(expectedKeys).toEqual( - expect.arrayContaining(['Andorra', 'United Arab Emirates', 'Afghanistan']) - ) - }) - - it('should return an object with locale as keys', () => { - const result = getAsKey('locale') - const expectedKeys = Object.keys(result) - - expect(expectedKeys).toEqual( - expect.arrayContaining([ - 'ca_AD', - 'ar_AE', - 'ps_AF', - 'uz_AF', - 'tk_AF', - 'en_AG', - 'en_AI', - ]) - ) - }) - - it('should return an object with language-code as keys', () => { - const result = getAsKey('language-code') - const expectedKeys = Object.keys(result) - - expect(expectedKeys).toEqual( - expect.arrayContaining([ - 'ca-AD', - 'uz-AF', - 'tk-AF', - 'en-AG', - 'en-AI', - 'sq-AL', - 'hy-AM', - ]) - ) - }) - - it('should return an object with original data as keys', () => { - const result = getAsKey('iso') - const expectedKeys = Object.keys(result) - - expect(expectedKeys).toEqual( - expect.arrayContaining(['AD', 'AE', 'AF', 'AG', 'AI', 'AL', 'AM', 'AO', 'AQ', 'AR']) - ) - }) - - it('should return an object with original data as keys', () => { - const result = getAsKey('language-name') - const expectedKeys = Object.keys(result) - - expect(expectedKeys).toEqual( - expect.arrayContaining(["Catalan; Valencian", "Arabic", "Pashto, Pushto", "Uzbek", "Turkmen", "English", "Albanian", "Armenian", "Russian", "Portuguese"]) - ) - }) - - it('should return an object with original data as keys', () => { - const result = getAsKey('language-original') - const expectedKeys = Object.keys(result) - - expect(expectedKeys).toEqual( - expect.arrayContaining(["Català", "العربية", "پښتو", "Türkmen, Түркмен", "English", "Shqip", "Հայերեն", "русский язык", "Português"]) - ) - }) - - it('should return an object with original data as keys', () => { - const result = getAsKey('original') - const expectedKeys = Object.keys(result) - - expect(expectedKeys).toEqual( - expect.arrayContaining([ - 'Andorra', - 'دولة الإمارات العربية المتحدة', - 'افغانستان', - 'Antigua and Barbuda', - 'Anguilla', - 'Shqipëria', - ]) - ) - }) + it('should return an object with languages as keys', () => { + const result = getAsKey('language') + const expectedKeys = Object.keys(result) + + expect(expectedKeys.length).toBeGreaterThan(10) + }) + + it('should return an object with names as keys', () => { + const result = getAsKey('name') + const expectedKeys = Object.keys(result) + + expect(expectedKeys).toEqual( + expect.arrayContaining(['Andorra', 'United Arab Emirates', 'Afghanistan']) + ) + }) + + it('should return an object with locale as keys', () => { + const result = getAsKey('locale') + const expectedKeys = Object.keys(result) + + expect(expectedKeys).toEqual( + expect.arrayContaining(['ca_AD', 'ar_AE', 'ps_AF', 'uz_AF', 'tk_AF', 'en_AG', 'en_AI']) + ) + }) + + it('should return an object with language-code as keys', () => { + const result = getAsKey('language-code') + const expectedKeys = Object.keys(result) + + expect(expectedKeys).toEqual( + expect.arrayContaining(['ca-AD', 'uz-AF', 'tk-AF', 'en-AG', 'en-AI', 'sq-AL', 'hy-AM']) + ) + }) + + it('should return an object with original data as keys', () => { + const result = getAsKey('iso') + const expectedKeys = Object.keys(result) + + expect(expectedKeys).toEqual( + expect.arrayContaining(['AD', 'AE', 'AF', 'AG', 'AI', 'AL', 'AM', 'AO', 'AQ', 'AR']) + ) + }) + + it('should return an object with original data as keys', () => { + const result = getAsKey('language-name') + const expectedKeys = Object.keys(result) + + expect(expectedKeys).toEqual( + expect.arrayContaining([ + 'Catalan; Valencian', + 'Arabic', + 'Pashto, Pushto', + 'Uzbek', + 'Turkmen', + 'English', + 'Albanian', + 'Armenian', + 'Russian', + 'Portuguese', + ]) + ) + }) + + it('should return an object with original data as keys', () => { + const result = getAsKey('language-original') + const expectedKeys = Object.keys(result) + + expect(expectedKeys).toEqual( + expect.arrayContaining([ + 'Català', + 'العربية', + 'پښتو', + 'Türkmen, Түркмен', + 'English', + 'Shqip', + 'Հայերեն', + 'русский язык', + 'Português', + ]) + ) + }) + + it('should return an object with original data as keys', () => { + const result = getAsKey('original') + const expectedKeys = Object.keys(result) + + expect(expectedKeys).toEqual( + expect.arrayContaining([ + 'Andorra', + 'دولة الإمارات العربية المتحدة', + 'افغانستان', + 'Antigua and Barbuda', + 'Anguilla', + 'Shqipëria', + ]) + ) }) }) diff --git a/tests/getKeyValue .test.ts b/tests/getKeyValue .test.ts index 1237f5a..564263a 100644 --- a/tests/getKeyValue .test.ts +++ b/tests/getKeyValue .test.ts @@ -1,6 +1,7 @@ import { describe, expect, test } from '@jest/globals' import { getKeyValue } from '../src/' -import { IsoDataType } from '../src/type' +import { IsoDataType } from '../src/types' + describe('getKeyValue', () => { test.each([ diff --git a/tests/getLanguageData.test.ts b/tests/getLanguageData.test.ts index 68e3a81..5b1efb4 100644 --- a/tests/getLanguageData.test.ts +++ b/tests/getLanguageData.test.ts @@ -3,30 +3,30 @@ import { getLanguageData } from '../src/' describe('getLanguageData', () => { it('should return language data for valid ISO code', () => { - const languageCode = 'en'; - const expectedResult = { name: 'English', original: 'English' }; + const languageCode = 'en' + const expectedResult = { name: 'English', original: 'English' } - const result = getLanguageData(languageCode); - expect(result).toEqual(expectedResult); - }); + const result = getLanguageData(languageCode) + expect(result).toEqual(expectedResult) + }) it('should return language data for valid locale code', () => { - const languageCode = 'en-US'; - const expectedResult = { name: 'English', original: 'English' }; + const languageCode = 'en-US' + const expectedResult = { name: 'English', original: 'English' } - const result = getLanguageData(languageCode); - expect(result).toEqual(expectedResult); - }); + const result = getLanguageData(languageCode) + expect(result).toEqual(expectedResult) + }) it('should return false for invalid language code', () => { - const languageCode = 'invalidCode'; - const result = getLanguageData(languageCode); - expect(result).toBe(false); - }); + const languageCode = 'invalidCode' + const result = getLanguageData(languageCode) + expect(result).toBe(false) + }) it('should return false for empty string', () => { - const languageCode = ''; - const result = getLanguageData(languageCode); - expect(result).toBe(false); - }); -}); + const languageCode = '' + const result = getLanguageData(languageCode) + expect(result).toBe(false) + }) +}) diff --git a/tests/getLanguagesByISO.test.ts b/tests/getLanguagesByISO.test.ts index b970e0c..c781436 100644 --- a/tests/getLanguagesByISO.test.ts +++ b/tests/getLanguagesByISO.test.ts @@ -1,38 +1,38 @@ -import { describe, expect } from '@jest/globals'; -import { getLanguagesByISO } from '../src/'; +import { describe, expect } from '@jest/globals' +import { getLanguagesByISO } from '../src/' describe('getLanguagesByISO', () => { it('should return languages for valid ISO codes in the list', () => { - const isoCodes = ['en', 'fr']; + const isoCodes = ['en', 'fr'] const expectedResult = { en: { name: 'English', original: 'English' }, fr: { name: 'French', original: 'français' }, - }; + } - const result = getLanguagesByISO(isoCodes); - expect(result).toEqual(expectedResult); - }); + const result = getLanguagesByISO(isoCodes) + expect(result).toEqual(expectedResult) + }) it('should exclude invalid ISO codes from the result', () => { - const isoCodes = ['en', 'invalidCode', 'es']; + const isoCodes = ['en', 'invalidCode', 'es'] const expectedResult = { en: { name: 'English', original: 'English' }, es: { name: 'Spanish; Castilian', original: 'español, castellano' }, - }; + } - const result = getLanguagesByISO(isoCodes); - expect(result).toEqual(expectedResult); - }); + const result = getLanguagesByISO(isoCodes) + expect(result).toEqual(expectedResult) + }) it('should return an empty object for an empty ISO codes array', () => { - const isoCodes: string[] = []; - const result = getLanguagesByISO(isoCodes); - expect(result).toEqual({}); - }); + const isoCodes: string[] = [] + const result = getLanguagesByISO(isoCodes) + expect(result).toEqual({}) + }) it('should return an empty object for an array with only invalid ISO codes', () => { - const isoCodes = ['invalidCode1', 'invalidCode2']; - const result = getLanguagesByISO(isoCodes); - expect(result).toEqual({}); - }); -}); + const isoCodes = ['invalidCode1', 'invalidCode2'] + const result = getLanguagesByISO(isoCodes) + expect(result).toEqual({}) + }) +}) diff --git a/tests/iso.test.ts b/tests/iso.test.ts index 60e0d13..9998f57 100644 --- a/tests/iso.test.ts +++ b/tests/iso.test.ts @@ -15,7 +15,7 @@ describe('ISO', () => { const isoCode = 'AD' const result = isoTL(isoCode, 'locale') const expectedLanguages = isoCountries[isoCode].languages.map( - (language) => `${language}_${isoCode}`, + (language) => `${language}_${isoCode}` ) expect(result).toEqual(expectedLanguages) @@ -25,7 +25,7 @@ describe('ISO', () => { const isoCode = 'AD' const result = isoTL(isoCode, 'language-code') const expectedLanguages = isoCountries[isoCode].languages.map( - (language) => `${language}-${isoCode}`, + (language) => `${language}-${isoCode}` ) expect(result).toEqual(expectedLanguages) @@ -61,7 +61,7 @@ describe('ISO', () => { const isoCode = 'AD' const result = isoTL(isoCode, 'locale') const expectedLanguages = isoCountries[isoCode].languages.map( - (language) => `${language}_${isoCode}`, + (language) => `${language}_${isoCode}` ) expect(result).toEqual(expectedLanguages)