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

chore(locales): convert locales data to faker.fake format #927

Merged
merged 4 commits into from
May 7, 2022

Conversation

ST-DDT
Copy link
Member

@ST-DDT ST-DDT commented May 5, 2022

Depends on: #884
Fixes: #725
Motivation: #691 (comment)

Converts the (currently) useless patterns to useable faker patterns.
(The patterns never worked before, AFAICT not even when they were added to the repo.)

Upgrade code (generate:locales)
function updateLocaleFileHook(
  filePath: string,
  locale: string,
  localePath: string[]
): void {
  const origContent: string[] =
    // eslint-disable-next-line @typescript-eslint/no-var-requires
    require(`../src/locales/${locale}/${localePath.join('/')}`)
      .default as string[];
  let modified = false;

  if (
    Array.isArray(origContent) &&
    origContent.length !== 0 &&
    typeof origContent[0] === 'string'
  ) {
    const content = origContent.map((entry) =>
      entry.replace(
        /#{([^\. }]*?)\.?([a-zA-Z][^\. }]+)}/g,
        (all, module: string, file: string) => {
          module ||= localePath[0];
          module = module.substring(0, 1).toLowerCase() + module.substring(1);
          modified = true;
          file = file === 'man_last_name' ? 'male_last_name' : file;
          return `{{${module}.${file}}}`;
        }
      )
    );

    if (!modified) {
      return;
    }

    // Test conversion
    faker.locale = locale;
    content.map((entry) => faker.fake(entry));

    let outContent = `export default ${JSON.stringify(
      content,
      undefined,
      '  '
    )}`;
    outContent = formatTypescript(outContent);
    writeFileSync(filePath, outContent);
  }
}

The first commit is entirely generated by that script.
The second one is from manually searching for #{.
All changed files have been checked by faker.fake(entry) with faker.locale = locale (fallback en).

@ST-DDT ST-DDT added s: on hold Blocked by something or frozen to avoid conflicts p: 1-normal Nothing urgent s: accepted Accepted feature / Confirmed bug c: locale Permutes locale definitions labels May 5, 2022
@ST-DDT ST-DDT requested review from a team May 5, 2022 11:56
@ST-DDT ST-DDT self-assigned this May 5, 2022
@codecov
Copy link

codecov bot commented May 5, 2022

Codecov Report

Merging #927 (3b2331c) into main (f2c3a39) will increase coverage by 0.00%.
The diff coverage is 100.00%.

❗ Current head 3b2331c differs from pull request most recent head 4ade607. Consider uploading reports for the commit 4ade607 to get more accurate results

@@           Coverage Diff           @@
##             main     #927   +/-   ##
=======================================
  Coverage   99.66%   99.66%           
=======================================
  Files        1957     1957           
  Lines      209797   209831   +34     
  Branches      885      890    +5     
=======================================
+ Hits       209084   209118   +34     
  Misses        693      693           
  Partials       20       20           
Impacted Files Coverage Δ
src/locales/ar/address/city.ts 100.00% <100.00%> (ø)
src/locales/ar/address/street_name.ts 100.00% <100.00%> (ø)
src/locales/ar/name/name.ts 100.00% <100.00%> (ø)
src/locales/ar/team/name.ts 100.00% <100.00%> (ø)
src/locales/az/address/city.ts 100.00% <100.00%> (ø)
src/locales/az/address/street_name.ts 100.00% <100.00%> (ø)
src/locales/az/company/name.ts 100.00% <100.00%> (ø)
src/locales/az/name/name.ts 100.00% <100.00%> (ø)
src/locales/cz/address/city.ts 100.00% <100.00%> (ø)
src/locales/cz/address/street_name.ts 100.00% <100.00%> (ø)
... and 152 more

Base automatically changed from feature/fake-with-locale-definitions to main May 7, 2022 08:25
@ST-DDT ST-DDT removed the s: on hold Blocked by something or frozen to avoid conflicts label May 7, 2022
@ST-DDT ST-DDT enabled auto-merge (squash) May 7, 2022 08:29
@ST-DDT ST-DDT merged commit 723a53c into main May 7, 2022
@ST-DDT ST-DDT deleted the fix/fake-locale-data branch May 7, 2022 08:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: locale Permutes locale definitions p: 1-normal Nothing urgent s: accepted Accepted feature / Confirmed bug
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Check use of the #{pattern}s in the locales
4 participants