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

Refactor internal data format #11

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

eemeli
Copy link

@eemeli eemeli commented Jan 9, 2019

Based on #10, this goes on to refactor the internal data format and separates the data into data.json and the output functions into index.js. All external interfaces are kept unmodified.

The previous data format was:

[[name, iso2, dialCode, format, priority, areaCodes]]

And the updated data format is:

{ [dialCode]: [{ iso2, name, format, areaCodes }] }

In terms of file size, using an object with string keys rather than an array actually has almost no effect on gzipped output size, as the common strings get compacted. Legibility is significantly increased, and some of the previous possibilities for errors (namely, missing priority order) are eliminated.

As an additional feature, the data itself is now exposed separately, which allows its use even outside JS environments.

@eemeli
Copy link
Author

eemeli commented Jan 9, 2019

In case it's of interest, here's the mapping I used to generate data.json from the previous allCountries data:

const data = allCountries.reduce(
  (data, [name, iso2, dial, format, prio, areas]) => {
    const row = { iso2, name };
    if (format) row.format = format;
    if (areas) row.areaCodes = areas;
    if (!data[dial]) data[dial] = [];
    if (typeof prio == 'number') data[dial][prio] = row;
    else data[dial].push(row);
    return data;
  },
  {}
);

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.

1 participant