faker icon indicating copy to clipboard operation
faker copied to clipboard

refactor(locale): group name entries by gender

Open xDivisionByZerox opened this issue 1 year ago • 4 comments

Description

Groups locale name entries in the PersonModule by gender.

Additional Info

Fixes #1677.

  • #1677
Script that was used to refactor locale files automatically

ℹ️ Note The file being .ts is important, so you are able to directly import() other .ts files.

import { execSync } from 'node:child_process';
import { readdir, stat, unlink, writeFile } from 'node:fs/promises';
import { join, resolve } from 'node:path';

const localeRootDir = resolve('./src/locales');
const localeDirNames = await readdir(localeRootDir);

async function pathExist(path: string) {
  return stat(path)
    .then(() => true)
    .catch(() => false);
}

for (const localeDirName of localeDirNames) {
  const personPath = join(localeRootDir, localeDirName, 'person');
  if (!(await pathExist(personPath))) {
    continue;
  }

  const getEntryPath = (entryName: string) =>
    join(personPath, `${entryName}.ts`);

  const entryGroups = [
    ['prefix', 'female_prefix', 'male_prefix'],
    ['first_name', 'female_first_name', 'male_first_name'],
    ['middle_name', 'female_middle_name', 'male_middle_name'],
    ['last_name', 'female_last_name', 'male_last_name'],
    ['last_name_pattern', 'female_last_name_pattern', 'male_last_name_pattern'],
  ];
  for (const entryGroup of entryGroups) {
    const [mainEntry, femaleEntry, maleEntry] = entryGroup;
    const mainEntryPath = getEntryPath(mainEntry);
    const femaleEntryPath = getEntryPath(femaleEntry);
    const maleEntryPath = getEntryPath(maleEntry);

    const finalMainEntry: Record<string, unknown> = {};

    const keyPathMapList = [
      { key: 'generic', path: mainEntryPath },
      { key: 'female', path: femaleEntryPath },
      { key: 'male', path: maleEntryPath },
    ];
    for (const { key, path } of keyPathMapList) {
      if (!(await pathExist(path))) {
        continue;
      }

      const { default: defaultExportMember } = await import(`file://${path}`);
      if (!defaultExportMember) {
        continue;
      }

      finalMainEntry[key] = defaultExportMember;
    }

    // remove in second cycle in case files import each other
    for (const { path } of keyPathMapList) {
      await unlink(path).catch((): void => undefined);
    }

    if (Object.keys(finalMainEntry).length === 0) {
      continue;
    }

    await writeFile(
      mainEntryPath,
      `export default ${JSON.stringify(finalMainEntry)}`
    );
    execSync(`npx prettier --write ${mainEntryPath}`);
  }
}

[!NOTE] The script did not account for non-applicable values. These have been manually justified in 4b86dcb60e5a7079dfea16b233861d259dd8206e. A list of affected files can be found here.

xDivisionByZerox avatar Jun 08 '24 23:06 xDivisionByZerox

Deploy Preview for fakerjs ready!

Name Link
Latest commit 3e2bb1b41b2b97f337b0fa18da5e672639f884e6
Latest deploy log https://app.netlify.com/sites/fakerjs/deploys/667f1694a52312000882d4fe
Deploy Preview https://deploy-preview-2938.fakerjs.dev
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

netlify[bot] avatar Jun 08 '24 23:06 netlify[bot]

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 99.95%. Comparing base (92a2f17) to head (3e2bb1b).

Additional details and impacted files
@@            Coverage Diff             @@
##             next    #2938      +/-   ##
==========================================
- Coverage   99.95%   99.95%   -0.01%     
==========================================
  Files        2984     2748     -236     
  Lines      216057   239091   +23034     
  Branches      597      586      -11     
==========================================
+ Hits       215969   238983   +23014     
- Misses         88      108      +20     
Files Coverage Δ
src/index.ts 100.00% <100.00%> (ø)
src/locales/af_ZA/person/first_name.ts 100.00% <100.00%> (ø)
src/locales/af_ZA/person/index.ts 100.00% <ø> (ø)
src/locales/af_ZA/person/last_name.ts 100.00% <100.00%> (ø)
src/locales/af_ZA/person/last_name_pattern.ts 100.00% <100.00%> (ø)
src/locales/ar/location/street_pattern.ts 100.00% <100.00%> (ø)
src/locales/ar/person/first_name.ts 100.00% <100.00%> (ø)
src/locales/ar/person/index.ts 100.00% <ø> (ø)
src/locales/ar/person/last_name.ts 100.00% <100.00%> (ø)
src/locales/ar/person/last_name_pattern.ts 100.00% <100.00%> (ø)
... and 174 more

... and 264 files with indirect coverage changes

codecov[bot] avatar Jun 09 '24 10:06 codecov[bot]

Oh I'm sorry, looks like I caused a merged conflict 🙇‍♂️

ST-DDT avatar Jun 26 '24 08:06 ST-DDT

Oh I'm sorry, looks like I caused a merged conflict 🙇‍♂️

All good, happens.I resolved them to the best of my abilities.

xDivisionByZerox avatar Jun 27 '24 10:06 xDivisionByZerox