Skip to content

Commit

Permalink
Merge pull request #9 from amplifiedhq/fix-require-commonjs-error
Browse files Browse the repository at this point in the history
Fix require commonjs error
  • Loading branch information
hendurhance authored Apr 16, 2024
2 parents 8522101 + bc268ca commit 6272871
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 23 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ The `getStates()` method will return an array of states, which contains all the
- cities: The cities of the state.
```typescript
import { CountriesAtlas } from '@amplifiedhq/countries-atlas'
const states = CountriesAtlas.getStates('AD')
const states = await CountriesAtlas.getStates('AD')
// [
// {
// "name": "Andorra la Vella",
Expand All @@ -255,7 +255,7 @@ const states = CountriesAtlas.getStates('AD')
The `state()` method is used to get the state by the given `iso2` and `state_code` properties, it will return the state if it exists, otherwise it will return `undefined`. For example, if you want to get the state, you can do the following:
```typescript
import { CountriesAtlas } from '@amplifiedhq/countries-atlas'
const state = CountriesAtlas.state('AD', '07')
const state = await CountriesAtlas.state('AD', '07')
//{
// "name": "Andorra la Vella",
// "state_code": "07",
Expand Down Expand Up @@ -466,9 +466,9 @@ The `isValidStateCode()` method is used to validate the given `stateCode` proper
```typescript
import { ValidatorAtlas } from '@amplifiedhq/countries-atlas'

const isValid = ValidatorAtlas.isValidStateCode('AD', '07')
const isValid = await ValidatorAtlas.isValidStateCode('AD', '07')
// true
const isValid = ValidatorAtlas.isValidStateCode('AD', 'ABC')
const isValid = await ValidatorAtlas.isValidStateCode('AD', 'ABC')
// false
```

Expand Down
8 changes: 4 additions & 4 deletions src/__tests__/ValidatorAtlas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ describe('ValidatorAtlas', () => {
// expect(isValid).toBe(false);
// });

it('should return true if the state code is valid', () => {
expect(ValidatorAtlas.isValidStateCode('US', 'AZ')).toBe(true);
it('should return true if the state code is valid', async () => {
expect(await ValidatorAtlas.isValidStateCode('US', 'AZ')).toBe(true);
});

it('should return false if the state code is invalid', () => {
expect(ValidatorAtlas.isValidStateCode('US', 'XX')).toBe(false);
it('should return false if the state code is invalid', async () => {
expect(await ValidatorAtlas.isValidStateCode('US', 'XX')).toBe(false);
});
});

Expand Down
30 changes: 18 additions & 12 deletions src/helpers/CountriesAtlas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,33 +56,39 @@ export class CountriesAtlas {
* Retrieve all states of a country by its ISO2 code.
*
* @param {string} iso2 - ISO2 code of the country.
* @returns {Promise<State[]> | undefined} - Array of state objects or undefined if not found.
* @returns {Promise<State[] | undefined>} - Array of state objects or undefined if not found.
*/
getStates(iso2: string): Promise<State[] | undefined> | undefined {
async getStates(iso2: string): Promise<State[] | undefined> {
const country = this.find(iso2)
if (country) {
const stateData = import(`../data/countries/${country.iso2?.toLowerCase()}.json`)
return stateData.then((statesData: StateData) => {
return statesData.states
});
try {
const statesData = await import(`../data/countries/${country.iso2?.toLowerCase()}.json`);
return statesData.states as State[];
} catch (err) {
return undefined;
}
}
return undefined
return undefined;
}

/**
* Find a state by its state code and country ISO2 code.
*
* @param {string} iso2 - ISO2 code of the country.
* @param {string} stateCode - State code of the state to find.
* @returns {State | undefined} - State object or undefined if not found.
* @returns {Promise<State | undefined>} - State object or undefined if not found.
*/
state(iso2: string, stateCode: string): State | undefined {
async state(iso2: string, stateCode: string): Promise<State | undefined> {
const country = this.find(iso2);
if (country) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const statesData = require(`../data/countries/${country.iso2?.toLowerCase()}.json`) as StateData
const state = statesData.states.find((s: State) => s.state_code?.toUpperCase() === stateCode);
return state ? state : undefined;
try {
const statesData = await import(`../data/countries/${country.iso2?.toLowerCase()}.json`) as StateData;
const state = statesData.states.find((s: State) => s.state_code?.toUpperCase() === stateCode);
return state ? state : undefined;
} catch (err) {
return undefined;
}
}
return undefined;
}
Expand Down
6 changes: 3 additions & 3 deletions src/helpers/ValidatorAtlas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ class ValidatorAtlas {
*
* @param {string} iso2 - ISO2 code of the country.
* @param {string} stateCode - State code.
* @returns {boolean} - True if valid, false otherwise.
* @returns {Promise<boolean>} - True if valid, false otherwise.
*/
static isValidStateCode(iso2: string, stateCode: string): boolean {
return !!CountriesAtlas.state(iso2, stateCode);
static async isValidStateCode(iso2: string, stateCode: string): Promise<boolean> {
return !!(await CountriesAtlas.state(iso2, stateCode));
}
}

Expand Down

0 comments on commit 6272871

Please sign in to comment.