Skip to content

Commit

Permalink
feat: 🔥 add specialChars in charType
Browse files Browse the repository at this point in the history
developer can use special type to get special random value
  • Loading branch information
okinawaa authored and netanel-utila committed Nov 16, 2022
1 parent bcd6855 commit f799fa9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
13 changes: 10 additions & 3 deletions packages/falso/src/lib/sequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { random } from './random';

export const numericChars = '0123456789';
export const alphaChars = 'abcdefghijklmnopqrstuvwxyz';
// TODO
export const specialChars = 'TODO';
export const specialChars = '<>@!#$%^&*()_+[]{}?:;|\'"\\,./~`-=';
export const numericAlphaChars = `${numericChars}${alphaChars}${alphaChars.toUpperCase()}`;

function generator(size = 8, chars = numericAlphaChars) {
Expand All @@ -23,7 +22,7 @@ type RandomSequenceOptions = {

type RandomSequenceOptions2 = {
size?: number;
charType?: 'numeric' | 'alpha' | 'alphaNumeric'; // TODO | 'special'
charType?: 'numeric' | 'alpha' | 'alphaNumeric' | 'special';
} & FakeOptions;

/**
Expand Down Expand Up @@ -76,8 +75,16 @@ export function randSequence<
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);
}
} else {
return fake(() => generator(options?.size, options?.chars), options);
}
}

function neverChecker(value: never) {
throw new Error(`you didn't all condition ${value}`);
}
12 changes: 12 additions & 0 deletions packages/falso/src/tests/sequence.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { randSequence } from '../lib/sequence';
describe('randSequence', () => {
const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
const letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
const specials = '<>@!#$%^&*()_+[]{}?:;|\'"\\,./~`-=';

it('should create one sequence', () => {
expect(typeof randSequence()).toBe('string');
Expand Down Expand Up @@ -50,4 +51,15 @@ describe('randSequence', () => {
expect(typeof result).toBe('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
);
});
});

0 comments on commit f799fa9

Please sign in to comment.