Skip to content

Commit

Permalink
Merge pull request #23 from Clifftech123/dev
Browse files Browse the repository at this point in the history
Refactor package.json and package-lock.json: Update countrydata.js to…
  • Loading branch information
Clifftech123 authored Sep 27, 2024
2 parents 6fa82a8 + 9126c8f commit e823fbf
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 26 deletions.
8 changes: 4 additions & 4 deletions Sample/TypeScript/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 7 additions & 12 deletions Sample/TypeScript/package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
{
"name": "typescript",
"name": "typescript-sample",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "ts-node index.ts "
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"devDependencies": {
"typescript": "^5.6.2"
"start": "ts-node index.ts"
},
"dependencies": {
"countrydata.js": "^1.0.2"
},
"devDependencies": {
"ts-node": "^10.9.2",
"typescript": "^5.6.2"
}
}
}
26 changes: 25 additions & 1 deletion Sample/javascript/app.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,56 @@
import { CountryHelper } from 'countrydata.js';
import { CountryHelper } from '.'; // Import the CountryHelper class

// Create an instance of CountryHelper
const countryHelper = new CountryHelper();

/**
* Fetches and logs all countries.
*/
const getCountries = async () => {
const allCountries = await countryHelper.getCountries();
console.log(JSON.stringify(allCountries, null, 2));
};

/**
* Fetches and logs a country by its short code.
* @param {string} shortCode - The short code of the country (e.g., "US").
*/
const getCountryByShortCode = async (shortCode) => {
const country = await countryHelper.getCountryByShortCode(shortCode);
console.log(country);
};

/**
* Fetches and logs the regions of a country by its short code.
* @param {string} shortCode - The short code of the country (e.g., "US").
*/
const getRegionsByCountryShortCode = async (shortCode) => {
const regions = await countryHelper.getRegionsByCountryShortCode(shortCode);
console.log(regions);
};

/**
* Fetches and logs the flag emoji of a country by its short code.
* @param {string} shortCode - The short code of the country (e.g., "US").
*/
const getCountryFlag = async (shortCode) => {
const country = await countryHelper.getCountryByShortCode(shortCode);
console.log(country?.countryFlag);
};

/**
* Fetches and logs a country by its phone code.
* @param {string} phoneCode - The phone code of the country (e.g., "1" for the US).
*/
const getCountryByPhoneCode = async (phoneCode) => {
const country = await countryHelper.getCountryByPhoneCode(phoneCode);
console.log(country);
};

/**
* Fetches and logs the phone code of a country by its short code.
* @param {string} shortCode - The short code of the country (e.g., "US").
*/
const getCountryPhoneCodeByShortCode = async (shortCode) => {
const phoneCode = await countryHelper.getCountryPhoneCodeByShortCode(shortCode);
console.log(phoneCode);
Expand Down
8 changes: 4 additions & 4 deletions Sample/javascript/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Sample/javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"dev": "node app.js"
},
"dependencies": {
"countrydata.js": "^1.0.2"
"countrydata.js": "^1.0.3"
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"src/data.json"
],
"scripts": {
"build": "tsup && shx cp src/data.json dist/",
"build": "tsup",
"format": "prettier --write .",
"check-format": "prettier --check .",
"release": "npm run build && changeset publish --access public",
Expand Down
12 changes: 10 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,16 @@ export class CountryHelper {
private loadingPromise: Promise<void> | null = null;
private timeoutId: NodeJS.Timeout | null = null;

// Use import.meta.url to get the current file URL and convert it to a path
private static readonly fileName = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'src', 'data.json');
// Determine the file path based on the module system
private static readonly fileName = (() => {
if (typeof __dirname !== 'undefined') {
// CommonJS
return path.join(__dirname, '..', 'src', 'data.json');
} else {
// ESM
return path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'src', 'data.json');
}
})();

/**
* Initializes the CountryHelper instance and starts loading the country data.
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

{
"compilerOptions": {
"esModuleInterop": true,
Expand All @@ -7,7 +8,7 @@
"resolveJsonModule": true,
"moduleDetection": "force",
"isolatedModules": true,
"verbatimModuleSyntax": false,
"verbatimModuleSyntax": false,
"strict": true,
"noUncheckedIndexedAccess": true,
"noImplicitOverride": true,
Expand Down

0 comments on commit e823fbf

Please sign in to comment.