Skip to content

Commit

Permalink
chore: merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-DDT committed Apr 9, 2022
2 parents 645e94b + 651d684 commit 5cc84e7
Show file tree
Hide file tree
Showing 20 changed files with 275 additions and 167 deletions.
33 changes: 10 additions & 23 deletions scripts/apidoc/apiDocsWriter.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { writeFileSync } from 'node:fs';
import { resolve } from 'node:path';
import type { Options } from 'prettier';
import { format } from 'prettier';
import prettierConfig from '../../.prettierrc.cjs';
import type { Method } from '../../docs/.vitepress/components/api-docs/method';
import type { PageIndex } from './utils';
import { pathDocsDir, pathOutputDir } from './utils';
import {
formatMarkdown,
formatTypescript,
pathDocsDir,
pathOutputDir,
} from './utils';

const pathDocsApiPages = resolve(pathDocsDir, '.vitepress', 'api-pages.ts');

Expand All @@ -18,21 +20,6 @@ editLink: false
`;

const prettierMarkdown: Options = {
...prettierConfig,
parser: 'markdown',
};

const prettierTypescript: Options = {
...prettierConfig,
parser: 'typescript',
};

const prettierBabel: Options = {
...prettierConfig,
parser: 'babel',
};

/**
* Writes the api page for the given module to the correct location.
*
Expand Down Expand Up @@ -75,7 +62,7 @@ export function writeApiDocsModulePage(
<ApiDocsMethod v-for="method of methods" :key="method.name" :method="method" v-once />
`.replace(/\n +/g, '\n');

content = vitePressInFileOptions + format(content, prettierMarkdown);
content = vitePressInFileOptions + formatMarkdown(content);

