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

Swap allowLeadingZeros default value to true #1592

Closed
Shinigami92 opened this issue Nov 22, 2022 · 8 comments · Fixed by #1602
Closed

Swap allowLeadingZeros default value to true #1592

Shinigami92 opened this issue Nov 22, 2022 · 8 comments · Fixed by #1602
Assignees
Labels
breaking change Cannot be merged when next version is not a major release c: feature Request for new feature m: string Something is referring to the string module p: 1-normal Nothing urgent s: accepted Accepted feature / Confirmed bug

Comments

@Shinigami92
Copy link
Member

Clear and concise description of the problem

Often we have a call like faker.number.int(9) and then convert it to a string

Suggested solution

Implement a new method faker.string.digit() which returns a digit between [0, 9] and directly returns it as string

Alternative

No response

Additional context

Found in

@Shinigami92 Shinigami92 added c: feature Request for new feature m: string Something is referring to the string module labels Nov 22, 2022
@Shinigami92 Shinigami92 moved this to Todo in Faker Roadmap Nov 22, 2022
@ejcheng
Copy link
Member

ejcheng commented Nov 22, 2022

I can take this one

@ejcheng ejcheng self-assigned this Nov 22, 2022
@Shinigami92
Copy link
Member Author

I can take this one

Please wait until #1122 was merged

@Shinigami92 Shinigami92 added the s: on hold Blocked by something or frozen to avoid conflicts label Nov 22, 2022
@ST-DDT ST-DDT added p: 1-normal Nothing urgent s: needs decision Needs team/maintainer decision labels Nov 22, 2022
@ST-DDT
Copy link
Member

ST-DDT commented Nov 22, 2022

I would rather change the spec for string.numeric to allow returning '0' when length is 1.

@Shinigami92
Copy link
Member Author

Maybe lets make a benchmark of using string.numeric({ allowLeadingZeros: true }) compared to string.digit() which just uses number.int under the hood

numeric(
options:
| number
| {
length?: number | { min: number; max: number };
allowLeadingZeros?: boolean;
exclude?: readonly LiteralUnion<NumericChar>[] | string;
} = {}
): string {
if (typeof options === 'number') {
options = {
length: options,
};
}
const length = this.faker.helpers.rangeToNumber(options.length ?? 1);
if (length <= 0) {
return '';
}
const { allowLeadingZeros = false } = options;
let { exclude = [] } = options;
if (typeof exclude === 'string') {
exclude = exclude.split('');
}
const allowedDigits = DIGIT_CHARS.filter(
(digit) => !exclude.includes(digit)
);
if (
allowedDigits.length === 0 ||
(allowedDigits.length === 1 &&
!allowLeadingZeros &&
allowedDigits[0] === '0')
) {
throw new FakerError(
'Unable to generate numeric string, because all possible digits are excluded.'
);
}
let result = '';
if (!allowLeadingZeros && !exclude.includes('0')) {
result += this.faker.helpers.arrayElement(
allowedDigits.filter((digit) => digit !== '0')
);
}
while (result.length < length) {
result += this.faker.helpers.arrayElement(allowedDigits);
}
return result;
}

@ST-DDT
Copy link
Member

ST-DDT commented Nov 23, 2022

IMO we dont have to us a benchmark as long as it good enough. Otherwise we have to measure all our methods and reimplement them accordingly.

@Shinigami92
Copy link
Member Author

I would rather change the spec for string.numeric to allow returning '0' when length is 1.

Why do you want to confuse users with another behavior if the length input differs from one case to another case?
IMO it is a bit hard for the user to exactly know that they need to read the docs to find out that the behavior differs based of the used number passed into one and the same input parameter.

@ST-DDT
Copy link
Member

ST-DDT commented Nov 23, 2022

0001 isnt a real world number, but 0 is.
IMO this doesnt even need to be mentioned in the jsdocs.

@Shinigami92
Copy link
Member Author

Team Decision:

  • Do not introduce new method digit
  • Swap allowLeadingZeros parameter of string.numeric to default: true
  • _clean up all out internal calls as they don't need allowLeadingZeros anymore
  • After release, lets see if feedback from community comes back that this affected them

@Shinigami92 Shinigami92 removed the s: needs decision Needs team/maintainer decision label Nov 24, 2022
@Shinigami92 Shinigami92 changed the title Add new method string.digit Swap allowLeadingZeros default value to true Nov 24, 2022
@Shinigami92 Shinigami92 added breaking change Cannot be merged when next version is not a major release s: accepted Accepted feature / Confirmed bug and removed s: on hold Blocked by something or frozen to avoid conflicts labels Nov 24, 2022
@ejcheng ejcheng moved this from Todo to Awaiting Review in Faker Roadmap Nov 25, 2022
Repository owner moved this from Awaiting Review to Done in Faker Roadmap Dec 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change Cannot be merged when next version is not a major release c: feature Request for new feature m: string Something is referring to the string module p: 1-normal Nothing urgent s: accepted Accepted feature / Confirmed bug
Projects
No open projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants