Skip to content

Commit

Permalink
Merge branch 'next' into infra/unicorn/prefer-array-some
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-DDT committed Oct 20, 2023
2 parents a5e041c + a882244 commit 2fe856a
Show file tree
Hide file tree
Showing 19 changed files with 205 additions and 111 deletions.
4 changes: 1 addition & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,12 @@ module.exports = defineConfig({
// Each rule should be checked whether it should be enabled/configured and the problems fixed, or stay disabled permanently.
'unicorn/better-regex': 'off',
'unicorn/consistent-function-scoping': 'off',
'unicorn/escape-case': 'off',
'unicorn/filename-case': 'off',
'unicorn/import-style': 'off',
'unicorn/no-array-callback-reference': 'off',
'unicorn/no-array-reduce': 'off',
'unicorn/no-await-expression-member': 'off',
'unicorn/no-for-loop': 'off',
'unicorn/no-instanceof-array': 'off',
'unicorn/no-negated-condition': 'off',
'unicorn/no-object-as-default-parameter': 'off',
'unicorn/no-useless-switch-case': 'off',
Expand All @@ -69,7 +67,6 @@ module.exports = defineConfig({
'unicorn/prefer-negative-index': 'off',
'unicorn/prefer-number-properties': 'off',
'unicorn/prefer-optional-catch-binding': 'off',
'unicorn/prefer-spread': 'off',
'unicorn/prefer-string-slice': 'off',
'unicorn/prefer-ternary': 'off',
'unicorn/prefer-top-level-await': 'off',
Expand Down Expand Up @@ -112,6 +109,7 @@ module.exports = defineConfig({
'error',
{ blankLine: 'always', prev: 'block-like', next: '*' },
],
'@typescript-eslint/prefer-regexp-exec': 'error',
'@typescript-eslint/restrict-template-expressions': [
'error',
{ allowNumber: true, allowBoolean: true },
Expand Down
124 changes: 113 additions & 11 deletions scripts/generateLocales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ async function generateRecursiveModuleIndexes(
submodules = removeIndexTs(submodules);
for (const submodule of submodules) {
const pathModule = resolve(path, submodule);
updateLocaleFile(pathModule);
await updateLocaleFile(pathModule);
// Only process sub folders recursively
if (lstatSync(pathModule).isDirectory()) {
let moduleDefinition =
Expand Down Expand Up @@ -238,14 +238,12 @@ async function generateRecursiveModuleIndexes(
*
* @param filePath The full file path to the file.
*/
function updateLocaleFile(filePath: string): void {
async function updateLocaleFile(filePath: string): Promise<void> {
if (lstatSync(filePath).isFile()) {
const pathParts = filePath
const [locale, moduleKey, entryKey] = filePath
.substring(pathLocales.length + 1, filePath.length - 3)
.split(/[\\/]/);
const locale = pathParts[0];
pathParts.splice(0, 1);
updateLocaleFileHook(filePath, locale, pathParts);
await updateLocaleFileHook(filePath, locale, moduleKey, entryKey);
}
}

Expand All @@ -255,16 +253,120 @@ function updateLocaleFile(filePath: string): void {
*
* @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).
* @param definitionKey The definition key of the current file (ex. 'location').
* @param entryName The entry key of the current file (ex. 'state'). Is `undefined` if `definitionKey` is `'metadata'`.
*/
function updateLocaleFileHook(
async function updateLocaleFileHook(
filePath: string,
locale: string,
localePath: string[]
): void {
definitionKey: string,
entryName: string | undefined
): Promise<void> {
// this needs to stay so all arguments are "used"
if (filePath === 'never') {
console.log(`${filePath} <-> ${locale} @ ${localePath.join(' -> ')}`);
console.log(`${filePath} <-> ${locale} @ ${definitionKey} -> ${entryName}`);
}

await normalizeLocaleFile(filePath, definitionKey);
}

/**
* Normalizes the data of a locale file based on a set of rules.
* Those include:
* - filter the entry list for duplicates
* - limiting the maximum entries of a file to 1000
* - sorting the entries alphabetically
*
* This function mutates the file by reading and writing to it!
*
* @param filePath The full file path to the file.
* @param definitionKey The definition key of the current file (ex. 'location').
*/
async function normalizeLocaleFile(filePath: string, definitionKey: string) {
function normalizeDataRecursive<T>(localeData: T): T {
if (typeof localeData !== 'object' || localeData === null) {
// we can only traverse object-like structs
return localeData;
}

if (Array.isArray(localeData)) {
return (
[...new Set(localeData)]
// limit entries to 1k
.slice(0, 1000)
// sort entries alphabetically
.sort() as T
);
}

const result = {} as T;
for (const key of Object.keys(localeData)) {
result[key] = normalizeDataRecursive(localeData[key]);
}

return result;
}

const legacyDefinitions = ['app', 'cell_phone', 'team'];
const definitionsToSkip = [
'airline',
'animal',
'color',
'commerce',
'company',
'database',
'date',
'finance',
'hacker',
'internet',
'location',
'lorem',
'metadata',
'music',
'person',
'phone_number',
'science',
'system',
'vehicle',
'word',
...legacyDefinitions,
];
if (definitionsToSkip.includes(definitionKey)) {
return;
}

console.log(`Running data normalization for:`, filePath);

const fileContent = readFileSync(filePath).toString();
const searchString = 'export default ';
const compareIndex = fileContent.indexOf(searchString) + searchString.length;
const compareString = fileContent.substring(compareIndex);

const isDynamicFile = compareString.startsWith('mergeArrays');
const isNonApplicable = compareString.startsWith('null');
const isFrozenData = compareString.startsWith('Object.freeze');
if (isDynamicFile || isNonApplicable || isFrozenData) {
return;
}

const validEntryListStartCharacters = ['[', '{'];
const staticFileOpenSyntax = validEntryListStartCharacters.find(
(validStart) => compareString.startsWith(validStart)
);
if (staticFileOpenSyntax === undefined) {
console.log('Found an unhandled dynamic file:', filePath);
return;
}

const fileContentPreData = fileContent.substring(0, compareIndex);
// eslint-disable-next-line @typescript-eslint/no-var-requires
const localeData = normalizeDataRecursive(require(filePath).default);

// We reattach the content before the actual data implementation to keep stuff like comments.
// In the long term we should probably define a whether we want those in the files at all.
const newContent = fileContentPreData + JSON.stringify(localeData);

writeFileSync(filePath, await format(newContent, prettierTsOptions));
}

// Start of actual logic
Expand Down
2 changes: 1 addition & 1 deletion src/internal/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
* @param args The arrays to merge.
*/
export function mergeArrays<T>(...args: T[][]): T[] {
return Array.from(new Set(args.flat())).sort();
return [...new Set(args.flat())].sort();
}
2 changes: 1 addition & 1 deletion src/modules/color/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function toBinary(values: number[]): string {
const buffer = new ArrayBuffer(4);
new DataView(buffer).setFloat32(0, value);
const bytes = new Uint8Array(buffer);
return toBinary(Array.from(bytes)).split(' ').join('');
return toBinary([...bytes]).replace(/ /g, '');
}

return (value >>> 0).toString(2).padStart(8, '0');
Expand Down
2 changes: 1 addition & 1 deletion src/modules/company/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class CompanyModule {
});
// Don't want the source array exposed to modification, so return a copy
// eslint-disable-next-line deprecation/deprecation
return this.faker.definitions.company.suffix.slice(0);
return [...this.faker.definitions.company.suffix];
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/modules/finance/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -722,9 +722,9 @@ export class FinanceModule {
let address = this.faker.helpers.arrayElement(['L', 'M', '3']);

for (let i = 0; i < addressLength - 1; i++)
address += this.faker.helpers.arrayElement(
'123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'.split('')
);
address += this.faker.helpers.arrayElement([
...'123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ',
]);

return address;
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/git/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Faker } from '../..';
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
import { deprecated } from '../../internal/deprecated';

const nbsp = '\u00a0';
const nbsp = '\u00A0';

/**
* Module to generate git related entries.
Expand Down
30 changes: 15 additions & 15 deletions src/modules/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export class SimpleHelpersModule {
slugify(string: string = ''): string {
return string
.normalize('NFKD') //for example è decomposes to as e + ̀
.replace(/[\u0300-\u036f]/g, '') // removes combining marks
.replace(/[\u0300-\u036F]/g, '') // removes combining marks
.replace(/ /g, '-') // replaces spaces with hyphens
.replace(/[^\w.-]+/g, ''); // removes all non-word characters except for dots and hyphens
}
Expand Down Expand Up @@ -407,7 +407,7 @@ export class SimpleHelpersModule {
if (pattern instanceof RegExp) {
isCaseInsensitive = pattern.flags.includes('i');
pattern = pattern.toString();
pattern = pattern.match(/\/(.+?)\//)?.[1] ?? ''; // Remove frontslash from front and back of RegExp
pattern = /\/(.+?)\//.exec(pattern)?.[1] ?? ''; // Remove frontslash from front and back of RegExp
}

let min: number;
Expand All @@ -417,7 +417,7 @@ export class SimpleHelpersModule {
// Deal with single wildcards
const SINGLE_CHAR_REG =
/([.A-Za-z0-9])(?:\{(\d+)(?:,(\d+)|)\}|(\?|\*|\+))(?![^[]*]|[^{]*})/;
let token = pattern.match(SINGLE_CHAR_REG);
let token = SINGLE_CHAR_REG.exec(pattern);
while (token != null) {
const quantifierMin: string = token[2];
const quantifierMax: string = token[3];
Expand All @@ -434,14 +434,14 @@ export class SimpleHelpersModule {
pattern.slice(0, token.index) +
token[1].repeat(repetitions) +
pattern.slice(token.index + token[0].length);
token = pattern.match(SINGLE_CHAR_REG);
token = SINGLE_CHAR_REG.exec(pattern);
}

const SINGLE_RANGE_REG = /(\d-\d|\w-\w|\d|\w|[-!@#$&()`.+,/"])/;
const RANGE_ALPHANUMEMRIC_REG =
/\[(\^|)(-|)(.+?)\](?:\{(\d+)(?:,(\d+)|)\}|(\?|\*|\+)|)/;
// Deal with character classes with quantifiers `[a-z0-9]{min[, max]}`
token = pattern.match(RANGE_ALPHANUMEMRIC_REG);
token = RANGE_ALPHANUMEMRIC_REG.exec(pattern);
while (token != null) {
const isNegated = token[1] === '^';
const includesDash: boolean = token[2] === '-';
Expand All @@ -452,7 +452,7 @@ export class SimpleHelpersModule {
const rangeCodes: number[] = [];

let ranges = token[3];
let range = ranges.match(SINGLE_RANGE_REG);
let range = SINGLE_RANGE_REG.exec(ranges);

if (includesDash) {
// 45 is the ascii code for '-'
Expand Down Expand Up @@ -494,7 +494,7 @@ export class SimpleHelpersModule {
}

ranges = ranges.substring(range[0].length);
range = ranges.match(SINGLE_RANGE_REG);
range = SINGLE_RANGE_REG.exec(ranges);
}

repetitions = getRepetitionsBasedOnQuantifierParameters(
Expand Down Expand Up @@ -549,12 +549,12 @@ export class SimpleHelpersModule {
pattern.slice(0, token.index) +
generatedString +
pattern.slice(token.index + token[0].length);
token = pattern.match(RANGE_ALPHANUMEMRIC_REG);
token = RANGE_ALPHANUMEMRIC_REG.exec(pattern);
}

const RANGE_REP_REG = /(.)\{(\d+),(\d+)\}/;
// Deal with quantifier ranges `{min,max}`
token = pattern.match(RANGE_REP_REG);
token = RANGE_REP_REG.exec(pattern);
while (token != null) {
min = parseInt(token[2]);
max = parseInt(token[3]);
Expand All @@ -568,19 +568,19 @@ export class SimpleHelpersModule {
pattern.slice(0, token.index) +
token[1].repeat(repetitions) +
pattern.slice(token.index + token[0].length);
token = pattern.match(RANGE_REP_REG);
token = RANGE_REP_REG.exec(pattern);
}

const REP_REG = /(.)\{(\d+)\}/;
// Deal with repeat `{num}`
token = pattern.match(REP_REG);
token = REP_REG.exec(pattern);
while (token != null) {
repetitions = parseInt(token[2]);
pattern =
pattern.slice(0, token.index) +
token[1].repeat(repetitions) +
pattern.slice(token.index + token[0].length);
token = pattern.match(REP_REG);
token = REP_REG.exec(pattern);
}

return pattern;
Expand Down Expand Up @@ -704,7 +704,7 @@ export class SimpleHelpersModule {
uniqueArray<T>(source: ReadonlyArray<T> | (() => T), length: number): T[] {
if (Array.isArray(source)) {
const set = new Set<T>(source);
const array = Array.from(set);
const array = [...set];
return this.shuffle(array).splice(0, length);
}

Expand All @@ -722,7 +722,7 @@ export class SimpleHelpersModule {
// Ignore
}

return Array.from(set);
return [...set];
}

/**
Expand Down Expand Up @@ -1003,7 +1003,7 @@ export class SimpleHelpersModule {
return [];
}

const arrayCopy = array.slice(0);
const arrayCopy = [...array];
let i = array.length;
const min = i - numElements;
let temp: T;
Expand Down
9 changes: 4 additions & 5 deletions src/modules/internet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,8 @@ export class InternetModule {
// We limit to 50 chars to be more realistic
localPart = localPart.substring(0, 50);
if (allowSpecialCharacters) {
const usernameChars: string[] = '._-'.split('');
const specialChars: string[] = ".!#$%&'*+-/=?^_`{|}~".split('');
const usernameChars: string[] = [...'._-'];
const specialChars: string[] = [...".!#$%&'*+-/=?^_`{|}~"];
localPart = localPart.replace(
this.faker.helpers.arrayElement(usernameChars),
this.faker.helpers.arrayElement(specialChars)
Expand Down Expand Up @@ -636,10 +636,9 @@ export class InternetModule {
// First remove simple accents etc
result = result
.normalize('NFKD') //for example è decomposes to as e + ̀
.replace(/[\u0300-\u036f]/g, ''); // removes combining marks
.replace(/[\u0300-\u036F]/g, ''); // removes combining marks

result = result
.split('')
result = [...result]
.map((char) => {
// If we have a mapping for this character, (for Cyrillic, Greek etc) use it
if (charMapping[char]) {
Expand Down
Loading

0 comments on commit 2fe856a

Please sign in to comment.