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

feat: πŸ”₯ First punt at priority option #223

Open
wants to merge 45 commits into
base: main
Choose a base branch
from
Open
Changes from 5 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
6160c3c
feat: πŸ”₯ First punt at priority option
Mar 16, 2022
485782e
Merge remote-tracking branch 'origin/main' into feat/220-unique-values
theryansmee Mar 22, 2022
6b57aa0
feat: πŸ”₯ Clean up fakFromArray logic
theryansmee Mar 22, 2022
713c77d
feat: πŸ”₯ Replace use of random index logic with getRandomInRange
theryansmee Mar 22, 2022
bbe7b0b
feat: πŸ”₯ Revert rand element logic for now
theryansmee Mar 22, 2022
7f69bad
feat: πŸ”₯ Allow devs to pass in comparison functions
theryansmee Mar 23, 2022
77831a5
feat: πŸ”₯ Add custom comparison functions non-conforming function
theryansmee Mar 23, 2022
96acb7e
Merge remote-tracking branch 'origin/main' into feat/220-unique-values
theryansmee Mar 24, 2022
af09284
feat: πŸ”₯ Add json check unique function. Clean up code
theryansmee Mar 24, 2022
6833592
feat: πŸ”₯ Add generic comparison function
Apr 1, 2022
13998f6
feat: πŸ”₯ Replace duplicate checkUnique logic with objectIsUnique
Apr 4, 2022
717bb7d
feat: πŸ”₯ Fix function name. Update file name
Apr 4, 2022
660f53a
test: πŸ₯³ Move error to snapshot
Apr 4, 2022
b18c227
Merge remote-tracking branch 'origin/main' into feat/220-unique-values
Apr 5, 2022
42a54d4
feat: πŸ”₯ Fix snapshot
Apr 5, 2022
0ec65be
feat: πŸ”₯ Move snapshots folder to tests/ route
Apr 5, 2022
ff6980a
feat: πŸ”₯ Clean up core file
Apr 5, 2022
d57bd25
feat: πŸ”₯ Add missing priority doc comment
Apr 5, 2022
fc5f8fc
test: πŸ₯³ Replace flakey regex
Apr 5, 2022
67c4f5f
Merge remote-tracking branch 'origin/main' into feat/220-unique-values
Apr 19, 2022
2b84b1a
feat: πŸ”₯ return empty array if length is 0
Apr 19, 2022
d4d1fa3
Merge remote-tracking branch 'origin/main' into feat/220-unique-values
May 3, 2022
32b84de
feat: πŸ”₯ clean up unique validators
May 3, 2022
6f7dcc6
Merge remote-tracking branch 'origin/main' into feat/220-unique-values
May 6, 2022
2d0aecc
feat: πŸ”₯ Add tests for unique validators
May 6, 2022
dbddca3
feat: πŸ”₯ Fix reversed logic and corresponding tests
May 6, 2022
2331772
feat: πŸ”₯ Combine uniqueComparer and comparisonKeys into config
May 10, 2022
b54d6b2
Merge remote-tracking branch 'origin/main' into feat/220-unique-values
May 10, 2022
39e7fa5
Merge remote-tracking branch 'origin/main' into feat/220-unique-values
May 12, 2022
e29cc7f
feat: πŸ”₯ Simplify priority documentation comment
May 12, 2022
641a875
feat: πŸ”₯ Tweak documentation comment to be priority specific
May 12, 2022
c48790a
feat: πŸ”₯ Revert package.lock
May 12, 2022
f29cca6
Merge remote-tracking branch 'origin/main' into feat/220-unique-values
May 23, 2022
e7dac07
Merge remote-tracking branch 'origin/main' into feat/220-unique-values
Jun 13, 2022
77edc54
feat: πŸ”₯ Fix merge conflicts in core
Jun 13, 2022
fee5cf6
Merge remote-tracking branch 'origin/main' into feat/220-unique-values
Sep 12, 2022
23f628e
fix: 🐞 Fix outdated tests
Sep 12, 2022
e2fb265
refactor: πŸ’‘ Remove commented out code
Sep 12, 2022
086ec7b
test: πŸ₯³ Add basic tests for core fake function
Sep 12, 2022
2e5b650
refactor: πŸ’‘ Code review changes
Sep 18, 2022
10cd7d0
refactor: πŸ’‘ Code review changes
Sep 18, 2022
274fdc9
refactor: πŸ’‘ Change request changes
Sep 18, 2022
1143091
refactor: πŸ’‘ Code review changes
Sep 18, 2022
2a037d0
Update packages/falso/src/lib/core/core.ts
theryansmee Sep 19, 2022
8869b35
refactor: πŸ’‘ Code review changes
Sep 19, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 66 additions & 7 deletions packages/falso/src/lib/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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),
Copy link
Member

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?

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are handling the length options both here and in the fake function, please choose one place

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use structuredClone instead.

Copy link
Collaborator Author

@theryansmee theryansmee Sep 19, 2022

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

Choose a reason for hiding this comment

The 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)];
}

Expand Down