Skip to content

Commit 79858fe

Browse files
xDivisionByZeroxejchengST-DDTShinigami92
authored
feat(string): move methods to new module (#1155)
Co-authored-by: Eric Cheng <[email protected]> Co-authored-by: ST-DDT <[email protected]> Co-authored-by: Shinigami92 <[email protected]>
1 parent c977dbc commit 79858fe

File tree

17 files changed

+1366
-318
lines changed

17 files changed

+1366
-318
lines changed

docs/.vitepress/api-pages.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export const apiPages = [
2222
{ text: 'Phone', link: '/api/phone.html' },
2323
{ text: 'Random', link: '/api/random.html' },
2424
{ text: 'Science', link: '/api/science.html' },
25+
{ text: 'String', link: '/api/string.html' },
2526
{ text: 'System', link: '/api/system.html' },
2627
{ text: 'Vehicle', link: '/api/vehicle.html' },
2728
{ text: 'Word', link: '/api/word.html' },

src/faker.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { PersonModule } from './modules/person';
2424
import { PhoneModule } from './modules/phone';
2525
import { RandomModule } from './modules/random';
2626
import { ScienceModule } from './modules/science';
27+
import { StringModule } from './modules/string';
2728
import { SystemModule } from './modules/system';
2829
import { VehicleModule } from './modules/vehicle';
2930
import { WordModule } from './modules/word';
@@ -102,6 +103,7 @@ export class Faker {
102103
readonly person: PersonModule = new PersonModule(this);
103104
readonly phone: PhoneModule = new PhoneModule(this);
104105
readonly science: ScienceModule = new ScienceModule(this);
106+
readonly string: StringModule = new StringModule(this);
105107
readonly system: SystemModule = new SystemModule(this);
106108
readonly vehicle: VehicleModule = new VehicleModule(this);
107109
readonly word: WordModule = new WordModule(this);

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export type {
6767
export type { PhoneModule } from './modules/phone';
6868
export type { RandomModule } from './modules/random';
6969
export type { ChemicalElement, ScienceModule, Unit } from './modules/science';
70+
export type { StringModule } from './modules/string';
7071
export type { SystemModule } from './modules/system';
7172
export type { VehicleModule } from './modules/vehicle';
7273
export type { WordModule } from './modules/word';

src/modules/color/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ export class ColorModule {
313313
let color: string | number[];
314314
let cssFunction: CSSFunction = 'rgb';
315315
if (format === 'hex') {
316-
color = this.faker.datatype.hexadecimal({
316+
color = this.faker.string.hexadecimal({
317317
length: includeAlpha ? 8 : 6,
318318
prefix: '',
319319
});

src/modules/database/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ export class DatabaseModule {
7979
* @since 6.2.0
8080
*/
8181
mongodbObjectId(): string {
82-
return this.faker.datatype.hexadecimal({
82+
return this.faker.string.hexadecimal({
8383
length: 24,
84-
case: 'lower',
84+
casing: 'lower',
8585
prefix: '',
8686
});
8787
}

src/modules/datatype/index.ts

Lines changed: 40 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Faker } from '../..';
22
import { FakerError } from '../../errors/faker-error';
3+
import { deprecated } from '../../internal/deprecated';
34
import type { MersenneModule } from '../../internal/mersenne/mersenne';
45

56
/**
@@ -145,48 +146,46 @@ export class DatatypeModule {
145146
*
146147
* @param length Length of the generated string. Max length is `2^20`. Defaults to `10`.
147148
*
149+
* @see faker.string.sample()
150+
*
148151
* @example
149152
* faker.datatype.string() // 'Zo!.:*e>wR'
150153
* faker.datatype.string(5) // '6Bye8'
151154
*
152155
* @since 5.5.0
156+
*
157+
* @deprecated Use faker.string.sample() instead.
153158
*/
154159
string(length = 10): string {
155-
const maxLength = Math.pow(2, 20);
156-
if (length >= maxLength) {
157-
length = maxLength;
158-
}
159-
160-
const charCodeOption = {
161-
min: 33,
162-
max: 125,
163-
};
164-
165-
let returnString = '';
166-
167-
for (let i = 0; i < length; i++) {
168-
returnString += String.fromCharCode(this.number(charCodeOption));
169-
}
170-
171-
return returnString;
160+
deprecated({
161+
deprecated: 'faker.datatype.string()',
162+
proposed: 'faker.string.sample()',
163+
since: '8.0',
164+
until: '9.0',
165+
});
166+
return this.faker.string.sample(length);
172167
}
173168

174169
/**
175170
* Returns a UUID v4 ([Universally Unique Identifier](https://en.wikipedia.org/wiki/Universally_unique_identifier)).
176171
*
172+
* @see faker.string.uuid()
173+
*
177174
* @example
178175
* faker.datatype.uuid() // '4136cd0b-d90b-4af7-b485-5d1ded8db252'
179176
*
180177
* @since 5.5.0
178+
*
179+
* @deprecated Use faker.string.uuid() instead.
181180
*/
182181
uuid(): string {
183-
const RFC4122_TEMPLATE = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
184-
const replacePlaceholders = (placeholder) => {
185-
const random = this.number({ min: 0, max: 15 });
186-
const value = placeholder === 'x' ? random : (random & 0x3) | 0x8;
187-
return value.toString(16);
188-
};
189-
return RFC4122_TEMPLATE.replace(/[xy]/g, replacePlaceholders);
182+
deprecated({
183+
deprecated: 'faker.datatype.uuid()',
184+
proposed: 'faker.string.uuid()',
185+
since: '8.0',
186+
until: '9.0',
187+
});
188+
return this.faker.string.uuid();
190189
}
191190

192191
/**
@@ -209,6 +208,8 @@ export class DatatypeModule {
209208
* @param options.prefix Prefix for the generated number. Defaults to `'0x'`.
210209
* @param options.case Case of the generated number. Defaults to `'mixed'`.
211210
*
211+
* @see faker.string.hexadecimal()
212+
*
212213
* @example
213214
* faker.datatype.hexadecimal() // '0xB'
214215
* faker.datatype.hexadecimal({ length: 10 }) // '0xaE13d044cB'
@@ -220,6 +221,8 @@ export class DatatypeModule {
220221
* faker.datatype.hexadecimal({ length: 10, prefix: '0x', case: 'mixed' }) // '0xAdE330a4D1'
221222
*
222223
* @since 6.1.2
224+
*
225+
* @deprecated Use `faker.string.hexadecimal()` instead.
223226
*/
224227
hexadecimal(
225228
options: {
@@ -228,44 +231,13 @@ export class DatatypeModule {
228231
case?: 'lower' | 'upper' | 'mixed';
229232
} = {}
230233
): string {
231-
const { length = 1, prefix = '0x', case: letterCase = 'mixed' } = options;
232-
233-
let wholeString = '';
234-
235-
for (let i = 0; i < length; i++) {
236-
wholeString += this.faker.helpers.arrayElement([
237-
'0',
238-
'1',
239-
'2',
240-
'3',
241-
'4',
242-
'5',
243-
'6',
244-
'7',
245-
'8',
246-
'9',
247-
'a',
248-
'b',
249-
'c',
250-
'd',
251-
'e',
252-
'f',
253-
'A',
254-
'B',
255-
'C',
256-
'D',
257-
'E',
258-
'F',
259-
]);
260-
}
261-
262-
if (letterCase === 'upper') {
263-
wholeString = wholeString.toUpperCase();
264-
} else if (letterCase === 'lower') {
265-
wholeString = wholeString.toLowerCase();
266-
}
267-
268-
return `${prefix}${wholeString}`;
234+
deprecated({
235+
deprecated: 'faker.datatype.hexadecimal()',
236+
proposed: 'faker.string.hexadecimal()',
237+
since: '8.0',
238+
until: '9.0',
239+
});
240+
return this.faker.string.hexadecimal({ ...options, casing: options.case });
269241
}
270242

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

283255
properties.forEach((prop) => {
284-
returnObject[prop] = this.boolean() ? this.string() : this.number();
256+
returnObject[prop] = this.boolean()
257+
? this.faker.string.sample()
258+
: this.number();
285259
});
286260

287261
return JSON.stringify(returnObject);
@@ -300,7 +274,7 @@ export class DatatypeModule {
300274
*/
301275
array(length = 10): Array<string | number> {
302276
return Array.from<string | number>({ length }).map(() =>
303-
this.boolean() ? this.string() : this.number()
277+
this.boolean() ? this.faker.string.sample() : this.number()
304278
);
305279
}
306280

@@ -356,7 +330,8 @@ export class DatatypeModule {
356330

357331
const offset =
358332
BigInt(
359-
this.faker.random.numeric(delta.toString(10).length, {
333+
this.faker.string.numeric({
334+
length: delta.toString(10).length,
360335
allowLeadingZeros: true,
361336
})
362337
) %

src/modules/finance/index.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,8 @@ export class FinanceModule {
235235

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

238-
address += this.faker.random.alphaNumeric(addressLength, {
238+
address += this.faker.string.alphanumeric({
239+
length: addressLength,
239240
casing: 'mixed',
240241
bannedChars: '0OIl',
241242
});
@@ -353,9 +354,9 @@ export class FinanceModule {
353354
* @since 5.0.0
354355
*/
355356
ethereumAddress(): string {
356-
const address = this.faker.datatype.hexadecimal({
357+
const address = this.faker.string.hexadecimal({
357358
length: 40,
358-
case: 'lower',
359+
casing: 'lower',
359360
});
360361
return address;
361362
}
@@ -446,15 +447,18 @@ export class FinanceModule {
446447
): string {
447448
const { includeBranchCode = this.faker.datatype.boolean() } = options;
448449

449-
const bankIdentifier = this.faker.random.alpha({
450-
count: 4,
450+
const bankIdentifier = this.faker.string.alpha({
451+
length: 4,
451452
casing: 'upper',
452453
});
453454
const countryCode = this.faker.helpers.arrayElement(iban.iso3166);
454-
const locationCode = this.faker.random.alphaNumeric(2, { casing: 'upper' });
455+
const locationCode = this.faker.string.alphanumeric({
456+
length: 2,
457+
casing: 'upper',
458+
});
455459
const branchCode = includeBranchCode
456460
? this.faker.datatype.boolean()
457-
? this.faker.random.alphaNumeric(3, { casing: 'upper' })
461+
? this.faker.string.alphanumeric({ length: 3, casing: 'upper' })
458462
: 'XXX'
459463
: '';
460464

src/modules/git/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ export class GitModule {
100100
* @since 5.0.0
101101
*/
102102
commitSha(): string {
103-
return this.faker.datatype.hexadecimal({
103+
return this.faker.string.hexadecimal({
104104
length: 40,
105-
case: 'lower',
105+
casing: 'lower',
106106
prefix: '',
107107
});
108108
}
@@ -116,9 +116,9 @@ export class GitModule {
116116
* @since 5.0.0
117117
*/
118118
shortSha(): string {
119-
return this.faker.datatype.hexadecimal({
119+
return this.faker.string.hexadecimal({
120120
length: 7,
121-
case: 'lower',
121+
casing: 'lower',
122122
prefix: '',
123123
});
124124
}

0 commit comments

Comments
 (0)