Skip to content

Commit

Permalink
support only ESM module
Browse files Browse the repository at this point in the history
  • Loading branch information
canbax committed May 11, 2024
1 parent 0a0e74b commit a4d5cd6
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 119 deletions.
40 changes: 0 additions & 40 deletions docs/index.html

This file was deleted.

6 changes: 0 additions & 6 deletions docs/main.js

This file was deleted.

4 changes: 0 additions & 4 deletions docs/style.css

This file was deleted.

16 changes: 0 additions & 16 deletions docs/your-library.umd.js

This file was deleted.

23 changes: 2 additions & 21 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,10 @@
import commonjs from "@rollup/plugin-commonjs";
import typescript from "@rollup/plugin-typescript";
import terser from "@rollup/plugin-terser";

const isProduction = process.env.NODE_ENV === "production";

export default [
// browser-friendly UMD build
{
input: "src/main.ts",
output: {
name: "irem",
file: "dist/irem.umd.js",
format: "umd",
},
plugins: [
commonjs(), // so Rollup can convert `ms` to an ES module
typescript({ module: "esnext", tsconfig: "./tsconfig.json" }), // so Rollup can convert TypeScript to JavaScript
isProduction && terser(),
],
},

// CommonJS (for Node) and ES module (for bundlers) build.
// ES module (for bundlers) build.
// (We could have three entries in the configuration array
// instead of two, but it's quicker to generate multiple
// builds from a single configuration where possible, using
Expand All @@ -33,9 +17,6 @@ export default [
typescript({ module: "esnext", tsconfig: "./tsconfig.json" }), // so Rollup can convert TypeScript to JavaScript
isProduction && terser(),
],
output: [
{ file: "dist/irem.cjs.js", format: "cjs" },
{ file: "dist/irem.esm.js", format: "es" },
],
output: [{ file: "dist/irem.esm.js", format: "es" }],
},
];
45 changes: 22 additions & 23 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ import {
import { readFileSync } from "fs";
import { isSupportedNonEnglishLanguage } from "./util";

export async function getAllCountries(
language?: SupportedLanguage,
): Promise<SimpleCountry[]> {
export function getAllCountries(language?: SupportedLanguage): SimpleCountry[] {
const t1 = new Date().getTime();
const data = await readJSON(language);
const data = readJSON(language);
const t2 = new Date().getTime();
console.log((t2 - t1) / 1000, " seconds passed ");

Expand All @@ -28,11 +26,11 @@ export async function getAllCountries(
return countries;
}

export async function getAllRegionsOfCountry(
export function getAllRegionsOfCountry(
countryCode: CountryCode,
language?: SupportedLanguage,
): Promise<SimplePlace[]> {
const data = await readJSON(language);
): SimplePlace[] {
const data = readJSON(language);
const regions: SimplePlace[] = [];
for (const regionNameInEnglish in data[countryCode][">"]) {
regions.push({
Expand All @@ -43,27 +41,28 @@ export async function getAllRegionsOfCountry(
return regions;
}

// export async function getAllCities(
// countryCode: CountryCode,
// language?: SupportedLanguage,
// ): SimplePlace[] {
// const data = await readJSON(language);
export function getAllCities(
countryCode: CountryCode,
regionNameInEnglish: string,
language?: SupportedLanguage,
): SimplePlace[] {
const data = readJSON(language);

// const regions: SimplePlace[] = [];
// for (const regionNameInEnglish in data[countryCode][">"]) {
// regions.push({
// englishName: regionNameInEnglish,
// name: data[countryCode][">"][regionNameInEnglish].t,
// });
// }
// return regions;
// }
const cities: SimplePlace[] = [];
for (const cityName in data[countryCode][">"][regionNameInEnglish][">"]) {
cities.push({
englishName: cityName,
name: data[countryCode][">"][regionNameInEnglish][">"][cityName].t,
});
}
return cities;
}

async function readJSON(
function readJSON(
language?: SupportedLanguage,
nonEnglishDataPath: string = "./data/",
englishDataPath: string = "./data/",
): Promise<Record<CountryCode, CountryData>> {
): Record<CountryCode, CountryData> {
const fileToRead = isSupportedNonEnglishLanguage(language)
? `${nonEnglishDataPath}GPS-data-${language}.json`
: `${englishDataPath}GPS-data.json`;
Expand Down
30 changes: 21 additions & 9 deletions test/main.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import { getAllCountries, getAllRegionsOfCountry } from "../src/main";
import {
getAllCities,
getAllCountries,
getAllRegionsOfCountry,
} from "../src/main";
import { describe, test, expect } from "vitest";
import { supportedNonEnglishLanguages } from "../src/util";

describe("allCountriesInEnglish", () => {
test.each([...supportedNonEnglishLanguages, "en", "EN", "undefined"])(
"should get all countries in Language %s",
async (language) => {
const allCountries = await getAllCountries(language);
(language) => {
const allCountries = getAllCountries(language);
expect(allCountries).toHaveLength(242);
},
);

test("should get Turkey from all countries in Turkish", async () => {
const allCountriesInTurkish = await getAllCountries("tr");
test("should get Turkey from all countries in Turkish", () => {
const allCountriesInTurkish = getAllCountries("tr");
const turkey = allCountriesInTurkish.find((x) => x.code == "TR");
expect(turkey).toEqual({
code: "TR",
Expand All @@ -23,13 +27,13 @@ describe("allCountriesInEnglish", () => {
});

describe("getAllRegionsOfCountry", () => {
test("should get 81 regions (cities) in Turkey", async () => {
const allRegionsInTurkey = await getAllRegionsOfCountry("TR", "tr");
test("should get 81 regions (cities) in Turkey", () => {
const allRegionsInTurkey = getAllRegionsOfCountry("TR", "tr");
expect(allRegionsInTurkey.length).toBe(81);
});

test("should get Kocaeli and İstanbul cities in Turkey", async () => {
const allRegionsInTurkey = await getAllRegionsOfCountry("TR", "tr");
test("should get Kocaeli and İstanbul cities in Turkey", () => {
const allRegionsInTurkey = getAllRegionsOfCountry("TR", "tr");

const istanbul = allRegionsInTurkey.find(
(x) => x.englishName === "İstanbul",
Expand All @@ -39,3 +43,11 @@ describe("getAllRegionsOfCountry", () => {
expect(kocaeli).toEqual({ englishName: "Kocaeli", name: "" });
});
});

describe("getAllCities", () => {
test("should get cities (districts) in Ankara in Turkey", () => {
const allPlacesInAnkara = getAllCities("TR", "Ankara", "tr");
console.log("allPlacesInAnkara: ", allPlacesInAnkara);
expect(allPlacesInAnkara.length).toBe(26);
});
});

0 comments on commit a4d5cd6

Please sign in to comment.