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

Add main file to import into JS #13

Merged
merged 7 commits into from Jul 2, 2023
Merged

Conversation

NJRBailey
Copy link
Contributor

@NJRBailey NJRBailey commented Jun 30, 2023

Pull Request following discussion at #12

I was wrong in my initial issue - I couldn't get it to work in the browser as easily (I actually couldn't find a way of getting web browsers to open the JSON directly either, so it may not be possible to use the library in browser even in its current state).

This PR adds methods to synchronously load the JSON. There is a way of doing it asynchronously, but it is still an experimental part of the JS spec, so I did not include it.

Using the code

import geoJson from "world-geojson";

geoJson.forCountry("Antigua & Barbuda"); // Converts to "antigua_and_barbuda"
geoJson.forArea("U.S.A.", "U.S. Virgin Islands"); // Converts to "usa", "us_virgin_islands"
geoJson.forState("Australia", "New South Wales"); // Converts to "australia", "new_south_wales"
geoJson.forCountry("abcd"); // returns `null` when data not found

Filename conversion

The name conversion is very simple - users can generally specify the English names of places and the conversion will successfully match the filename:

name
    .replace(/ /g, '_') // Replace spaces with underscores
    .replace(/\./g, '') // Remove dots
    .replace(/&/g, 'and') // Replace & with and
    .toLowerCase() // Make lowercase

They can also specify the filename directly and it would still work:

geoJson.forCountry("antigua_and_barbuda");

I can make this more helpful if you want (for example, adding support for strings like 'United States of America' or 'Democratic Republic of the Congo').

Tests

I've manually tested a range of countries, states and areas.

I have not added any automated tests - I am happy to add some but it would require me to add a test library dependency.

@NJRBailey NJRBailey marked this pull request as ready for review July 2, 2023 16:07
Copy link
Owner

@georgique georgique left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thank you

@georgique georgique merged commit 73cb6c8 into georgique:develop Jul 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants