Skip to content

Commit

Permalink
Merge branch 'next' into Gyran-patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-DDT authored Nov 7, 2023
2 parents 09f15d0 + e0ba50b commit e7fd39e
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 103 deletions.
2 changes: 0 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ module.exports = defineConfig({
'unicorn/consistent-function-scoping': 'off',
'unicorn/import-style': 'off',
'unicorn/no-array-callback-reference': 'off',
'unicorn/no-array-reduce': 'off',
'unicorn/no-await-expression-member': 'off',
'unicorn/no-negated-condition': 'off',
'unicorn/no-object-as-default-parameter': 'off',
'unicorn/no-useless-switch-case': 'off',
'unicorn/numeric-separators-style': 'off',
Expand Down
4 changes: 2 additions & 2 deletions scripts/apidoc/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ async function loadRemote(url: string): Promise<DocsApiDiffIndex> {
throw new Error(
`Failed to load remote diff index from ${url}: ${res.statusText}`
);
} else {
return res.json() as Promise<DocsApiDiffIndex>;
}

return res.json() as Promise<DocsApiDiffIndex>;
});
}

Expand Down
23 changes: 10 additions & 13 deletions scripts/apidoc/writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,17 @@ export async function writeApiDocsModule(
text: moduleName,
link: `/api/${lowerModuleName}.html`,
methods,
diff: methods.reduce(
(data, method) => ({
...data,
[method.name]: methodDiffHash(method),
diff: {
moduleHash: diffHash({
name: moduleName,
field: lowerModuleName,
deprecated,
comment,
}),
{
moduleHash: diffHash({
name: moduleName,
field: lowerModuleName,
deprecated,
comment,
}),
}
),
...Object.fromEntries(
methods.map((method) => [method.name, methodDiffHash(method)])
),
},
};
}

Expand Down
25 changes: 25 additions & 0 deletions src/internal/group-by.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Groups the values by the key function.
*
* @internal
*
* @param values The values to group.
* @param keyFunction The function to get the key from the value.
*/
export function groupBy<TValue>(
values: ReadonlyArray<TValue>,
keyFunction: (value: TValue) => string | number
): Record<string, TValue[]> {
const result: Record<string, TValue[]> = {};

for (const value of values) {
const key = keyFunction(value);
if (result[key] === undefined) {
result[key] = [];
}

result[key].push(value);
}

return result;
}
2 changes: 1 addition & 1 deletion src/modules/finance/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ export class FinanceModule extends ModuleBase {
options = { length: options };
}

const { ellipsis, length = 4, parens } = options;
const { ellipsis = true, length = 4, parens = true } = options;

let template = this.faker.string.numeric({ length });

Expand Down
22 changes: 11 additions & 11 deletions src/modules/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,17 +456,7 @@ export class SimpleHelpersModule extends SimpleModuleBase {
}

while (range != null) {
if (!range[0].includes('-')) {
// handle non-ranges
if (isCaseInsensitive && Number.isNaN(Number(range[0]))) {
rangeCodes.push(
range[0].toUpperCase().charCodeAt(0),
range[0].toLowerCase().charCodeAt(0)
);
} else {
rangeCodes.push(range[0].charCodeAt(0));
}
} else {
if (range[0].includes('-')) {
// handle ranges
const rangeMinMax = range[0].split('-').map((x) => x.charCodeAt(0));
min = rangeMinMax[0];
Expand All @@ -490,6 +480,16 @@ export class SimpleHelpersModule extends SimpleModuleBase {
rangeCodes.push(i);
}
}
} else {
// handle non-ranges
if (isCaseInsensitive && Number.isNaN(Number(range[0]))) {
rangeCodes.push(
range[0].toUpperCase().charCodeAt(0),
range[0].toLowerCase().charCodeAt(0)
);
} else {
rangeCodes.push(range[0].charCodeAt(0));
}
}

ranges = ranges.substring(range[0].length);
Expand Down
2 changes: 1 addition & 1 deletion src/modules/image/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export class ImageModule extends ModuleBase {
const { width = 640, height = 480, category } = options;

return `https://loremflickr.com/${width}/${height}${
category != null ? `/${category}` : ''
category == null ? '' : `/${category}`
}?lock=${this.faker.number.int()}`;
}

Expand Down
10 changes: 2 additions & 8 deletions src/modules/word/filter-word-list-by-length.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FakerError } from '../../errors/faker-error';
import { groupBy } from '../../internal/group-by';

/**
* The error handling strategies for the `filterWordListByLength` function.
Expand All @@ -13,14 +14,7 @@ const STRATEGIES = {
wordList: ReadonlyArray<string>,
length: { min: number; max: number }
): string[] => {
const wordsByLength = wordList.reduce<Record<number, string[]>>(
(data, word) => {
(data[word.length] = data[word.length] ?? []).push(word);
return data;
},
{}
);

const wordsByLength = groupBy(wordList, (word) => word.length);
const lengths = Object.keys(wordsByLength).map(Number);
const min = Math.min(...lengths);
const max = Math.max(...lengths);
Expand Down
59 changes: 32 additions & 27 deletions test/all-functional.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,36 @@ const IGNORED_MODULES = new Set([
'_defaultRefDate',
]);

function isTestableModule(mod: string) {
return !IGNORED_MODULES.has(mod);
function getMethodNamesByModules(faker: Faker): { [module: string]: string[] } {
return Object.fromEntries(
Object.keys(faker)
.filter(isTestableModule)
.sort()
.map<[string, string[]]>((moduleName) => [
moduleName,
getMethodNamesOf(faker[moduleName]),
])
.filter(([module, methods]) => {
if (methods.length === 0) {
console.log(`Skipping ${module} - No testable methods`);
return false;
}

return true;
})
);
}

function isTestableModule(moduleName: string): moduleName is keyof Faker {
return !IGNORED_MODULES.has(moduleName);
}

function getMethodNamesOf(module: object): string[] {
return Object.keys(module).filter(isMethodOf(module));
}

function isMethodOf(mod: string) {
return (meth: string) => typeof fakerEN[mod][meth] === 'function';
function isMethodOf(module: object): (method: string) => boolean {
return (method: string) => typeof module[method] === 'function';
}

type SkipConfig<TModule> = Partial<
Expand Down Expand Up @@ -53,36 +77,17 @@ const BROKEN_LOCALE_METHODS = {
};

function isWorkingLocaleForMethod(
mod: string,
meth: string,
module: string,
method: string,
locale: string
): boolean {
const broken = BROKEN_LOCALE_METHODS[mod]?.[meth] ?? [];
const broken = BROKEN_LOCALE_METHODS[module]?.[method] ?? [];
return broken !== '*' && !broken.includes(locale);
}

// Basic smoke tests to make sure each method is at least implemented and returns a value.

function modulesList(): { [module: string]: string[] } {
const modules = Object.keys(fakerEN)
.sort()
.filter(isTestableModule)
.reduce((result, mod) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
const methods = Object.keys(fakerEN[mod]).filter(isMethodOf(mod));
if (methods.length > 0) {
result[mod] = methods;
} else {
console.log(`Skipping ${mod} - No testable methods`);
}

return result;
}, {});

return modules;
}

const modules = modulesList();
const modules = getMethodNamesByModules(fakerEN);

describe('BROKEN_LOCALE_METHODS test', () => {
it('should not contain obsolete configuration (modules)', () => {
Expand Down
54 changes: 27 additions & 27 deletions test/modules/__snapshots__/finance.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,23 @@ exports[`finance > 42 > iban > with formatted option 1`] = `"GT03 9751 1086 7098

exports[`finance > 42 > litecoinAddress 1`] = `"3XbJMAAara64sSkA9HD24YHQWd1b"`;

exports[`finance > 42 > mask > noArgs 1`] = `"3791"`;
exports[`finance > 42 > mask > noArgs 1`] = `"(...3791)"`;

exports[`finance > 42 > mask > with ellipsis 1`] = `"...3791"`;
exports[`finance > 42 > mask > with ellipsis 1`] = `"(...3791)"`;

exports[`finance > 42 > mask > with length 1`] = `"37917"`;
exports[`finance > 42 > mask > with length 1`] = `"(...37917)"`;

exports[`finance > 42 > mask > with length, parenthesis and ellipsis 1`] = `"(...37917)"`;

exports[`finance > 42 > mask > with parenthesis 1`] = `"(3791)"`;
exports[`finance > 42 > mask > with parenthesis 1`] = `"(...3791)"`;

exports[`finance > 42 > maskedNumber > noArgs 1`] = `"3791"`;
exports[`finance > 42 > maskedNumber > noArgs 1`] = `"(...3791)"`;

exports[`finance > 42 > maskedNumber > with length 1`] = `"37917"`;
exports[`finance > 42 > maskedNumber > with length 1`] = `"(...37917)"`;

exports[`finance > 42 > maskedNumber > with length and parenthesis option 1`] = `"37917"`;
exports[`finance > 42 > maskedNumber > with length and parenthesis option 1`] = `"...37917"`;

exports[`finance > 42 > maskedNumber > with length option 1`] = `"37917"`;
exports[`finance > 42 > maskedNumber > with length option 1`] = `"(...37917)"`;

exports[`finance > 42 > maskedNumber > with length, parenthesis and ellipsis option 1`] = `"...37917"`;

Expand All @@ -106,7 +106,7 @@ exports[`finance > 42 > pin > with length option 1`] = `"3791775514"`;

exports[`finance > 42 > routingNumber 1`] = `"379177554"`;

exports[`finance > 42 > transactionDescription 1`] = `"invoice transaction at Wiegand, Deckow and Reynolds using card ending with ***8361 for RSD 374.54 in account ***55141004"`;
exports[`finance > 42 > transactionDescription 1`] = `"invoice transaction at Wiegand, Deckow and Reynolds using card ending with ***(...8361) for RSD 374.54 in account ***55141004"`;

exports[`finance > 42 > transactionType 1`] = `"withdrawal"`;

Expand Down Expand Up @@ -188,23 +188,23 @@ exports[`finance > 1211 > iban > with formatted option 1`] = `"TN42 8201 6024 17

exports[`finance > 1211 > litecoinAddress 1`] = `"MTMe8Z3EaFdLqmaGKP1LEEJQVriSZRZds"`;

exports[`finance > 1211 > mask > noArgs 1`] = `"9487"`;
exports[`finance > 1211 > mask > noArgs 1`] = `"(...9487)"`;

exports[`finance > 1211 > mask > with ellipsis 1`] = `"...9487"`;
exports[`finance > 1211 > mask > with ellipsis 1`] = `"(...9487)"`;

exports[`finance > 1211 > mask > with length 1`] = `"94872"`;
exports[`finance > 1211 > mask > with length 1`] = `"(...94872)"`;

exports[`finance > 1211 > mask > with length, parenthesis and ellipsis 1`] = `"(...94872)"`;

exports[`finance > 1211 > mask > with parenthesis 1`] = `"(9487)"`;
exports[`finance > 1211 > mask > with parenthesis 1`] = `"(...9487)"`;

exports[`finance > 1211 > maskedNumber > noArgs 1`] = `"9487"`;
exports[`finance > 1211 > maskedNumber > noArgs 1`] = `"(...9487)"`;

exports[`finance > 1211 > maskedNumber > with length 1`] = `"94872"`;
exports[`finance > 1211 > maskedNumber > with length 1`] = `"(...94872)"`;

exports[`finance > 1211 > maskedNumber > with length and parenthesis option 1`] = `"94872"`;
exports[`finance > 1211 > maskedNumber > with length and parenthesis option 1`] = `"...94872"`;

exports[`finance > 1211 > maskedNumber > with length option 1`] = `"94872"`;
exports[`finance > 1211 > maskedNumber > with length option 1`] = `"(...94872)"`;

exports[`finance > 1211 > maskedNumber > with length, parenthesis and ellipsis option 1`] = `"...94872"`;

Expand All @@ -216,7 +216,7 @@ exports[`finance > 1211 > pin > with length option 1`] = `"9487219061"`;

exports[`finance > 1211 > routingNumber 1`] = `"948721904"`;

exports[`finance > 1211 > transactionDescription 1`] = `"deposit transaction at Trantow - Satterfield using card ending with ***4316 for SDG 928.52 in account ***19061627"`;
exports[`finance > 1211 > transactionDescription 1`] = `"deposit transaction at Trantow - Satterfield using card ending with ***(...4316) for SDG 928.52 in account ***19061627"`;

exports[`finance > 1211 > transactionType 1`] = `"invoice"`;

Expand Down Expand Up @@ -298,23 +298,23 @@ exports[`finance > 1337 > iban > with formatted option 1`] = `"FO56 1005 0250 09

exports[`finance > 1337 > litecoinAddress 1`] = `"Madhxs2jewAgkYgJi7No6Cn8JZar"`;

exports[`finance > 1337 > mask > noArgs 1`] = `"2512"`;
exports[`finance > 1337 > mask > noArgs 1`] = `"(...2512)"`;

exports[`finance > 1337 > mask > with ellipsis 1`] = `"...2512"`;
exports[`finance > 1337 > mask > with ellipsis 1`] = `"(...2512)"`;

exports[`finance > 1337 > mask > with length 1`] = `"25122"`;
exports[`finance > 1337 > mask > with length 1`] = `"(...25122)"`;

exports[`finance > 1337 > mask > with length, parenthesis and ellipsis 1`] = `"(...25122)"`;

exports[`finance > 1337 > mask > with parenthesis 1`] = `"(2512)"`;
exports[`finance > 1337 > mask > with parenthesis 1`] = `"(...2512)"`;

exports[`finance > 1337 > maskedNumber > noArgs 1`] = `"2512"`;
exports[`finance > 1337 > maskedNumber > noArgs 1`] = `"(...2512)"`;

exports[`finance > 1337 > maskedNumber > with length 1`] = `"25122"`;
exports[`finance > 1337 > maskedNumber > with length 1`] = `"(...25122)"`;

exports[`finance > 1337 > maskedNumber > with length and parenthesis option 1`] = `"25122"`;
exports[`finance > 1337 > maskedNumber > with length and parenthesis option 1`] = `"...25122"`;

exports[`finance > 1337 > maskedNumber > with length option 1`] = `"25122"`;
exports[`finance > 1337 > maskedNumber > with length option 1`] = `"(...25122)"`;

exports[`finance > 1337 > maskedNumber > with length, parenthesis and ellipsis option 1`] = `"...25122"`;

Expand All @@ -326,6 +326,6 @@ exports[`finance > 1337 > pin > with length option 1`] = `"2512254032"`;

exports[`finance > 1337 > routingNumber 1`] = `"251225401"`;

exports[`finance > 1337 > transactionDescription 1`] = `"withdrawal transaction at Cronin - Effertz using card ending with ***3927 for GIP 262.02 in account ***54032552"`;
exports[`finance > 1337 > transactionDescription 1`] = `"withdrawal transaction at Cronin - Effertz using card ending with ***(...3927) for GIP 262.02 in account ***54032552"`;

exports[`finance > 1337 > transactionType 1`] = `"withdrawal"`;
6 changes: 6 additions & 0 deletions test/modules/finance.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,12 @@ describe('finance', () => {
});

describe('maskedNumber()', () => {
it('should return contain parenthesis, ellipsis and have a length of 4 by default', () => {
const actual = faker.finance.maskedNumber();

expect(actual).toMatch(/\(\.{3}\d{4}\)/);
});

it('should set a default length', () => {
const expected = 4; // default account mask length
const mask = faker.finance.maskedNumber({
Expand Down
Loading

0 comments on commit e7fd39e

Please sign in to comment.