-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from snaplet/v1.1.0-relese
chore: v1.1.0 release
- Loading branch information
Showing
10 changed files
with
858 additions
and
11 deletions.
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
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
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const { v4: uuid } = require('uuid') | ||
const {copycat, fictional} = require('../dist/index') | ||
|
||
const TRANSFORMATIONS = { | ||
...fictional, | ||
...copycat, | ||
} | ||
|
||
const METHOD = process.env.METHOD ? process.env.METHOD : 'phoneNumber' | ||
const MAX_N = +(process.env.MAX_N ?? 999999) | ||
|
||
function main() { | ||
let firstColision = null | ||
let colisions = 0 | ||
const colide = new Set() | ||
for (let i = 0; i < MAX_N; i++) { | ||
const result = TRANSFORMATIONS[METHOD](uuid()) | ||
if (colide.has(result)) { | ||
colisions++ | ||
if (firstColision == null) { | ||
firstColision = i | ||
} | ||
} else { | ||
colide.add(result) | ||
} | ||
} | ||
console.log(`firstColision: ${firstColision} colided ${colisions} times`) | ||
} | ||
|
||
main() |
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
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
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 |
---|---|---|
@@ -1,7 +1,68 @@ | ||
import { join, char, times } from 'fictional' | ||
import { int, oneOf } from 'fictional' | ||
import { Input } from './types' | ||
|
||
export const phoneNumber = join('', [ | ||
'+', | ||
times([2, 3], char.digit), | ||
times([10, 12], char.digit), | ||
]) | ||
type PhoneNumberOptions = { | ||
/** | ||
* An array of prefixes to use when generating a phone number. | ||
* Can be used to generate a fictional phone number instead of a random one. | ||
* Using fictional phone numbers might make the generation slower. And might increase likelihood of collisions. | ||
* @example | ||
* ```ts | ||
* phoneNumber(seed, { | ||
* // Generate a French phone number within fictional phone number delimited range (cf: https://en.wikipedia.org/wiki/Fictitious_telephone_number) | ||
* prefixes: ['+3319900', '+3326191', '+3335301'], | ||
* // A french phone number is 11 digits long (including the prefix) so there is no need to generate a number longer than 4 digits | ||
* min: 1000, max: 9999 | ||
* }) | ||
* ``` | ||
* @example | ||
* | ||
* ```ts | ||
* phoneNumber(seed, { | ||
* // Generate a New Jersey fictional phone number | ||
* prefixes: ['+201555'], | ||
* min: 1000, max: 9999 | ||
* }) | ||
* ``` | ||
* @default undefined | ||
*/ | ||
prefixes?: Array<string> | ||
/** | ||
* The minimum number to generate. | ||
* @default 10000000000 | ||
*/ | ||
min?: number | ||
/** | ||
* The maximum number to generate. | ||
* @default 999999999999999 | ||
*/ | ||
max?: number | ||
} | ||
|
||
export const phoneNumber = ( | ||
input: Input, | ||
options: PhoneNumberOptions = { min: 10000000000, max: 999999999999999 } | ||
) => { | ||
// Use provided min and max, or default values if not provided | ||
const min = options.min ?? 10000000000 | ||
const max = options.max ?? 999999999999999 | ||
|
||
if (options.prefixes) { | ||
const prefix = | ||
options.prefixes.length > 1 | ||
? // If multiple prefixes are provided, pick one deterministically | ||
oneOf(input, options.prefixes) | ||
: options.prefixes[0] | ||
const prefixLength = prefix.length | ||
|
||
// Adjust min and max based on prefix length to keep a valid number of digits in the phone number | ||
const adjustedMin = Math.max(min, 10 ** (10 - prefixLength)) | ||
const adjustedMax = Math.min(max, 10 ** (15 - prefixLength) - 1) | ||
return `${prefix}${int(input, { | ||
min: adjustedMin, | ||
max: adjustedMax, | ||
})}` | ||
} | ||
|
||
return `+${int(input, { min, max })}` | ||
} |
Oops, something went wrong.