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

feat: use localized fake pattern in street #966

Merged
merged 8 commits into from
May 21, 2022

Conversation

ST-DDT
Copy link
Member

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

This PR changes the address.street method to use the address.street locale data with fake to generate street names.

For that I checked, that all street files are named correctly (street -> patterns; street _name -> actual street names).
I did not check whether the specified patterns make sense or the street names are real.

I generated the missing street files with the original pattern from the method to ensure it does not break between versions.
I did not add '{{address.street_name}}' to the street patterns if both the street and street_name files exist .
I did not remove it from the patterns if the street_name file is missing in that locale.

Everything in the first commit was generated by using this code:

generateLocales.ts
const processed = new Set<string>();

/**
 * Use this hook method to selectively update locale data files (not for index.ts files).
 * This method is intended to be temporarily overwritten for one-time updates.
 *
 * @param filePath The full file path to the file.
 * @param locale The locale for that file.
 * @param localePath The locale path parts (after the locale).
 */
function updateLocaleFileHook(
  filePath: string,
  locale: string,
  localePath: string[]
): void {
  if (processed.has(locale)) {
    return;
  }
  if (localePath[0] !== 'address') {
    return;
  }
  processed.add(locale);

  let streetPattern: string[];
  let streetNames: string[];

  const streetFiles = [
    'street',
    'streets',
    'street_name',
    'street_root',
    'street_title',
  ];
  for (const name of streetFiles) {
    if (existsSync(`src/locales/${locale}/address/${name}.ts`)) {
      let content: string[] =
        require(`../src/locales/${locale}/address/${name}`).default;
      unlinkSync(resolve(`src/locales/${locale}/address/${name}.ts`));
      if (content[0].includes('{{')) {
        for (const replaced of streetFiles) {
          content = content.map((e) =>
            e.replace(
              new RegExp(`{{address.${replaced}}}`, 'g'),
              '{{address.street_name}}'
            )
          );
        }
        streetPattern = content;
      } else {
        streetNames = content;
      }
    }
  }

  if (streetPattern == null) {
    if (streetNames == null) {
      streetPattern = [
        '{{name.firstName}} {{address.streetSuffix}}',
        '{{name.lastName}} {{address.streetSuffix}}',
      ];
    } else {
      streetPattern = [
        '{{name.firstName}} {{address.streetSuffix}}',
        '{{name.lastName}} {{address.streetSuffix}}',
        '{{address.street_name}}',
      ];
    }
  }

  let streetPatternContent = `export default ${JSON.stringify(
    streetPattern,
    undefined,
    '  '
  )};\n`;
  streetPatternContent = formatTypescript(streetPatternContent);
  writeFileSync(
    resolve(`src/locales/${locale}/address/street.ts`),
    streetPatternContent
  );

  if (streetNames != null) {
    let streetNamesContent = `export default ${JSON.stringify(
      streetNames,
      undefined,
      '  '
    )};\n`;
    streetNamesContent = formatTypescript(streetNamesContent);

    writeFileSync(
      resolve(`src/locales/${locale}/address/street_name.ts`),
      streetNamesContent
    );
  }

  if (existsSync(`src/locales/${locale}/address/street_address.ts`)) {
    const content: AddressDefinitions['street_address'] =
      require(`../src/locales/${locale}/address/street_address`).default;
    content.normal = content.normal
      .replace(/{{address.streetName}}/g, '{{address.street}}')
      .replace(/{{address.street_name}}/g, '{{address.street}}');
    content.full = content.full
      .replace(/{{address.streetName}}/g, '{{address.street}}')
      .replace(/{{address.street_name}}/g, '{{address.street}}');
    let streetAddressContent = `export default ${JSON.stringify(
      content,
      undefined,
      '  '
    )};\n`;
    streetAddressContent = formatTypescript(streetAddressContent);

    writeFileSync(
      resolve(`src/locales/${locale}/address/street_address.ts`),
      streetAddressContent
    );
  }
}

And executed it using this helper:

untilSuccess() {
        until "$@"; do :; done
}

untilSuccess pnpm run generate:locales
pnpm run generate:locales

@ST-DDT ST-DDT added c: feature Request for new feature p: 1-normal Nothing urgent c: locale Permutes locale definitions labels May 17, 2022
@ST-DDT ST-DDT added this to the v7 - Current Major milestone May 17, 2022
@ST-DDT ST-DDT requested review from a team May 17, 2022 18:12
@ST-DDT ST-DDT self-assigned this May 17, 2022
@codecov
Copy link

codecov bot commented May 17, 2022

Codecov Report

❗ No coverage uploaded for pull request base (main@05f555b). Click here to learn what that means.
The diff coverage is 99.74%.

@@           Coverage Diff           @@
##             main     #966   +/-   ##
=======================================
  Coverage        ?   99.65%           
=======================================
  Files           ?     1988           
  Lines           ?   210187           
  Branches        ?      888           
=======================================
  Hits            ?   209468           
  Misses          ?      700           
  Partials        ?       19           
Impacted Files Coverage Δ
src/definitions/address.ts 0.00% <0.00%> (ø)
src/locales/ar/address/street.ts 100.00% <ø> (ø)
src/locales/cz/address/street.ts 100.00% <ø> (ø)
src/locales/cz/address/street_name.ts 100.00% <ø> (ø)
src/locales/en/address/street.ts 100.00% <ø> (ø)
src/locales/en_GH/address/street.ts 100.00% <ø> (ø)
src/locales/es/address/street.ts 100.00% <ø> (ø)
src/locales/fa/address/street.ts 100.00% <ø> (ø)
src/locales/fr/address/street.ts 100.00% <ø> (ø)
src/locales/fr_BE/address/street.ts 100.00% <ø> (ø)
... and 147 more

Shinigami92
Shinigami92 previously approved these changes May 17, 2022
Copy link
Member

@Shinigami92 Shinigami92 left a comment

Choose a reason for hiding this comment

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

Trust-bonus-approval 😜

@ST-DDT ST-DDT requested a review from a team May 17, 2022 20:17
@ST-DDT ST-DDT linked an issue May 17, 2022 that may be closed by this pull request
import-brain
import-brain previously approved these changes May 17, 2022
@ST-DDT ST-DDT dismissed stale reviews from import-brain and Shinigami92 via d7439bf May 18, 2022 22:55
@ST-DDT ST-DDT requested a review from a team May 18, 2022 22:59
@ST-DDT ST-DDT requested a review from a team May 20, 2022 06:20
@ST-DDT ST-DDT requested a review from a team May 20, 2022 22:42
@Shinigami92 Shinigami92 enabled auto-merge (squash) May 21, 2022 14:15
@Shinigami92 Shinigami92 merged commit 5af79f4 into main May 21, 2022
@ST-DDT ST-DDT deleted the feat/address/street-use-fake-pattern branch May 21, 2022 17:55
@xDivisionByZerox xDivisionByZerox added the m: location Something is referring to the location module label Aug 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: feature Request for new feature c: locale Permutes locale definitions m: location Something is referring to the location module p: 1-normal Nothing urgent
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Address doesn't use street_name / street_root from locale
4 participants