-
-
Notifications
You must be signed in to change notification settings - Fork 963
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: reimplement datatype.bigInt #791
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
46bf568
feat: reimplement datatype.bigInt
Shinigami92 fa3f4ce
Merge branch 'main' into 348-improve-datatype-bigint
Shinigami92 aef9644
Merge branch 'main' into 348-improve-datatype-bigint
Shinigami92 24745a7
Merge branch 'main' into 348-improve-datatype-bigint
Shinigami92 35a5680
Merge branch 'main' into 348-improve-datatype-bigint
Shinigami92 7b2fa87
Merge branch 'main' into 348-improve-datatype-bigint
Shinigami92 9cd8b56
Merge branch 'main' into 348-improve-datatype-bigint
Shinigami92 a846ac9
Merge branch 'main' into 348-improve-datatype-bigint
prisis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -261,20 +261,59 @@ export class Datatype { | |
/** | ||
* Returns a [BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#bigint_type) number. | ||
* | ||
* @param value When provided, this method simply converts it to `BigInt` type. | ||
* @param options Maximum value or options object. | ||
* @param options.min Lower bound for generated bigint. Defaults to `0n`. | ||
* @param options.max Upper bound for generated bigint. Defaults to `min + 999999999999999n`. | ||
* | ||
* @throws When options define `max < min`. | ||
* | ||
* @example | ||
* faker.datatype.bigInt() // 8507209999914928n | ||
* faker.datatype.bigInt('156') // 156n | ||
* faker.datatype.bigInt(30) // 30n | ||
* faker.datatype.bigInt(3000n) // 3000n | ||
* faker.datatype.bigInt(true) // 1n | ||
* faker.datatype.bigInt() // 55422n | ||
* faker.datatype.bigInt(100n) // 52n | ||
* faker.datatype.bigInt({ min: 1000000n }) // 431433n | ||
* faker.datatype.bigInt({ max: 100n }) // 42n | ||
* faker.datatype.bigInt({ min: 10n, max: 100n }) // 36n | ||
*/ | ||
bigInt(value?: string | number | bigint | boolean): bigint { | ||
if (value === undefined) { | ||
value = Math.floor(this.number() * 99999999999) + 10000000000; | ||
bigInt( | ||
options?: | ||
| bigint | ||
| boolean | ||
| number | ||
| string | ||
| { | ||
min?: bigint | boolean | number | string; | ||
max?: bigint | boolean | number | string; | ||
} | ||
): bigint { | ||
let min: bigint; | ||
let max: bigint; | ||
|
||
if (typeof options === 'object') { | ||
min = BigInt(options.min ?? 0); | ||
max = BigInt(options.max ?? min + BigInt(999999999999999)); | ||
} else { | ||
min = BigInt(0); | ||
max = BigInt(options ?? 999999999999999); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why can you pass I've never worked with BigInt so sorry if this makes no sense. |
||
} | ||
|
||
return BigInt(value); | ||
if (max === min) { | ||
return min; | ||
} | ||
|
||
if (max < min) { | ||
throw new FakerError(`Max ${max} should be larger then min ${min}.`); | ||
} | ||
|
||
const delta = max - min; | ||
|
||
const offset = | ||
BigInt( | ||
this.faker.random.numeric(delta.toString(10).length, { | ||
allowLeadingZeros: true, | ||
}) | ||
) % | ||
(delta + BigInt(1)); | ||
|
||
return min + offset; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What would
faker.datatype.bigInt(false)
return? I don't understand what advantage we have from accepting a boolean parameter.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's just the official constructor argument