Skip to content

Commit

Permalink
feat(string): move methods to new module (#1155)
Browse files Browse the repository at this point in the history
Co-authored-by: Eric Cheng <ericcheng9316@gmail.com>
Co-authored-by: ST-DDT <ST-DDT@gmx.de>
Co-authored-by: Shinigami92 <chrissi92@hotmail.de>
  • Loading branch information
4 people authored Oct 18, 2022
1 parent c977dbc commit 79858fe
Show file tree
Hide file tree
Showing 17 changed files with 1,366 additions and 318 deletions.
1 change: 1 addition & 0 deletions docs/.vitepress/api-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const apiPages = [
{ text: 'Phone', link: '/api/phone.html' },
{ text: 'Random', link: '/api/random.html' },
{ text: 'Science', link: '/api/science.html' },
{ text: 'String', link: '/api/string.html' },
{ text: 'System', link: '/api/system.html' },
{ text: 'Vehicle', link: '/api/vehicle.html' },
{ text: 'Word', link: '/api/word.html' },
Expand Down
2 changes: 2 additions & 0 deletions src/faker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { PersonModule } from './modules/person';
import { PhoneModule } from './modules/phone';
import { RandomModule } from './modules/random';
import { ScienceModule } from './modules/science';
import { StringModule } from './modules/string';
import { SystemModule } from './modules/system';
import { VehicleModule } from './modules/vehicle';
import { WordModule } from './modules/word';
Expand Down Expand Up @@ -102,6 +103,7 @@ export class Faker {
readonly person: PersonModule = new PersonModule(this);
readonly phone: PhoneModule = new PhoneModule(this);
readonly science: ScienceModule = new ScienceModule(this);
readonly string: StringModule = new StringModule(this);
readonly system: SystemModule = new SystemModule(this);
readonly vehicle: VehicleModule = new VehicleModule(this);
readonly word: WordModule = new WordModule(this);
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export type {
export type { PhoneModule } from './modules/phone';
export type { RandomModule } from './modules/random';
export type { ChemicalElement, ScienceModule, Unit } from './modules/science';
export type { StringModule } from './modules/string';
export type { SystemModule } from './modules/system';
export type { VehicleModule } from './modules/vehicle';
export type { WordModule } from './modules/word';
Expand Down
2 changes: 1 addition & 1 deletion src/modules/color/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ export class ColorModule {
let color: string | number[];
let cssFunction: CSSFunction = 'rgb';
if (format === 'hex') {
color = this.faker.datatype.hexadecimal({
color = this.faker.string.hexadecimal({
length: includeAlpha ? 8 : 6,
prefix: '',
});
Expand Down
4 changes: 2 additions & 2 deletions src/modules/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ export class DatabaseModule {
* @since 6.2.0
*/
mongodbObjectId(): string {
return this.faker.datatype.hexadecimal({
return this.faker.string.hexadecimal({
length: 24,
case: 'lower',
casing: 'lower',
prefix: '',
});
}
Expand Down
105 changes: 40 additions & 65 deletions src/modules/datatype/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Faker } from '../..';
import { FakerError } from '../../errors/faker-error';
import { deprecated } from '../../internal/deprecated';
import type { MersenneModule } from '../../internal/mersenne/mersenne';

/**
Expand Down Expand Up @@ -145,48 +146,46 @@ export class DatatypeModule {
*
* @param length Length of the generated string. Max length is `2^20`. Defaults to `10`.
*
* @see faker.string.sample()
*
* @example
* faker.datatype.string() // 'Zo!.:*e>wR'
* faker.datatype.string(5) // '6Bye8'
*
* @since 5.5.0
*
* @deprecated Use faker.string.sample() instead.
*/
string(length = 10): string {
const maxLength = Math.pow(2, 20);
if (length >= maxLength) {
length = maxLength;
}

const charCodeOption = {
min: 33,
max: 125,
};

let returnString = '';

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

return returnString;
deprecated({
deprecated: 'faker.datatype.string()',
proposed: 'faker.string.sample()',
since: '8.0',
until: '9.0',
});
return this.faker.string.sample(length);
}

/**
* Returns a UUID v4 ([Universally Unique Identifier](https://en.wikipedia.org/wiki/Universally_unique_identifier)).
*
* @see faker.string.uuid()
*
* @example
* faker.datatype.uuid() // '4136cd0b-d90b-4af7-b485-5d1ded8db252'
*
* @since 5.5.0
*
* @deprecated Use faker.string.uuid() instead.
*/
uuid(): string {
const RFC4122_TEMPLATE = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
const replacePlaceholders = (placeholder) => {
const random = this.number({ min: 0, max: 15 });
const value = placeholder === 'x' ? random : (random & 0x3) | 0x8;
return value.toString(16);
};
return RFC4122_TEMPLATE.replace(/[xy]/g, replacePlaceholders);
deprecated({
deprecated: 'faker.datatype.uuid()',
proposed: 'faker.string.uuid()',
since: '8.0',
until: '9.0',
});
return this.faker.string.uuid();
}

/**
Expand All @@ -209,6 +208,8 @@ export class DatatypeModule {
* @param options.prefix Prefix for the generated number. Defaults to `'0x'`.
* @param options.case Case of the generated number. Defaults to `'mixed'`.
*
* @see faker.string.hexadecimal()
*
* @example
* faker.datatype.hexadecimal() // '0xB'
* faker.datatype.hexadecimal({ length: 10 }) // '0xaE13d044cB'
Expand All @@ -220,6 +221,8 @@ export class DatatypeModule {
* faker.datatype.hexadecimal({ length: 10, prefix: '0x', case: 'mixed' }) // '0xAdE330a4D1'
*
* @since 6.1.2
*
* @deprecated Use `faker.string.hexadecimal()` instead.
*/
hexadecimal(
options: {
Expand All @@ -228,44 +231,13 @@ export class DatatypeModule {
case?: 'lower' | 'upper' | 'mixed';
} = {}
): string {
const { length = 1, prefix = '0x', case: letterCase = 'mixed' } = options;

let wholeString = '';

for (let i = 0; i < length; i++) {
wholeString += this.faker.helpers.arrayElement([
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'a',
'b',
'c',
'd',
'e',
'f',
'A',
'B',
'C',
'D',
'E',
'F',
]);
}

if (letterCase === 'upper') {
wholeString = wholeString.toUpperCase();
} else if (letterCase === 'lower') {
wholeString = wholeString.toLowerCase();
}

return `${prefix}${wholeString}`;
deprecated({
deprecated: 'faker.datatype.hexadecimal()',
proposed: 'faker.string.hexadecimal()',
since: '8.0',
until: '9.0',
});
return this.faker.string.hexadecimal({ ...options, casing: options.case });
}

/**
Expand All @@ -281,7 +253,9 @@ export class DatatypeModule {
const returnObject: Record<string, string | number> = {};

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

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

Expand Down Expand Up @@ -356,7 +330,8 @@ export class DatatypeModule {

const offset =
BigInt(
this.faker.random.numeric(delta.toString(10).length, {
this.faker.string.numeric({
length: delta.toString(10).length,
allowLeadingZeros: true,
})
) %
Expand Down
18 changes: 11 additions & 7 deletions src/modules/finance/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ export class FinanceModule {

let address = this.faker.helpers.arrayElement(['1', '3']);

address += this.faker.random.alphaNumeric(addressLength, {
address += this.faker.string.alphanumeric({
length: addressLength,
casing: 'mixed',
bannedChars: '0OIl',
});
Expand Down Expand Up @@ -353,9 +354,9 @@ export class FinanceModule {
* @since 5.0.0
*/
ethereumAddress(): string {
const address = this.faker.datatype.hexadecimal({
const address = this.faker.string.hexadecimal({
length: 40,
case: 'lower',
casing: 'lower',
});
return address;
}
Expand Down Expand Up @@ -446,15 +447,18 @@ export class FinanceModule {
): string {
const { includeBranchCode = this.faker.datatype.boolean() } = options;

const bankIdentifier = this.faker.random.alpha({
count: 4,
const bankIdentifier = this.faker.string.alpha({
length: 4,
casing: 'upper',
});
const countryCode = this.faker.helpers.arrayElement(iban.iso3166);
const locationCode = this.faker.random.alphaNumeric(2, { casing: 'upper' });
const locationCode = this.faker.string.alphanumeric({
length: 2,
casing: 'upper',
});
const branchCode = includeBranchCode
? this.faker.datatype.boolean()
? this.faker.random.alphaNumeric(3, { casing: 'upper' })
? this.faker.string.alphanumeric({ length: 3, casing: 'upper' })
: 'XXX'
: '';

Expand Down
8 changes: 4 additions & 4 deletions src/modules/git/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ export class GitModule {
* @since 5.0.0
*/
commitSha(): string {
return this.faker.datatype.hexadecimal({
return this.faker.string.hexadecimal({
length: 40,
case: 'lower',
casing: 'lower',
prefix: '',
});
}
Expand All @@ -116,9 +116,9 @@ export class GitModule {
* @since 5.0.0
*/
shortSha(): string {
return this.faker.datatype.hexadecimal({
return this.faker.string.hexadecimal({
length: 7,
case: 'lower',
casing: 'lower',
prefix: '',
});
}
Expand Down
Loading

0 comments on commit 79858fe

Please sign in to comment.