aboutsummaryrefslogtreecommitdiff
path: root/src/modules
diff options
context:
space:
mode:
authorPablo Ladreyt <[email protected]>2023-01-03 05:03:05 -0300
committerGitHub <[email protected]>2023-01-03 08:03:05 +0000
commitd3229fcdf28b5a1abdbc44a7bcdde934bd472bf2 (patch)
treecf0ac5d86fc02f564f3369f821945c6b8f036e0b /src/modules
parent84b3c20c7d4459e820bbce65b64201b29283b149 (diff)
downloadfaker-d3229fcdf28b5a1abdbc44a7bcdde934bd472bf2.tar.xz
faker-d3229fcdf28b5a1abdbc44a7bcdde934bd472bf2.zip
feat(number): add binary and octal random number generation (#1708)
Diffstat (limited to 'src/modules')
-rw-r--r--src/modules/number/index.ts58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/modules/number/index.ts b/src/modules/number/index.ts
index 54352fcd..6a5fd5d6 100644
--- a/src/modules/number/index.ts
+++ b/src/modules/number/index.ts
@@ -115,6 +115,64 @@ export class NumberModule {
}
/**
+ * Returns a [binary](https://en.wikipedia.org/wiki/Binary_number) number.
+ *
+ * @param options Maximum value or options object. Defaults to `{}`.
+ * @param options.min Lower bound for generated number. Defaults to `0`.
+ * @param options.max Upper bound for generated number. Defaults to `1`.
+ *
+ * @throws When options define `max < min`.
+ *
+ * @example
+ * faker.number.binary() // '1'
+ * faker.number.binary(255) // '110101'
+ * faker.number.binary({ min: 0, max: 65535 }) // '10110101'
+ *
+ * @since 8.0.0
+ */
+ binary(options: number | { min?: number; max?: number } = {}): string {
+ if (typeof options === 'number') {
+ options = { max: options };
+ }
+
+ const { min = 0, max = 1 } = options;
+
+ return this.int({
+ max,
+ min,
+ }).toString(2);
+ }
+
+ /**
+ * Returns an [octal](https://en.wikipedia.org/wiki/Octal) number.
+ *
+ * @param options Maximum value or options object. Defaults to `{}`.
+ * @param options.min Lower bound for generated number. Defaults to `0`.
+ * @param options.max Upper bound for generated number. Defaults to `7`.
+ *
+ * @throws When options define `max < min`.
+ *
+ * @example
+ * faker.number.octal() // '5'
+ * faker.number.octal(255) // '377'
+ * faker.number.octal({ min: 0, max: 65535 }) // '4766'
+ *
+ * @since 8.0.0
+ */
+ octal(options: number | { min?: number; max?: number } = {}): string {
+ if (typeof options === 'number') {
+ options = { max: options };
+ }
+
+ const { min = 0, max = 7 } = options;
+
+ return this.int({
+ max,
+ min,
+ }).toString(8);
+ }
+
+ /**
* Returns a lowercase [hexadecimal](https://en.wikipedia.org/wiki/Hexadecimal) number.
*
* @param options Maximum value or options object. Defaults to `{}`.