forked from ntix/parsing
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ntix#39 from MrAntix/main
feat(parsing): major refactor
- Loading branch information
Showing
90 changed files
with
1,014 additions
and
668 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,26 @@ | ||
import { IRoot, RootParser } from './parsing'; | ||
import { RootParser, provideParseRoot } from './parsing'; | ||
import { asCurrent } from './parsing/asCurrent'; | ||
|
||
/** Starting point for a new parsing and validating */ | ||
export class Is { | ||
private constructor() { | ||
throw new Error('static class'); | ||
} | ||
|
||
static readonly required: IRoot.Parser = new RootParser(true, false); | ||
private static root = new RootParser(asCurrent(provideParseRoot(false))); | ||
|
||
static readonly boolean = new RootParser().boolean; | ||
static readonly int = new RootParser().int; | ||
static readonly float = new RootParser().float; | ||
static readonly date = new RootParser().date; | ||
static readonly string = new RootParser().string; | ||
static readonly array = new RootParser().array; | ||
static readonly dictionary = new RootParser().dictionary; | ||
static readonly json = new RootParser().json; | ||
static readonly not = Is.root.not; | ||
static readonly required = Is.root.required; | ||
|
||
static readonly for = new RootParser().for; | ||
static readonly use = new RootParser().use; | ||
static readonly boolean = Is.root.boolean; | ||
static readonly int = Is.root.int; | ||
static readonly float = Is.root.float; | ||
static readonly date = Is.root.date; | ||
static readonly string = Is.root.string; | ||
static readonly array = Is.root.array; | ||
static readonly dictionary = Is.root.dictionary; | ||
static readonly json = Is.root.json; | ||
|
||
static readonly not = new RootParser(false, true); | ||
static readonly for = Is.root.for; | ||
static readonly use = Is.root.use; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { IParser } from './IParser'; | ||
|
||
/** | ||
* A parser function and negate value | ||
* | ||
* The parse function should return a negatable (non-normal) result | ||
* ie, it should return errors on success and failure to parse | ||
* so the result can be negated | ||
*/ | ||
export interface ICurrentParser<T> extends IParser<T> { | ||
negate: boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
import { IParseResult } from './IParseResult'; | ||
|
||
/** | ||
* A parse function | ||
*/ | ||
export interface IParse<T> { | ||
(value: unknown): IParseResult<T> | ||
(value: unknown): IParseResult<T>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
import { IParseErrors } from './IParseErrors'; | ||
|
||
/** | ||
* A Parse result which always returns with ParseErrors to enable negate | ||
*/ | ||
export interface IParseResult<T> { | ||
value: T | null | ||
success: boolean | ||
errors: IParseErrors | ||
value: T | null; | ||
success: boolean; | ||
errors: IParseErrors; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
import { IParse } from './IParse'; | ||
|
||
/** | ||
* A parser | ||
*/ | ||
export interface IParser<T> { | ||
parse: IParse<T> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,8 @@ | ||
/** | ||
* removes/adds given methods from the builder | ||
* and any returned builder from other methods | ||
* | ||
* recursive | ||
*/ | ||
export type NextBuilder<B, exclude extends keyof B = never, include extends keyof B = never> = | ||
{ | ||
[key in Exclude<keyof B, Exclude<exclude, include>>] | ||
: B[key] extends (...args: infer A) => NextBuilder<B, infer e, infer i> | ||
? (...args: A) => NextBuilder<B, e | Exclude<exclude, include>, i> // cascade excludes and includes | ||
: B[key]; | ||
}; | ||
export type NextBuilder<P, remove extends keyof P = never, add extends keyof P = never> = | ||
{ | ||
[K in Exclude<keyof P, Exclude<remove, add>>] | ||
: P[K] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,32 @@ | ||
import { IParse } from '../IParse'; | ||
import { IParser } from '../IParser'; | ||
import { parseAllArray } from './parseAllArray'; | ||
import { provideParseArrayValues } from './provideParseArrayValues'; | ||
import { parseChain } from '../parseChain'; | ||
import { ICurrentParser } from '../ICurrentParser'; | ||
import { provideMaxLength } from '../provideMaxLength'; | ||
import { provideMinLength } from '../provideMinLength'; | ||
import { IArray } from './IArray'; | ||
import { provideParseArray } from './provideParseArray'; | ||
import { provideUniqueArray } from './provideUniqueArray'; | ||
import { provideParseUniqueArray } from './provideParseUniqueArray'; | ||
import { asCurrent } from '../asCurrent'; | ||
|
||
export class ArrayParser<T> implements IArray.Parser<T> { | ||
|
||
constructor( | ||
private parent: IParser<unknown>, | ||
private parseCurrent: IParse<T[]> = null, | ||
private parseCurrent: ICurrentParser<T[]>, | ||
private negate: boolean = false | ||
) { } | ||
|
||
readonly parse = parseChain<T[]>(this.parent, this.parseCurrent ?? provideParseArray<T>(this.negate)); | ||
readonly parse = parseChain(this.parent, this.parseCurrent, 'ARRAY'); | ||
|
||
get not() { | ||
return new ArrayParser<T>(this.parent, this.parseCurrent, true); | ||
return new ArrayParser<T>(this.parent, this.parseCurrent, !this.negate); | ||
} | ||
|
||
readonly of = <U>() => new ArrayParser<U>(this.parent); | ||
readonly each = <U = T>(parser: IParser<U>) => new ArrayParser<U>(this, parseAllArray(parser.parse), this.negate); | ||
readonly of = <U>() => new ArrayParser<U>(this.parent, asCurrent(provideParseArray<U>(), this.negate)); | ||
readonly each = <U = T>(parser: IParser<U>) => new ArrayParser<U>(this, asCurrent(provideParseArrayValues(parser.parse), this.negate)); | ||
|
||
readonly unique = (distinctor: (item: T) => unknown) => new ArrayParser<T>(this, provideUniqueArray(distinctor, this.negate)); | ||
readonly minLength = (minValue: number, exclusive = false) => new ArrayParser<T>(this, provideMinLength<T[]>(minValue, exclusive, this.negate)); | ||
readonly maxLength = (maxValue: number, exclusive = false) => new ArrayParser<T>(this, provideMaxLength<T[]>(maxValue, exclusive, this.negate)); | ||
readonly unique = <U = T>(distinctor: (item: U) => unknown) => new ArrayParser<U>(this, asCurrent(provideParseUniqueArray(distinctor), this.negate)); | ||
readonly minLength = (minValue: number, exclusive = false) => new ArrayParser<T>(this, asCurrent(provideMinLength<T[]>(minValue, exclusive), this.negate)); | ||
readonly maxLength = (maxValue: number, exclusive = false) => new ArrayParser<T>(this, asCurrent(provideMaxLength<T[]>(maxValue, exclusive), this.negate)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.