Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(string): move methods to new module #1155

Merged
merged 49 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from 48 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
7c09bc6
feat(modules): StringModule
xDivisionByZerox Jul 13, 2022
dd34917
feat(StringModule): uuid
xDivisionByZerox Jul 13, 2022
ca54d0f
test(StringModule): uuid
xDivisionByZerox Jul 13, 2022
36532a9
refactor(Datatype): deprecate uuid
xDivisionByZerox Jul 13, 2022
6a2a9d8
feat(StringModule): hexadecimal
xDivisionByZerox Jul 13, 2022
1dbad5e
test(StringModule): hexadecimal
xDivisionByZerox Jul 13, 2022
8ce8c77
refactor(Datatype): deprecate hexadecimal
xDivisionByZerox Jul 13, 2022
197d385
feat(StringModule): random
xDivisionByZerox Jul 13, 2022
2735d90
test(StringModule): random
xDivisionByZerox Jul 13, 2022
0967750
refactor(Datatype): deprecate string
xDivisionByZerox Jul 13, 2022
578b79a
feat(StringModule): alpha
xDivisionByZerox Jul 14, 2022
63c3ff4
test(StringModule): alpha
xDivisionByZerox Jul 14, 2022
f0020b1
refactor(Random): deprecate alpha
xDivisionByZerox Jul 14, 2022
ce9889e
feat(StringModule): alphaNumeric
xDivisionByZerox Jul 14, 2022
b470b11
test(StringModule): alphaNumeric
xDivisionByZerox Jul 14, 2022
d810794
refactor(Random): deprecate alphaNumeric
xDivisionByZerox Jul 15, 2022
cd8cec2
feat(StringModule): numeric
xDivisionByZerox Jul 15, 2022
4560f65
test(StringModule): numeric
xDivisionByZerox Jul 15, 2022
ea72002
refactor(Random): deprecate numeric
xDivisionByZerox Jul 15, 2022
36361da
chore(Random): import char types from StringModule
xDivisionByZerox Jul 15, 2022
483891f
refactor(string): rename alphaNumeric to alphanumeric
xDivisionByZerox Jul 21, 2022
28e6da9
test(helpers): fix alpha casing expectation
xDivisionByZerox Sep 9, 2022
3338fd6
docs(string): JSDocs add `@since` tag
xDivisionByZerox Sep 9, 2022
fdb9878
test(helpers): fix alpha casing expectation for real
xDivisionByZerox Sep 9, 2022
0fffffb
refactor(finance): use `string.alpha` in `bic`
xDivisionByZerox Sep 9, 2022
9d955ce
docs(datatype): human readble deprecation message
xDivisionByZerox Sep 9, 2022
9935052
test(datatype): fix hexadecimal expectations
xDivisionByZerox Sep 9, 2022
a4f40b9
Merge branch 'next' into feat/modules/string
Shinigami92 Oct 17, 2022
e9829fe
chore: fix missing import
Shinigami92 Oct 17, 2022
17c2a1e
chore: fix calling new method
Shinigami92 Oct 17, 2022
647b63b
test: revert datatype test file
Shinigami92 Oct 17, 2022
44fea50
chore: adjust minor things
Shinigami92 Oct 17, 2022
ea8f75a
refactor: rename string.random to string.sample
Shinigami92 Oct 17, 2022
7c459be
chore: revert signature changes
Shinigami92 Oct 17, 2022
2a0244b
refactor: rename count to length
Shinigami92 Oct 17, 2022
d8352c6
refactor: rename case to casing
Shinigami92 Oct 17, 2022
8a847b7
test: clean some casing to lower
Shinigami92 Oct 17, 2022
97131a6
chore: sort methods alphabetically
Shinigami92 Oct 17, 2022
e3df60e
test: nothing in string should depend on locale
Shinigami92 Oct 17, 2022
f54730f
chore: move sample max length to const
Shinigami92 Oct 17, 2022
a05e72c
docs: run pnpm run generate:api-docs
Shinigami92 Oct 17, 2022
1909555
test: use seeded tests factory
Shinigami92 Oct 17, 2022
2caf404
chore: apply review suggestion
Shinigami92 Oct 18, 2022
833f395
chore: apply review suggestion
Shinigami92 Oct 18, 2022
6d163b6
chore: reorder options
Shinigami92 Oct 18, 2022
42690f8
test: apply review suggestions
Shinigami92 Oct 18, 2022
c5deddd
test: remove unnecessary casing
Shinigami92 Oct 18, 2022
dc4255f
Update test/string.spec.ts
Shinigami92 Oct 18, 2022
f5e25f8
test: update snapshots
Shinigami92 Oct 18, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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