writeFileSync(resolve(pathOutputDir, lowerModuleName + '.md'), content);
}
Expand All @@ -98,7 +85,7 @@ export function writeApiDocsDirectPage(methodName: string): void {
<ApiDocsMethod v-for="method of methods" :key="method.name" :method="method" v-once />
`.replace(/\n +/g, '\n');

content = vitePressInFileOptions + format(content, prettierMarkdown);
content = vitePressInFileOptions + formatMarkdown(content);

writeFileSync(resolve(pathOutputDir, methodName + '.md'), content);
}
Expand All @@ -122,7 +109,7 @@ export const ${lowerModuleName}: Method[] = ${JSON.stringify(
2
)}`;

contentTs = format(contentTs, prettierTypescript);
contentTs = formatTypescript(contentTs);

writeFileSync(resolve(pathOutputDir, lowerModuleName + '.ts'), contentTs);
}
Expand All @@ -142,7 +129,7 @@ export function writeApiPagesIndex(pages: PageIndex): void {
export const apiPages = ${JSON.stringify(pages)};
`.replace(/\n +/, '\n');

apiPagesContent = format(apiPagesContent, prettierBabel);
apiPagesContent = formatTypescript(apiPagesContent);

writeFileSync(pathDocsApiPages, apiPagesContent);
}
4 changes: 3 additions & 1 deletion scripts/apidoc/signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type {
MethodParameter,
} from '../../docs/.vitepress/components/api-docs/method';
import { faker } from '../../src';
import { pathOutputDir } from './utils';
import { formatTypescript, pathOutputDir } from './utils';
// TODO ST-DDT 2022-02-20: Actually import this/fix module import errors
// import vitepressConfig from '../../docs/.vitepress/config';

Expand Down Expand Up @@ -245,6 +245,8 @@ function typeToText(type_: Type, short = false): string {
type.indexType,
short
)}]`;
case 'literal':
return formatTypescript(type.toString()).replace(/;\n$/, '');
default:
return type.toString();
}
Expand Down
31 changes: 31 additions & 0 deletions scripts/apidoc/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { resolve } from 'node:path';
import type { Options } from 'prettier';
import { format } from 'prettier';
import * as TypeDoc from 'typedoc';
import prettierConfig from '../../.prettierrc.cjs';
import {
DefaultParameterAwareSerializer,
parameterDefaultReader,
Expand Down Expand Up @@ -44,3 +47,31 @@ export function newTypeDocApp(): TypeDoc.Application {
export function patchProject(project: TypeDoc.ProjectReflection): void {
patchProjectParameterDefaults(project);
}

/**
* Formats markdown contents.
*
* @param text The text to format.
*/
export function formatMarkdown(text: string): string {
return format(text, prettierMarkdown);
}

/**
* Formats typedoc contents.
*
* @param text The text to format.
*/
export function formatTypescript(text: string): string {
return format(text, prettierTypescript);
}

const prettierMarkdown: Options = {
...prettierConfig,
parser: 'markdown',
};

const prettierTypescript: Options = {
...prettierConfig,
parser: 'typescript',
};
6 changes: 3 additions & 3 deletions src/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export class Address {
if (zipRange) {
return String(this.faker.datatype.number(zipRange));
}
return this.faker.address.zipCode();
return this.zipCode();
}

/**
Expand Down Expand Up @@ -228,7 +228,7 @@ export class Address {
*/
streetName(): string {
let result: string;
let suffix = this.faker.address.streetSuffix();
let suffix = this.streetSuffix();
if (suffix !== '') {
suffix = ' ' + suffix;
}
Expand Down Expand Up @@ -498,7 +498,7 @@ export class Address {
): [string, string] {
// If there is no coordinate, the best we can do is return a random GPS coordinate.
if (coordinate === undefined) {
return [this.faker.address.latitude(), this.faker.address.longitude()];
return [this.latitude(), this.longitude()];
}

radius = radius || 10.0;
Expand Down
6 changes: 3 additions & 3 deletions src/commerce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ export class Commerce {
*/
productName(): string {
return (
this.faker.commerce.productAdjective() +
this.productAdjective() +
' ' +
this.faker.commerce.productMaterial() +
this.productMaterial() +
' ' +
this.faker.commerce.product()
this.product()
);
}

Expand Down
15 changes: 6 additions & 9 deletions src/company.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import type { Faker } from '.';
import type { Fake } from './fake';

let f: Fake['fake'];

/**
* Module to generate company related entries.
*/
export class Company {
constructor(private readonly faker: Faker) {
f = this.faker.fake;

// Bind `this` so namespaced is working correctly
for (const name of Object.getOwnPropertyNames(Company.prototype)) {
if (name === 'constructor' || typeof this[name] !== 'function') {
Expand Down Expand Up @@ -49,7 +44,7 @@ export class Company {
format = this.faker.datatype.number(formats.length - 1);
}

return f(formats[format]);
return this.faker.fake(formats[format]);
}

/**
Expand All @@ -59,7 +54,7 @@ export class Company {
* faker.company.companySuffix() // 'and Sons'
*/
companySuffix(): string {
return this.faker.random.arrayElement(this.faker.company.suffixes());
return this.faker.random.arrayElement(this.suffixes());
}

/**
Expand All @@ -69,7 +64,7 @@ export class Company {
* faker.company.catchPhrase() // 'Upgradable systematic flexibility'
*/
catchPhrase(): string {
return f(
return this.faker.fake(
'{{company.catchPhraseAdjective}} {{company.catchPhraseDescriptor}} {{company.catchPhraseNoun}}'
);
}
Expand All @@ -81,7 +76,9 @@ export class Company {
* faker.company.bs() // 'cultivate synergistic e-markets'
*/
bs(): string {
return f('{{company.bsBuzz}} {{company.bsAdjective}} {{company.bsNoun}}');
return this.faker.fake(
'{{company.bsBuzz}} {{company.bsAdjective}} {{company.bsNoun}}'
);
}

/**
Expand Down
48 changes: 21 additions & 27 deletions src/datatype.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Faker } from '.';
import { FakerError } from './errors/faker-error';
import { deprecated } from './internal/deprecated';

/**
Expand All @@ -24,7 +25,7 @@ export class Datatype {
* @param options.max Upper bound for generated number. Defaults to `min + 99999`.
* @param options.precision Precision of the generated number. Defaults to `1`.
*
* @throws When options define `max < min`
* @throws When options define `max < min`.
*
* @example
* faker.datatype.number() // 55422
Expand All @@ -35,25 +36,25 @@ export class Datatype {
* faker.datatype.number({ min: 10, max: 100, precision: 0.01 }) // 36.94
*/
number(
options?: number | { min?: number; max?: number; precision?: number }
options: number | { min?: number; max?: number; precision?: number } = 99999
): number {
const opts = typeof options === 'number' ? { max: options } : options ?? {};
if (typeof options === 'number') {
options = { max: options };
}

const min = typeof opts.min === 'number' ? opts.min : 0;
let max = typeof opts.max === 'number' ? opts.max : min + 99999;
const precision = typeof opts.precision === 'number' ? opts.precision : 1;
const { min = 0, precision = 1 } = options;
const max = options.max ?? min + 99999;

if (max < min) {
throw new Error(`Max ${max} should be larger then min ${min}`);
if (max === min) {
return min;
}

// Make the range inclusive of the max value
if (max >= 0) {
max += precision;
if (max < min) {
throw new FakerError(`Max ${max} should be larger then min ${min}.`);
}

const randomNumber = Math.floor(
this.faker.mersenne.rand(max / precision, min / precision)
this.faker.mersenne.rand(max / precision + 1, min / precision)
);

// Workaround problem in float point arithmetics for e.g. 6681493 / 0.01
Expand Down Expand Up @@ -92,7 +93,7 @@ export class Datatype {
if (opts.precision == null) {
opts.precision = 0.01;
}
return this.faker.datatype.number(opts);
return this.number(opts);
}

/**
Expand Down Expand Up @@ -121,7 +122,7 @@ export class Datatype {
max = Date.UTC(2100, 0);
}

return new Date(this.faker.datatype.number({ min, max }));
return new Date(this.number({ min, max }));
}

/**
Expand All @@ -147,9 +148,7 @@ export class Datatype {
let returnString = '';

for (let i = 0; i < length; i++) {
returnString += String.fromCharCode(
this.faker.datatype.number(charCodeOption)
);
returnString += String.fromCharCode(this.number(charCodeOption));
}

return returnString;
Expand All @@ -164,7 +163,7 @@ export class Datatype {
uuid(): string {
const RFC4122_TEMPLATE = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
const replacePlaceholders = (placeholder) => {
const random = this.faker.datatype.number({ min: 0, max: 15 });
const random = this.number({ min: 0, max: 15 });
const value = placeholder === 'x' ? random : (random & 0x3) | 0x8;
return value.toString(16);
};
Expand All @@ -178,7 +177,7 @@ export class Datatype {
* faker.datatype.boolean() // false
*/
boolean(): boolean {
return !!this.faker.datatype.number(1);
return !!this.number(1);
}

/**
Expand Down Expand Up @@ -258,9 +257,7 @@ export class Datatype {
const returnObject: Record<string, string | number> = {};

properties.forEach((prop) => {
returnObject[prop] = this.faker.datatype.boolean()
? this.faker.datatype.string()
: this.faker.datatype.number();
returnObject[prop] = this.boolean() ? this.string() : this.number();
});

return JSON.stringify(returnObject);
Expand All @@ -277,9 +274,7 @@ export class Datatype {
*/
array(length = 10): Array<string | number> {
return Array.from<string | number>({ length }).map(() =>
this.faker.datatype.boolean()
? this.faker.datatype.string()
: this.faker.datatype.number()
this.boolean() ? this.string() : this.number()
);
}

Expand All @@ -297,8 +292,7 @@ export class Datatype {
*/
bigInt(value?: string | number | bigint | boolean): bigint {
if (value === undefined) {
value =
Math.floor(this.faker.datatype.number() * 99999999999) + 10000000000;
value = Math.floor(this.number() * 99999999999) + 10000000000;
}

return BigInt(value);
Expand Down
4 changes: 2 additions & 2 deletions src/finance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,8 +416,8 @@ export class Finance {
const amount = transaction.amount;
const transactionType = transaction.type;
const company = transaction.business;
const card = this.faker.finance.mask();
const currency = this.faker.finance.currencyCode();
const card = this.mask();
const currency = this.currencyCode();
return (
transactionType +
' transaction at ' +
Expand Down
Loading

0 comments on commit 5cc84e7

Please sign in to comment.