-
-
Notifications
You must be signed in to change notification settings - Fork 120
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: π₯ First punt at priority option #223
base: main
Are you sure you want to change the base?
Changes from 5 commits
6160c3c
485782e
6b57aa0
713c77d
bbe7b0b
7f69bad
77831a5
96acb7e
af09284
6833592
13998f6
717bb7d
660f53a
b18c227
42a54d4
0ec65be
ff6980a
d57bd25
fc5f8fc
67c4f5f
2b84b1a
d4d1fa3
32b84de
6f7dcc6
2d0aecc
dbddca3
2331772
b54d6b2
39e7fa5
e29cc7f
641a875
c48790a
f29cca6
e7dac07
77edc54
fee5cf6
23f628e
e2fb265
086ec7b
2e5b650
10cd7d0
274fdc9
1143091
2a037d0
8869b35
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ import { random } from '../random'; | |
|
||
export interface FakeOptions { | ||
length?: number; | ||
priority?: 'length' | 'unique'; | ||
} | ||
|
||
type Return<T, O extends FakeOptions> = [O] extends [never] | ||
|
@@ -11,21 +12,79 @@ type Return<T, O extends FakeOptions> = [O] extends [never] | |
: T; | ||
|
||
export function fake<T, Options extends FakeOptions>( | ||
data: T[] | ((i: number) => T), | ||
data: T[] | (() => T), | ||
options?: Options | ||
): Return<T, Options> { | ||
const dataSource = Array.isArray(data) ? () => randElement(data) : data; | ||
if (Array.isArray(data)) { | ||
return fakeFromArray(data, options) as any; | ||
} | ||
|
||
return fakeFromFunction(data, options) as any; | ||
} | ||
|
||
export function fakeFromFunction<T, Options extends FakeOptions>( | ||
data: () => T, | ||
options?: Options | ||
) { | ||
if (!options?.length) { | ||
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. You are handling the length options both here and in the |
||
return data(); | ||
} | ||
|
||
const priority = options?.priority ?? 'length'; | ||
|
||
if (priority === 'length') { | ||
return Array.from({ length: options.length }, (_, index) => data()); | ||
} | ||
|
||
const items: T[] = []; | ||
|
||
let attempts = 0; | ||
const maxAttempts = options.length * 2; | ||
|
||
while (items.length < options.length && attempts < maxAttempts) { | ||
const item = data(); | ||
|
||
if (!items.includes(item)) { | ||
theryansmee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
items.push(item); | ||
} | ||
|
||
attempts++; | ||
} | ||
|
||
return items; | ||
} | ||
|
||
export function fakeFromArray<T, Options extends FakeOptions>( | ||
data: T[], | ||
options?: Options | ||
) { | ||
if (!options?.length) { | ||
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. same comment as above |
||
return dataSource(0) as any; | ||
return randElement(data); | ||
} | ||
|
||
const priority = options?.priority ?? 'length'; | ||
|
||
if (priority === 'length') { | ||
return Array.from({ length: options.length }, (_, index) => | ||
randElement(data) | ||
); | ||
} | ||
|
||
const clonedData: T[] = JSON.parse(JSON.stringify(data)); | ||
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. Use 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. @shaharkazaz - structuredClone isn't available in our version of node / typescript. We would need to upgrade - is this ok? 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. @NetanelBasal any objection to upgrading and releasing a new major? |
||
const newArray: T[] = []; | ||
|
||
while (clonedData.length && newArray.length !== options.length) { | ||
const randomIndex = Math.floor(random() * clonedData.length); | ||
const item = clonedData[randomIndex]; | ||
|
||
newArray.push(item); | ||
clonedData.splice(randomIndex, 1); | ||
} | ||
|
||
return Array.from({ length: options.length }, (_, index) => | ||
dataSource(index) | ||
) as any; | ||
return newArray; | ||
} | ||
|
||
export function randElement<T>(arr: T[]) { | ||
export function randElement<T>(arr: T[]): T { | ||
shaharkazaz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return arr[Math.floor(random() * arr.length)]; | ||
} | ||
|
||
|
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.
Why did you remove the
index
variable?