Skip to content

Commit

Permalink
fix(typing): randSequence
Browse files Browse the repository at this point in the history
  • Loading branch information
kobenguyent committed Jan 7, 2025
1 parent e89b27b commit 2f2c44e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 34 deletions.
2 changes: 1 addition & 1 deletion packages/falso/src/lib/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type FactoryFunction<T> = (i: number) => T;
export function fake<T, Options extends FakeOptions>(
data: Readonly<T[]> | FactoryFunction<T>,
options?: Options
): Return<T, Options> {
): Return<T | T[], Options> {
let dataSource = data as FactoryFunction<T>;
if (Array.isArray(data)) {
let resolvedData = data;
Expand Down
56 changes: 33 additions & 23 deletions packages/falso/src/lib/sequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,36 +53,46 @@ type RandomSequenceOptions2 = {
*/
export function randSequence<Options extends RandomSequenceOptions = never>(
options?: RandomSequenceOptions
): Return<string, Options>;
): Return<string | string[], Options>;
export function randSequence<Options extends RandomSequenceOptions2 = never>(
options?: RandomSequenceOptions2
): Return<string, Options>;
): Return<string | string[], Options>;
export function randSequence<
Options extends RandomSequenceOptions & RandomSequenceOptions2 = never
>(options?: Options) {
if (options?.charType && !options?.chars) {
switch (options.charType) {
case 'alpha':
return fake(
() =>
generator(
options?.size,
`${alphaChars}${alphaChars.toUpperCase()}`
),
options
);
case 'alphaNumeric':
return fake(() => generator(options?.size, numericAlphaChars), options);
case 'numeric':
return fake(() => generator(options?.size, numericChars), options);
case 'special':
return fake(() => generator(options?.size, specialChars), options);
default:
return neverChecker(options.charType);
const generateSequence = () => {
if (options?.charType && !options?.chars) {
switch (options.charType) {
case 'alpha':
return generator(
options?.size,
`${alphaChars}${alphaChars.toUpperCase()}`
);
case 'alphaNumeric':
return generator(options?.size, numericAlphaChars);
case 'numeric':
return generator(options?.size, numericChars);
case 'special':
return generator(options?.size, specialChars);
default:
return neverChecker(options.charType);
}
} else {
return generator(options?.size, options?.chars);
}
} else {
return fake(() => generator(options?.size, options?.chars), options);
};

if (options?.length && options.length > 1) {
return fake(
() =>
Array.from({ length: options.length ?? 0 }, () =>
generateSequence()
) as string[],
options
);
}

return fake(generateSequence, options);
}

function neverChecker(value: never) {
Expand Down
34 changes: 24 additions & 10 deletions packages/falso/src/tests/sequence.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('randSequence', () => {

expect(Array.isArray(result)).toBe(true);
expect(result.length).toBe(10);
expect(result[0].length).toBe(8);
expect(result[0].length).toBe(10);
expect(result[0]).not.toEqual(result[1]);
});

Expand All @@ -24,17 +24,23 @@ describe('randSequence', () => {

expect(typeof result).toBe('string');
expect(result.length).toBe(10);
expect(result.split('').some((char) => letters.includes(char))).toBe(true);
if (typeof result === 'string') {
expect(result.split('').some((char) => letters.includes(char))).toBe(
true
);
}
});

it('should create sequence with charType: alpha', () => {
const result = randSequence({ charType: 'alpha', size: 10 });

expect(result.length).toBe(10);
expect(typeof result).toBe('string');
expect(
result.split('').some((char) => numbers.includes(parseInt(char)))
).toBe(false);
if (typeof result === 'string') {
expect(
result.split('').some((char) => numbers.includes(parseInt(char)))
).toBe(false);
}
});

it('should create sequence with charType: alphaNumeric', () => {
Expand All @@ -49,17 +55,25 @@ describe('randSequence', () => {

expect(result.length).toBe(8);
expect(typeof result).toBe('string');
expect(result.split('').some((char) => letters.includes(char))).toBe(false);
if (typeof result === 'string') {
expect(result.split('').some((char) => letters.includes(char))).toBe(
false
);
}
});

it('should create sequence with charType: special', () => {
const result = randSequence({ charType: 'special' });

expect(result.length).toBe(8);
expect(typeof result).toBe('string');
expect(result.split('').some((char) => letters.includes(char))).toBe(false);
expect(result.split('').every((char) => specials.includes(char))).toBe(
true
);
if (typeof result === 'string') {
expect(result.split('').some((char) => letters.includes(char))).toBe(
false
);
expect(result.split('').every((char) => specials.includes(char))).toBe(
true
);
}
});
});

0 comments on commit 2f2c44e

Please sign in to comment.