From 50e459c37421d3c6ef9c6eadd864d020e7e33570 Mon Sep 17 00:00:00 2001 From: MrAntix Date: Fri, 2 Feb 2024 18:26:50 +0000 Subject: [PATCH] fix(strings): starts/ends/includes fix and test --- package.json | 1 - src/.spec/index.ts | 5 ++ src/.spec/stringOfLength.ts | 3 + src/index.ts | 1 + src/parsing/ParseErrors.ts | 6 +- src/parsing/dates/dates-parser.spec.ts | 32 +++++--- src/parsing/numbers/numbers-float.spec.ts | 14 ++-- src/parsing/numbers/numbers-int.spec.ts | 16 ++-- src/parsing/numbers/numbers-max.spec.ts | 36 ++++++++- src/parsing/numbers/numbers-min.spec.ts | 39 ++++++++-- src/parsing/numbers/parseInt.spec.ts | 14 ++++ src/parsing/strings/provideEndsWithString.ts | 8 +- src/parsing/strings/provideIncludesString.ts | 14 ++-- .../strings/provideStartsWithString.ts | 6 +- src/parsing/strings/strings-any-of.spec.ts | 77 +++++++++++++++++++ src/parsing/strings/strings-ends-with.spec.ts | 77 +++++++++++++++++++ src/parsing/strings/strings-includes.spec.ts | 77 +++++++++++++++++++ .../strings/strings-max-length.spec.ts | 54 +++++++++++++ .../strings/strings-min-length.spec.ts | 54 +++++++++++++ .../strings/strings-starts-with.spec.ts | 77 +++++++++++++++++++ tsconfig.json | 17 +++- 21 files changed, 579 insertions(+), 49 deletions(-) create mode 100644 src/.spec/index.ts create mode 100644 src/.spec/stringOfLength.ts create mode 100644 src/parsing/numbers/parseInt.spec.ts create mode 100644 src/parsing/strings/strings-any-of.spec.ts create mode 100644 src/parsing/strings/strings-ends-with.spec.ts create mode 100644 src/parsing/strings/strings-includes.spec.ts create mode 100644 src/parsing/strings/strings-max-length.spec.ts create mode 100644 src/parsing/strings/strings-min-length.spec.ts create mode 100644 src/parsing/strings/strings-starts-with.spec.ts diff --git a/package.json b/package.json index 5ce7949..4be1384 100644 --- a/package.json +++ b/package.json @@ -75,7 +75,6 @@ "collectCoverageFrom": [ "src/**/*.ts" ], - "collectCoverage": true, "moduleFileExtensions": [ "js", "json", diff --git a/src/.spec/index.ts b/src/.spec/index.ts new file mode 100644 index 0000000..4f2cf44 --- /dev/null +++ b/src/.spec/index.ts @@ -0,0 +1,5 @@ +/** + * @file Automatically generated by barrelsby. + */ + +export * from './stringOfLength'; diff --git a/src/.spec/stringOfLength.ts b/src/.spec/stringOfLength.ts new file mode 100644 index 0000000..5335b26 --- /dev/null +++ b/src/.spec/stringOfLength.ts @@ -0,0 +1,3 @@ +export const stringOfLength + : (length: number) => string + = length => Array.from({ length }, (_, i) => i.toString()).join(''); diff --git a/src/index.ts b/src/index.ts index 9fc9bc5..6411784 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,5 +3,6 @@ */ export * from './Is'; +export * from './.spec/index'; export * from './parsing/index'; export * from './predicates/index'; diff --git a/src/parsing/ParseErrors.ts b/src/parsing/ParseErrors.ts index faacd21..b11b29a 100644 --- a/src/parsing/ParseErrors.ts +++ b/src/parsing/ParseErrors.ts @@ -40,11 +40,11 @@ export class ParseErrors { /** value includes */ static readonly matches = (name: T) => ({ matches: name }); /** value includes */ - static readonly includes = (value: T, ignoreCase: boolean) => ({ includes: { value, ignoreCase } }); + static readonly includes = (value: T, ignoreCase: boolean = false) => ({ includes: { value, ignoreCase } }); /** starts with */ - static readonly startsWith = (value: T, ignoreCase: boolean) => ({ startsWith: { value, ignoreCase } }); + static readonly startsWith = (value: T, ignoreCase: boolean = false) => ({ startsWith: { value, ignoreCase } }); /** ends with */ - static readonly endsWith = (value: T, ignoreCase: boolean) => ({ endsWith: { value, ignoreCase } }); + static readonly endsWith = (value: T, ignoreCase: boolean = false) => ({ endsWith: { value, ignoreCase } }); /** values unique */ static readonly unique = { unique: true }; } diff --git a/src/parsing/dates/dates-parser.spec.ts b/src/parsing/dates/dates-parser.spec.ts index af92938..6b9a917 100644 --- a/src/parsing/dates/dates-parser.spec.ts +++ b/src/parsing/dates/dates-parser.spec.ts @@ -3,32 +3,32 @@ 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))); @@ -36,21 +36,35 @@ describe('dates-parser', () => { 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 }); @@ -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)); diff --git a/src/parsing/numbers/numbers-float.spec.ts b/src/parsing/numbers/numbers-float.spec.ts index 98a3fba..84539f2 100644 --- a/src/parsing/numbers/numbers-float.spec.ts +++ b/src/parsing/numbers/numbers-float.spec.ts @@ -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 diff --git a/src/parsing/numbers/numbers-int.spec.ts b/src/parsing/numbers/numbers-int.spec.ts index 93e6ecf..fca8bdb 100644 --- a/src/parsing/numbers/numbers-int.spec.ts +++ b/src/parsing/numbers/numbers-int.spec.ts @@ -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); diff --git a/src/parsing/numbers/numbers-max.spec.ts b/src/parsing/numbers/numbers-max.spec.ts index 0e9974b..68406eb 100644 --- a/src/parsing/numbers/numbers-max.spec.ts +++ b/src/parsing/numbers/numbers-max.spec.ts @@ -3,11 +3,11 @@ 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); @@ -15,9 +15,39 @@ describe('numbers-max', () => { 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); + }); + + }); }); diff --git a/src/parsing/numbers/numbers-min.spec.ts b/src/parsing/numbers/numbers-min.spec.ts index 27f0974..a59ddfb 100644 --- a/src/parsing/numbers/numbers-min.spec.ts +++ b/src/parsing/numbers/numbers-min.spec.ts @@ -3,11 +3,11 @@ 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); @@ -15,7 +15,7 @@ describe('numbers-min', () => { 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); @@ -23,7 +23,7 @@ describe('numbers-min', () => { 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); @@ -31,10 +31,39 @@ describe('numbers-min', () => { 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); + }); + + }); }); diff --git a/src/parsing/numbers/parseInt.spec.ts b/src/parsing/numbers/parseInt.spec.ts new file mode 100644 index 0000000..a7e7886 --- /dev/null +++ b/src/parsing/numbers/parseInt.spec.ts @@ -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); + }); +}); \ No newline at end of file diff --git a/src/parsing/strings/provideEndsWithString.ts b/src/parsing/strings/provideEndsWithString.ts index 4ac841f..7041c23 100644 --- a/src/parsing/strings/provideEndsWithString.ts +++ b/src/parsing/strings/provideEndsWithString.ts @@ -4,7 +4,7 @@ import { IParseResult } from '../IParseResult'; import { ParseErrors } from '../ParseErrors'; export function provideEndsWithString( - endswithValue: string, ignoreCase :boolean, negate:boolean + endswithValue: string, ignoreCase: boolean, negate: boolean ) { return (value: string): IParseResult => { @@ -17,6 +17,10 @@ export function provideEndsWithString( if (a.endsWith(b) !== negate) return createParseResult(value); - return createParseResult(value, ParseErrors.endsWith(value, ignoreCase)); + const errors = negate + ? ParseErrors.not(ParseErrors.endsWith(endswithValue)) + : ParseErrors.endsWith(endswithValue); + + return createParseResult(value, errors); }; } diff --git a/src/parsing/strings/provideIncludesString.ts b/src/parsing/strings/provideIncludesString.ts index 651f23e..866a3b4 100644 --- a/src/parsing/strings/provideIncludesString.ts +++ b/src/parsing/strings/provideIncludesString.ts @@ -11,14 +11,16 @@ export function provideIncludesString( if (isNullOrEmpty(value)) return createParseResult(null); - if (ignoreCase) { - value = value.toLowerCase(); - includesValue = includesValue.toLowerCase(); - } + const a = ignoreCase ? value.toLowerCase() : value; + const b = ignoreCase ? includesValue.toLowerCase() : includesValue; - if (value.includes(includesValue) !== negate) + if (a.includes(b) !== negate) return createParseResult(value); - return createParseResult(value, ParseErrors.includes(value, ignoreCase)); + const errors = negate + ? ParseErrors.not(ParseErrors.includes(includesValue)) + : ParseErrors.includes(includesValue); + + return createParseResult(value, errors); }; } diff --git a/src/parsing/strings/provideStartsWithString.ts b/src/parsing/strings/provideStartsWithString.ts index 024a0f8..d4808eb 100644 --- a/src/parsing/strings/provideStartsWithString.ts +++ b/src/parsing/strings/provideStartsWithString.ts @@ -17,6 +17,10 @@ export function provideStartsWithString( if (a.startsWith(b) !== negate) return createParseResult(value); - return createParseResult(value, ParseErrors.startsWith(value, ignoreCase)); + const errors = negate + ? ParseErrors.not(ParseErrors.startsWith(startswithValue)) + : ParseErrors.startsWith(startswithValue); + + return createParseResult(value, errors); }; } diff --git a/src/parsing/strings/strings-any-of.spec.ts b/src/parsing/strings/strings-any-of.spec.ts new file mode 100644 index 0000000..a667c70 --- /dev/null +++ b/src/parsing/strings/strings-any-of.spec.ts @@ -0,0 +1,77 @@ +import { Is } from '../../Is'; +import { ParseErrors } from '../ParseErrors'; + +describe('strings-any-of', () => { + const strings = ['a', 'b']; + const schama = Is.string.anyOf(strings); + + it('success', () => { + const value = strings[0]; + const result = schama.parse(value); + + expect(result.errors).toEqual(ParseErrors.empty); + expect(result.value).toBe(value); + }); + + it('success null', () => { + const result = schama.parse(null); + + expect(result.errors).toEqual(ParseErrors.empty); + expect(result.value).toBe(null); + }); + + it('success undefined', () => { + const result = schama.parse(undefined); + + expect(result.errors).toEqual(ParseErrors.empty); + expect(result.value).toBe(null); + }); + + it('success empty', () => { + const result = schama.parse(''); + + expect(result.errors).toEqual(ParseErrors.empty); + expect(result.value).toBe(null); + }); + + it('failure', () => { + const value = 'o'; + const result = schama.parse(value); + + expect(result.errors).toEqual(ParseErrors.anyOf(strings)); + expect(result.value).toBe(value); + }); + + describe('ignore case', () => { + + it('success', () => { + const ignoreCaseSchama = Is.string.anyOf(strings, true); + const value = strings[0].toUpperCase(); + const result = ignoreCaseSchama.parse(value); + + expect(result.errors).toEqual(ParseErrors.empty); + expect(result.value).toBe(value); + }); + + }); + + describe('not', () => { + const notSchama = Is.string.not.anyOf(strings); + + it('success', () => { + const value = 'o'; + const result = notSchama.parse(value); + + expect(result.errors).toEqual(ParseErrors.empty); + expect(result.value).toBe(value); + }); + + it('failure', () => { + const value = strings[0]; + const result = notSchama.parse(value); + + expect(result.errors).toEqual(ParseErrors.not(ParseErrors.anyOf(strings))); + expect(result.value).toBe(value); + }); + }); +}); diff --git a/src/parsing/strings/strings-ends-with.spec.ts b/src/parsing/strings/strings-ends-with.spec.ts new file mode 100644 index 0000000..ead633e --- /dev/null +++ b/src/parsing/strings/strings-ends-with.spec.ts @@ -0,0 +1,77 @@ +import { Is } from '../../Is'; +import { ParseErrors } from '../ParseErrors'; + +describe('strings-ends-with', () => { + const endsWith = 'end'; + const schama = Is.string.endsWith(endsWith); + + it('success', () => { + const value = 'o' + endsWith; + const result = schama.parse(value); + + expect(result.errors).toEqual(ParseErrors.empty); + expect(result.value).toBe(value); + }); + + it('success null', () => { + const result = schama.parse(null); + + expect(result.errors).toEqual(ParseErrors.empty); + expect(result.value).toBe(null); + }); + + it('success undefined', () => { + const result = schama.parse(undefined); + + expect(result.errors).toEqual(ParseErrors.empty); + expect(result.value).toBe(null); + }); + + it('success empty', () => { + const result = schama.parse(''); + + expect(result.errors).toEqual(ParseErrors.empty); + expect(result.value).toBe(null); + }); + + it('failure', () => { + const value = 'o'; + const result = schama.parse(value); + + expect(result.errors).toEqual(ParseErrors.endsWith(endsWith)); + expect(result.value).toBe(value); + }); + + describe('ignore case', () => { + + it('success', () => { + const ignoreCaseSchama = Is.string.endsWith(endsWith, true); + const value = 'o' + endsWith.toUpperCase(); + const result = ignoreCaseSchama.parse(value); + + expect(result.errors).toEqual(ParseErrors.empty); + expect(result.value).toBe(value); + }); + + }); + + describe('not', () => { + const notSchama = Is.string.not.endsWith(endsWith); + + it('success', () => { + const value = 'o'; + const result = notSchama.parse(value); + + expect(result.errors).toEqual(ParseErrors.empty); + expect(result.value).toBe(value); + }); + + it('failure', () => { + const value = 'o' + endsWith; + const result = notSchama.parse(value); + + expect(result.errors).toEqual(ParseErrors.not(ParseErrors.endsWith(endsWith))); + expect(result.value).toBe(value); + }); + }); +}); diff --git a/src/parsing/strings/strings-includes.spec.ts b/src/parsing/strings/strings-includes.spec.ts new file mode 100644 index 0000000..9ab53ed --- /dev/null +++ b/src/parsing/strings/strings-includes.spec.ts @@ -0,0 +1,77 @@ +import { Is } from '../../Is'; +import { ParseErrors } from '../ParseErrors'; + +describe('strings-includes', () => { + const includes = 'included'; + const schama = Is.string.includes(includes); + + it('success', () => { + const value = 'o' + includes + 'o'; + const result = schama.parse(value); + + expect(result.errors).toEqual(ParseErrors.empty); + expect(result.value).toBe(value); + }); + + it('success null', () => { + const result = schama.parse(null); + + expect(result.errors).toEqual(ParseErrors.empty); + expect(result.value).toBe(null); + }); + + it('success undefined', () => { + const result = schama.parse(undefined); + + expect(result.errors).toEqual(ParseErrors.empty); + expect(result.value).toBe(null); + }); + + it('success empty', () => { + const result = schama.parse(''); + + expect(result.errors).toEqual(ParseErrors.empty); + expect(result.value).toBe(null); + }); + + it('failure', () => { + const value = 'o'; + const result = schama.parse(value); + + expect(result.errors).toEqual(ParseErrors.includes(includes)); + expect(result.value).toBe(value); + }); + + describe('ignore case', () => { + + it('success', () => { + const ignoreCaseSchama = Is.string.includes(includes, true); + const value = 'o' + includes.toUpperCase() + 'o'; + const result = ignoreCaseSchama.parse(value); + + expect(result.errors).toEqual(ParseErrors.empty); + expect(result.value).toBe(value); + }); + + }); + + describe('not', () => { + const notSchama = Is.string.not.includes(includes); + + it('success', () => { + const value = 'o'; + const result = notSchama.parse(value); + + expect(result.errors).toEqual(ParseErrors.empty); + expect(result.value).toBe(value); + }); + + it('failure', () => { + const value = 'o' + includes + 'o'; + const result = notSchama.parse(value); + + expect(result.errors).toEqual(ParseErrors.not(ParseErrors.includes(includes))); + expect(result.value).toBe(value); + }); + }); +}); diff --git a/src/parsing/strings/strings-max-length.spec.ts b/src/parsing/strings/strings-max-length.spec.ts new file mode 100644 index 0000000..5843419 --- /dev/null +++ b/src/parsing/strings/strings-max-length.spec.ts @@ -0,0 +1,54 @@ +import { Is } from '../../Is'; +import { ParseErrors } from '../ParseErrors'; +import { stringOfLength } from '../../.spec/stringOfLength'; + +describe('strings-max-length', () => { + const max = 5; + const schama = Is.string.maxLength(max); + + it('success', () => { + const value = stringOfLength(max - 1); + const result = schama.parse(value); + + expect(result.errors).toEqual(ParseErrors.empty); + expect(result.value).toBe(value); + }); + + it('failure', () => { + const value = stringOfLength(max + 1); + const result = schama.parse(value); + + expect(result.errors).toEqual(ParseErrors.maxLength(max)); + expect(result.value).toBe(value); + }); + + it('failure exclusive', () => { + const exclusiveSchama = Is.string.maxLength(max, true); + const value = stringOfLength(max); + const result = exclusiveSchama.parse(value); + + expect(result.errors).toEqual(ParseErrors.maxLength(max)); + expect(result.value).toBe(value); + }); + + describe('not', () => { + const notSchama = Is.string.not.maxLength(max); + + it('success', () => { + const value = stringOfLength(max + 1); + const result = notSchama.parse(value); + + expect(result.errors).toEqual(ParseErrors.empty); + expect(result.value).toBe(value); + }); + + it('failure', () => { + const value = stringOfLength(max - 1); + const result = notSchama.parse(value); + + expect(result.errors).toEqual(ParseErrors.not(ParseErrors.maxLength(max))); + expect(result.value).toBe(value); + }); + + }); +}); \ No newline at end of file diff --git a/src/parsing/strings/strings-min-length.spec.ts b/src/parsing/strings/strings-min-length.spec.ts new file mode 100644 index 0000000..c2cbc34 --- /dev/null +++ b/src/parsing/strings/strings-min-length.spec.ts @@ -0,0 +1,54 @@ +import { Is } from '../../Is'; +import { ParseErrors } from '../ParseErrors'; +import { stringOfLength } from '../../.spec/stringOfLength'; + +describe('strings-min-length', () => { + const min = 5; + const schama = Is.string.minLength(min); + + it('success', () => { + const value = stringOfLength(min + 1); + const result = schama.parse(value); + + expect(result.errors).toEqual(ParseErrors.empty); + expect(result.value).toBe(value); + }); + + it('failure', () => { + const value = stringOfLength(min - 1); + const result = schama.parse(value); + + expect(result.errors).toEqual(ParseErrors.minLength(min)); + expect(result.value).toBe(value); + }); + + it('failure exclusive', () => { + const exclusiveSchama = Is.string.minLength(min, true); + const value = stringOfLength(min); + const result = exclusiveSchama.parse(value); + + expect(result.errors).toEqual(ParseErrors.minLength(min)); + expect(result.value).toBe(value); + }); + + describe('not', () => { + const notSchama = Is.string.not.minLength(min); + + it('success', () => { + const value = stringOfLength(min - 1); + const result = notSchama.parse(value); + + expect(result.errors).toEqual(ParseErrors.empty); + expect(result.value).toBe(value); + }); + + it('failure', () => { + const value = stringOfLength(min + 1); + const result = notSchama.parse(value); + + expect(result.errors).toEqual(ParseErrors.not(ParseErrors.minLength(min))); + expect(result.value).toBe(value); + }); + + }); +}); \ No newline at end of file diff --git a/src/parsing/strings/strings-starts-with.spec.ts b/src/parsing/strings/strings-starts-with.spec.ts new file mode 100644 index 0000000..745eef2 --- /dev/null +++ b/src/parsing/strings/strings-starts-with.spec.ts @@ -0,0 +1,77 @@ +import { Is } from '../../Is'; +import { ParseErrors } from '../ParseErrors'; + +describe('strings-starts-with', () => { + const startsWith = 'end'; + const schama = Is.string.startsWith(startsWith); + + it('success', () => { + const value = startsWith + 'o'; + const result = schama.parse(value); + + expect(result.errors).toEqual(ParseErrors.empty); + expect(result.value).toBe(value); + }); + + it('success null', () => { + const result = schama.parse(null); + + expect(result.errors).toEqual(ParseErrors.empty); + expect(result.value).toBe(null); + }); + + it('success undefined', () => { + const result = schama.parse(undefined); + + expect(result.errors).toEqual(ParseErrors.empty); + expect(result.value).toBe(null); + }); + + it('success empty', () => { + const result = schama.parse(''); + + expect(result.errors).toEqual(ParseErrors.empty); + expect(result.value).toBe(null); + }); + + it('failure', () => { + const value = 'o'; + const result = schama.parse(value); + + expect(result.errors).toEqual(ParseErrors.startsWith(startsWith)); + expect(result.value).toBe(value); + }); + + describe('ignore case', () => { + + it('success', () => { + const ignoreCaseSchama = Is.string.startsWith(startsWith, true); + const value = startsWith.toUpperCase() + 'o'; + const result = ignoreCaseSchama.parse(value); + + expect(result.errors).toEqual(ParseErrors.empty); + expect(result.value).toBe(value); + }); + + }); + + describe('not', () => { + const notSchama = Is.string.not.startsWith(startsWith); + + it('success', () => { + const value = 'o'; + const result = notSchama.parse(value); + + expect(result.errors).toEqual(ParseErrors.empty); + expect(result.value).toBe(value); + }); + + it('failure', () => { + const value = startsWith + '0'; + const result = notSchama.parse(value); + + expect(result.errors).toEqual(ParseErrors.not(ParseErrors.startsWith(startsWith))); + expect(result.value).toBe(value); + }); + }); +}); diff --git a/tsconfig.json b/tsconfig.json index 4062ef3..16ebe1e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,9 @@ { "compilerOptions": { - "lib": ["dom", "es2017"], + "lib": [ + "dom", + "es2017" + ], "moduleResolution": "node", "module": "esnext", "target": "es2017", @@ -14,6 +17,12 @@ "noUnusedLocals": true, "noUnusedParameters": true }, - "include": ["src/**/*.ts"], - "exclude": ["src/**/*.spec.ts", "node_modules"] -} + "include": [ + "src/**/*.ts" + ], + "exclude": [ + ".spec", + "src/**/*.spec.ts", + "node_modules" + ] +} \ No newline at end of file