Skip to content

Commit

Permalink
feat: πŸ”₯ First punt at priority option
Browse files Browse the repository at this point in the history
Allow user to set length priority to length or unique (POC)

βœ… Closes: #220
  • Loading branch information
Ryan Smee committed Mar 16, 2022
1 parent a983718 commit 6160c3c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Create massive amounts of fake data in the browser and NodeJS. Tree Shakeable &

</p>

βœ… &nbsp;192 Functions
βœ… &nbsp;193 Functions
βœ… &nbsp;Tree Shakable
βœ… &nbsp;Fully Typed
βœ… &nbsp;Entity Functions
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ title: Getting Started
/>
</p>

βœ… &nbsp;192 Functions
βœ… &nbsp;193 Functions
βœ… &nbsp;Tree Shakable
βœ… &nbsp;Fully Typed
βœ… &nbsp;Entity Functions
Expand Down
75 changes: 69 additions & 6 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,83 @@ 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) {
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) {
if (attempts >= maxAttempts) {
break;
}

const item = data();

if (!items.includes(item)) {
items.push(item);
}

attempts++;
}

return items;
}

export function fakeFromArray<T, Options extends FakeOptions>(
data: T[],
options?: Options
) {
if (!options?.length) {
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));
return Array.from({ length: options.length }, (_, index) =>
dataSource(index)
) as any;
randUniqueElement(clonedData)
).filter((item) => item);
}

export function randUniqueElement<T>(arr: T[]): T {
const index = Math.floor(random() * arr.length);
const item = arr[index];
arr.splice(index, 1);

return item;
}

export function randElement<T>(arr: T[]) {
export function randElement<T>(arr: T[]): T {
return arr[Math.floor(random() * arr.length)];
}

Expand Down

0 comments on commit 6160c3c

Please sign in to comment.