Skip to content

Commit

Permalink
fix(strings): starts/ends/includes fix and test
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAntix committed Feb 2, 2024
1 parent 16ee93a commit 50e459c
Show file tree
Hide file tree
Showing 21 changed files with 579 additions and 49 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@
"collectCoverageFrom": [
"src/**/*.ts"
],
"collectCoverage": true,
"moduleFileExtensions": [
"js",
"json",
Expand Down
5 changes: 5 additions & 0 deletions src/.spec/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* @file Automatically generated by barrelsby.
*/

export * from './stringOfLength';
3 changes: 3 additions & 0 deletions src/.spec/stringOfLength.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const stringOfLength
: (length: number) => string
= length => Array.from({ length }, (_, i) => i.toString()).join('');
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
*/

export * from './Is';
export * from './.spec/index';
export * from './parsing/index';
export * from './predicates/index';
6 changes: 3 additions & 3 deletions src/parsing/ParseErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ export class ParseErrors {
/** value includes */
static readonly matches = <T>(name: T) => ({ matches: name });
/** value includes */
static readonly includes = <T>(value: T, ignoreCase: boolean) => ({ includes: { value, ignoreCase } });
static readonly includes = <T>(value: T, ignoreCase: boolean = false) => ({ includes: { value, ignoreCase } });
/** starts with */
static readonly startsWith = <T>(value: T, ignoreCase: boolean) => ({ startsWith: { value, ignoreCase } });
static readonly startsWith = <T>(value: T, ignoreCase: boolean = false) => ({ startsWith: { value, ignoreCase } });
/** ends with */
static readonly endsWith = <T>(value: T, ignoreCase: boolean) => ({ endsWith: { value, ignoreCase } });
static readonly endsWith = <T>(value: T, ignoreCase: boolean = false) => ({ endsWith: { value, ignoreCase } });
/** values unique */
static readonly unique = { unique: true };
}
32 changes: 23 additions & 9 deletions src/parsing/dates/dates-parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,68 @@ import { ParseErrors } from '../ParseErrors';
import { DATE_SETTINGS } from './DATE_SETTINGS';

describe('dates-parser', () => {
const parser = Is.date;
const schema = Is.date;

it('success date', () => {
const value = new Date();
const result = parser.parse(value);
const result = schema.parse(value);

expect(result.errors).toEqual(ParseErrors.empty);
expect(result.value).toEqual(value);
});

it('success undefined', () => {
const result = parser.parse(undefined);
const result = schema.parse(undefined);

expect(result.errors).toEqual(ParseErrors.empty);
expect(result.value).toBe(null);
});

it('success null', () => {
const result = parser.parse(null);
const result = schema.parse(null);

expect(result.errors).toEqual(ParseErrors.empty);
expect(result.value).toBe(null);
});
it('success string', () => {
const value = '3000-01-02';
const result = parser.parse(value);
const result = schema.parse(value);

expect(result.errors).toEqual(ParseErrors.empty);
expect(result.value).toEqual(new Date(Date.parse(value)));
});

it('success string iso', () => {
const value = '2023-04-14T22:54:23.861073+01:00';
const result = parser.parse(value);
const result = schema.parse(value);

expect(result.errors).toEqual(ParseErrors.empty);
expect(result.value).toEqual(new Date(Date.parse(value)));
});

it('success string empty', () => {
const result = parser.parse('');
const result = schema.parse('');

expect(result.errors).toEqual(ParseErrors.empty);
expect(result.value).toBe(null);
});

it('failure not date', () => {
const result = parser.parse('a');
const result = schema.parse('a');

expect(result.value).toBe(null);
expect(result.errors).toEqual({ date: true });
});

it('failure not date object', () => {
const result = schema.parse({});

expect(result.value).toBe(null);
expect(result.errors).toEqual({ date: true });
});

it('failure not date NaN', () => {
const result = schema.parse(NaN);

expect(result.value).toBe(null);
expect(result.errors).toEqual({ date: true });
Expand All @@ -62,7 +76,7 @@ describe('dates-parser', () => {
DATE_SETTINGS.parseDayFirst = !DATE_SETTINGS.formatDayFirst;

const value = '1/2/2000';
const result = parser.parse(value);
const result = schema.parse(value);

expect(result.errors).toEqual(ParseErrors.empty);
expect(result.value).toEqual(new Date(2000, 1, 1));
Expand Down
14 changes: 7 additions & 7 deletions src/parsing/numbers/numbers-float.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,45 @@ import { Is } from '../../Is';
import { ParseErrors } from '../ParseErrors';

describe('numbers-float', () => {
const parser = Is.float;
const schema = Is.float;

it('success number', () => {
const result = parser.parse(1);
const result = schema.parse(1);

expect(result.errors).toEqual(ParseErrors.empty);
expect(result.value).toBe(1);
});

it('success undefined', () => {
const result = parser.parse(undefined);
const result = schema.parse(undefined);

expect(result.errors).toEqual(ParseErrors.empty);
expect(result.value).toBe(null);
});

it('success null', () => {
const result = parser.parse(null);
const result = schema.parse(null);

expect(result.errors).toEqual(ParseErrors.empty);
expect(result.value).toBe(null);
});

it('success string', () => {
const result = parser.parse('1');
const result = schema.parse('1');

expect(result.errors).toEqual(ParseErrors.empty);
expect(result.value).toBe(1);
});

it('success string empty', () => {
const result = parser.parse('');
const result = schema.parse('');

expect(result.errors).toEqual(ParseErrors.empty);
expect(result.value).toBe(null);
});

it('failure not number', () => {
const result = parser.parse('a');
const result = schema.parse('a');

expect(result.errors).toEqual({
float: true
Expand Down
16 changes: 8 additions & 8 deletions src/parsing/numbers/numbers-int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,52 @@ import { Is } from '../../Is';
import { ParseErrors } from '../ParseErrors';

describe('numbers-int', () => {
const parser = Is.int;
const schema = Is.int;

it('success number', () => {
const result = parser.parse(1);
const result = schema.parse(1);

expect(result.errors).toEqual(ParseErrors.empty);
expect(result.value).toBe(1);
});

it('success undefined', () => {
const result = parser.parse(undefined);
const result = schema.parse(undefined);

expect(result.errors).toEqual(ParseErrors.empty);
expect(result.value).toBe(null);
});

it('success null', () => {
const result = parser.parse(null);
const result = schema.parse(null);

expect(result.errors).toEqual(ParseErrors.empty);
expect(result.value).toBe(null);
});

it('success string', () => {
const result = parser.parse('1');
const result = schema.parse('1');

expect(result.errors).toEqual(ParseErrors.empty);
expect(result.value).toBe(1);
});

it('success string empty', () => {
const result = parser.parse('');
const result = schema.parse('');

expect(result.errors).toEqual(ParseErrors.empty);
expect(result.value).toBe(null);
});

it('failure not int (float)', () => {
const result = parser.parse(1.2);
const result = schema.parse(1.2);

expect(result.errors).toEqual(ParseErrors.int);
expect(result.value).toBe(null);
});

it('failure not number', () => {
const result = parser.parse('a');
const result = schema.parse('a');

expect(result.errors).toEqual(ParseErrors.int);
expect(result.value).toBe(null);
Expand Down
36 changes: 33 additions & 3 deletions src/parsing/numbers/numbers-max.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,51 @@ import { ParseErrors } from '../ParseErrors';

describe('numbers-max', () => {
const max = 10;
const maxSchema = Is.int.max(max);
const schema = Is.int.max(max);

it('success', () => {
const value = max - 1;
const result = maxSchema.parse(value);
const result = schema.parse(value);

expect(result.errors).toEqual(ParseErrors.empty);
expect(result.value).toBe(value);
});

it('failure', () => {
const value = max + 1;
const result = maxSchema.parse(value);
const result = schema.parse(value);

expect(result.errors).toEqual(ParseErrors.max(max, false));
expect(result.value).toBe(value);
});

it('failure', () => {
const exclusiveSchema = Is.int.max(max, true);
const value = max;
const result = exclusiveSchema.parse(value);

expect(result.errors).toEqual(ParseErrors.max(max, true));
expect(result.value).toBe(value);
});

describe('not', () => {
const notMaxSchema = Is.int.not.max(max);

it('success', () => {
const value = max + 1;
const result = notMaxSchema.parse(value);

expect(result.errors).toEqual(ParseErrors.empty);
expect(result.value).toBe(value);
});

it('failure', () => {
const value = max - 1;
const result = notMaxSchema.parse(value);

expect(result.errors).toEqual(ParseErrors.not(ParseErrors.max(max, false)));
expect(result.value).toBe(value);
});

});
});
39 changes: 34 additions & 5 deletions src/parsing/numbers/numbers-min.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,67 @@ import { ParseErrors } from '../ParseErrors';

describe('numbers-min', () => {
const min = 10;
const minSchema = Is.int.min(min);
const schema = Is.int.min(min);

it('success', () => {
const value = min + 1;
const result = minSchema.parse(value);
const result = schema.parse(value);

expect(result.errors).toEqual(ParseErrors.empty);
expect(result.value).toBe(value);
});

it('success string', () => {
const value = min + 1;
const result = minSchema.parse(`${value}`);
const result = schema.parse(`${value}`);

expect(result.errors).toEqual(ParseErrors.empty);
expect(result.value).toBe(value);
});

it('success null', () => {
const value = null;
const result = minSchema.parse(value);
const result = schema.parse(value);

expect(result.errors).toEqual(ParseErrors.empty);
expect(result.value).toBe(value);
});

it('failure', () => {
const value = min - 1;
const result = minSchema.parse(value);
const result = schema.parse(value);

expect(result.errors).toEqual(ParseErrors.min(min, false));
expect(result.value).toBe(value);
});

it('failure exclusive', () => {
const exclusiveSchema = Is.int.min(min, true);
const value = min;
const result = exclusiveSchema.parse(value);

expect(result.errors).toEqual(ParseErrors.min(min, true));
expect(result.value).toBe(value);
});

describe('not', () => {
const notMinSchema = Is.int.not.min(min);

it('success', () => {
const value = min - 1;
const result = notMinSchema.parse(value);

expect(result.errors).toEqual(ParseErrors.empty);
expect(result.value).toBe(value);
});

it('failure', () => {
const value = min + 1;
const result = notMinSchema.parse(value);

expect(result.errors).toEqual(ParseErrors.not(ParseErrors.min(min, false)));
expect(result.value).toBe(value);
});

});
});
14 changes: 14 additions & 0 deletions src/parsing/numbers/parseInt.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
describe('parseInt', () => {

it('success number', () => {
const result = parseInt('1');

expect(result).toBe(1);
});

it('success binary', () => {
const result = parseInt('101', 2);

expect(result).toBe(5);
});
});
Loading

0 comments on commit 50e459c

Please sign in to comment.