From a3b325f2acec749d210fa63376c82be33aded3ca Mon Sep 17 00:00:00 2001 From: MrAntix Date: Fri, 2 Feb 2024 15:00:52 +0000 Subject: [PATCH 1/2] chore: fix eslint --- .eslintrc.js | 5 +- package.json | 1 - src/parsing/NextBuilder.ts | 4 +- src/parsing/ParseErrors.ts | 4 +- src/parsing/arrays/IArray.ts | 2 +- src/parsing/dates/DATE_SETTINGS.ts | 6 +- src/parsing/dates/dates-parser.spec.ts | 2 +- src/parsing/dictionaries/Dictionary.ts | 2 +- .../dictionaries/parseAllDictionary.ts | 32 ++++----- src/parsing/json/JsonParser.ts | 10 +-- src/parsing/json/json-parser.spec.ts | 66 +++++++++---------- src/parsing/json/provideParseJson.ts | 25 ++++--- src/predicates/isObject.spec.ts | 28 ++++---- 13 files changed, 95 insertions(+), 92 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index fd3c67e..dfdfe2b 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -3,6 +3,7 @@ module.exports = { parser: '@typescript-eslint/parser', plugins: ['@typescript-eslint'], rules: { + "no-shadow": "off", "semi": ["error", "always"], "quotes": ["error", "single"], "indent": ["error", 2], @@ -16,9 +17,11 @@ module.exports = { "@typescript-eslint/consistent-type-definitions": ["error", "interface"], "@typescript-eslint/no-empty-interface": ["off"], "@typescript-eslint/no-namespace": ["off"], + "@typescript-eslint/no-shadow": "warn", + "@typescript-eslint/no-unused-vars": "off", "sort-imports": ["error", { "allowSeparatedGroups": false, "ignoreDeclarationSort": true }], "no-empty-interface": "off", - //"no-unused-vars": ["off", { "args": "none" }] + "no-unused-vars": "off", }, root: true, }; diff --git a/package.json b/package.json index 12c77a1..5ce7949 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,6 @@ "publishConfig": { "access": "public" }, - "type": "module", "main": "dist/index.umd.js", "module": "dist/index.js", "typings": "dist/types/index.d.ts", diff --git a/src/parsing/NextBuilder.ts b/src/parsing/NextBuilder.ts index 94a96b8..b0f1c9a 100644 --- a/src/parsing/NextBuilder.ts +++ b/src/parsing/NextBuilder.ts @@ -4,10 +4,10 @@ * * recursive */ - export type NextBuilder = +export type NextBuilder = { [key in Exclude>] : B[key] extends (...args: infer A) => NextBuilder - ? (...args: A) => NextBuilder, i> // cascade excludes and includes + ? (...args: A) => NextBuilder, i> // cascade excludes and includes : B[key]; }; diff --git a/src/parsing/ParseErrors.ts b/src/parsing/ParseErrors.ts index cce93f8..faacd21 100644 --- a/src/parsing/ParseErrors.ts +++ b/src/parsing/ParseErrors.ts @@ -1,5 +1,5 @@ -import { NumberEnumMap } from "./numbers/NumberEnumMap"; -import { getNumberEnumValues } from "./numbers/getNumberEnumValues"; +import { NumberEnumMap } from './numbers/NumberEnumMap'; +import { getNumberEnumValues } from './numbers/getNumberEnumValues'; /** * Creates error objects diff --git a/src/parsing/arrays/IArray.ts b/src/parsing/arrays/IArray.ts index 8217114..aff5d94 100644 --- a/src/parsing/arrays/IArray.ts +++ b/src/parsing/arrays/IArray.ts @@ -11,7 +11,7 @@ export namespace IArray { readonly each: (parser: IParser) => NextBuilder, 'of' | 'each'> readonly unique: (distinctor: (item: T) => unknown) => NextBuilder, 'of' | 'each' | 'unique'>; - readonly minLength: (value: number, exclusive?: boolean) => NextBuilder, 'of' | 'each'| 'minLength'>; + readonly minLength: (value: number, exclusive?: boolean) => NextBuilder, 'of' | 'each'| 'minLength'>; readonly maxLength: (value: number, exclusive?: boolean) => NextBuilder, 'of' | 'each' | 'maxLength'>; } diff --git a/src/parsing/dates/DATE_SETTINGS.ts b/src/parsing/dates/DATE_SETTINGS.ts index 5f56f78..e817a8d 100644 --- a/src/parsing/dates/DATE_SETTINGS.ts +++ b/src/parsing/dates/DATE_SETTINGS.ts @@ -1,6 +1,8 @@ - +/** + * Global date parsing settings + */ export const DATE_SETTINGS = { - shortRE: /^(\d{1,2})[^\d\-](\d{1,2})[^\d\-](\d{2}|\d{4})$/, + shortRE: /^(\d{1,2})[^\d-](\d{1,2})[^\d-](\d{2}|\d{4})$/, formatDayFirst: new Date(2000, 1, 1) .toLocaleString('default', { day: '2-digit', month: '2-digit' }) diff --git a/src/parsing/dates/dates-parser.spec.ts b/src/parsing/dates/dates-parser.spec.ts index c73c9db..af92938 100644 --- a/src/parsing/dates/dates-parser.spec.ts +++ b/src/parsing/dates/dates-parser.spec.ts @@ -68,5 +68,5 @@ describe('dates-parser', () => { expect(result.value).toEqual(new Date(2000, 1, 1)); DATE_SETTINGS.parseDayFirst = original; - }) + }); }); diff --git a/src/parsing/dictionaries/Dictionary.ts b/src/parsing/dictionaries/Dictionary.ts index ff3dc4c..89f20f8 100644 --- a/src/parsing/dictionaries/Dictionary.ts +++ b/src/parsing/dictionaries/Dictionary.ts @@ -1,2 +1,2 @@ -export type Dictionary = { [k: string]: T; }; +export interface Dictionary { [k: string]: T; } diff --git a/src/parsing/dictionaries/parseAllDictionary.ts b/src/parsing/dictionaries/parseAllDictionary.ts index 92b4d0e..2f25995 100644 --- a/src/parsing/dictionaries/parseAllDictionary.ts +++ b/src/parsing/dictionaries/parseAllDictionary.ts @@ -10,26 +10,26 @@ import { Dictionary } from './Dictionary'; * @returns IParseResult> */ export function parseAllDictionary( - parse: IParse + parse: IParse ): IParse> { - return value => { - if (value == null) return createParseResult(null); + return value => { + if (value == null) return createParseResult(null); - return Object.keys(value).reduce>>( - (r, n) => { + return Object.keys(value).reduce>>( + (r, n) => { - const result = parse(value[n]); - const errors = result.success - ? r.errors - : { ...r.errors, [n]: result.errors }; + const result = parse(value[n]); + const errors = result.success + ? r.errors + : { ...r.errors, [n]: result.errors }; - return createParseResult( - { ...r.value, [n]: result.value }, - errors - ); - - }, createParseResult>({}) + return createParseResult( + { ...r.value, [n]: result.value }, + errors ); - }; + + }, createParseResult>({}) + ); + }; } diff --git a/src/parsing/json/JsonParser.ts b/src/parsing/json/JsonParser.ts index 87b0511..7c1c078 100644 --- a/src/parsing/json/JsonParser.ts +++ b/src/parsing/json/JsonParser.ts @@ -7,13 +7,13 @@ import { provideParseJson } from './provideParseJson'; export class JsonParser implements IJson.Parser { - constructor( + constructor( private parent: IParser, private parseCurrent: IParse = provideParseJson() - ) { } + ) { } - readonly parse = parseChain(this.parent, this.parseCurrent); + readonly parse = parseChain(this.parent, this.parseCurrent); - readonly for = (schema: ComplexSchema): IComplex.Parser => new ComplexParser(this, schema); - readonly use = (parser: IParser) => ({ parse: parseChain(this, parser.parse) }); + readonly for = (schema: ComplexSchema): IComplex.Parser => new ComplexParser(this, schema); + readonly use = (parser: IParser) => ({ parse: parseChain(this, parser.parse) }); } diff --git a/src/parsing/json/json-parser.spec.ts b/src/parsing/json/json-parser.spec.ts index 6854fc7..455aaa0 100644 --- a/src/parsing/json/json-parser.spec.ts +++ b/src/parsing/json/json-parser.spec.ts @@ -2,50 +2,50 @@ import { Is } from '../../Is'; import { ParseErrors } from '../ParseErrors'; describe('json-parser', () => { - const parser = Is.required.json; + const parser = Is.required.json; - it('success', () => { - const value = '{}'; - const result = parser.parse(value); + it('success', () => { + const value = '{}'; + const result = parser.parse(value); - expect(result.errors).toEqual(ParseErrors.empty); - expect(result.value).toEqual({}); - }); + expect(result.errors).toEqual(ParseErrors.empty); + expect(result.value).toEqual({}); + }); - it('failure', () => { - const value = '{'; - const result = parser.parse(value); + it('failure', () => { + const value = '{'; + const result = parser.parse(value); - expect(result.errors).toEqual(ParseErrors.json); - expect(result.value).toBe(null); - }); + expect(result.errors).toEqual(ParseErrors.json); + expect(result.value).toBe(null); + }); - it('required', () => { + it('required', () => { - const result = parser.parse(null); + const result = parser.parse(null); - expect(result.errors).toEqual(ParseErrors.required); - }); + expect(result.errors).toEqual(ParseErrors.required); + }); - it('not required', () => { - const requiredParser = Is.json; + it('not required', () => { + const requiredParser = Is.json; - const result = requiredParser.parse(null); + const result = requiredParser.parse(null); - expect(result.errors).toEqual(ParseErrors.empty); - }); + expect(result.errors).toEqual(ParseErrors.empty); + }); - describe('and complex', () => { - const complexParser = parser.for({ - date: Is.required.date - }); + describe('and complex', () => { + const complexParser = parser.for({ + date: Is.required.date + }); - it('success', () => { - const value = '{"date":"1 Jan 2000"}'; - const result = complexParser.parse(value); + it('success', () => { + const value = '{"date":"1 Jan 2000"}'; + const result = complexParser.parse(value); - expect(result.errors).toEqual(ParseErrors.empty); - expect(result.value).toEqual({ "date": new Date('2000-01-01T00:00:00.000Z') }); - }); - }) + expect(result.errors).toEqual(ParseErrors.empty); + expect(result.value).toEqual({ 'date': new Date('2000-01-01T00:00:00.000Z') }); + }); + }); }); \ No newline at end of file diff --git a/src/parsing/json/provideParseJson.ts b/src/parsing/json/provideParseJson.ts index f040def..a10f73f 100644 --- a/src/parsing/json/provideParseJson.ts +++ b/src/parsing/json/provideParseJson.ts @@ -3,22 +3,21 @@ import { IParseResult } from '../IParseResult'; import { ParseErrors } from '../ParseErrors'; import { createParseResult } from '../createParseResult'; - export function provideParseJson() { - return (value: T): IParseResult => { - if (isNullOrEmpty(value)) - return createParseResult(null); + return (value: T): IParseResult => { + if (isNullOrEmpty(value)) + return createParseResult(null); - if (isStringType(value)) - try { - const parsed = JSON.parse(value); + if (isStringType(value)) + try { + const parsed = JSON.parse(value); - return createParseResult(parsed as T); - } catch (error) { - console.warn(error); - } + return createParseResult(parsed as T); + } catch (error) { + console.warn(error); + } - return createParseResult(null, ParseErrors.json); - }; + return createParseResult(null, ParseErrors.json); + }; } diff --git a/src/predicates/isObject.spec.ts b/src/predicates/isObject.spec.ts index ea77355..ab3a0e4 100644 --- a/src/predicates/isObject.spec.ts +++ b/src/predicates/isObject.spec.ts @@ -1,19 +1,19 @@ -import { isObject } from "./isObject"; +import { isObject } from './isObject'; describe('isObject', () => { - [ - { value: null, expected: false }, - { value: undefined, expected: false }, - { value: 1234, expected: false }, - { value: 'STRING', expected: false }, - { value: true, expected: false }, - { value: [], expected: false }, - { value: {}, expected: true }, - { value: { a: 1 }, expected: true } - ].forEach(({ value: value, expected }) => { + [ + { value: null, expected: false }, + { value: undefined, expected: false }, + { value: 1234, expected: false }, + { value: 'STRING', expected: false }, + { value: true, expected: false }, + { value: [], expected: false }, + { value: {}, expected: true }, + { value: { a: 1 }, expected: true } + ].forEach(({ value: value, expected }) => { - it(`should return ${expected} for ${JSON.stringify(value)}`, () => { - expect(isObject(value)).toBe(expected); - }); + it(`should return ${expected} for ${JSON.stringify(value)}`, () => { + expect(isObject(value)).toBe(expected); }); + }); }); \ No newline at end of file From 71ea800e97b1350362ee8a0f588fa32b26226121 Mon Sep 17 00:00:00 2001 From: MrAntix Date: Fri, 2 Feb 2024 15:41:06 +0000 Subject: [PATCH 2/2] chore(json): correct generic name to be clear --- src/parsing/json/JsonParser.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/parsing/json/JsonParser.ts b/src/parsing/json/JsonParser.ts index 7c1c078..20172e9 100644 --- a/src/parsing/json/JsonParser.ts +++ b/src/parsing/json/JsonParser.ts @@ -8,12 +8,12 @@ import { provideParseJson } from './provideParseJson'; export class JsonParser implements IJson.Parser { constructor( - private parent: IParser, - private parseCurrent: IParse = provideParseJson() + private parent: IParser, + private parseCurrent: IParse = provideParseJson() ) { } readonly parse = parseChain(this.parent, this.parseCurrent); - readonly for = (schema: ComplexSchema): IComplex.Parser => new ComplexParser(this, schema); - readonly use = (parser: IParser) => ({ parse: parseChain(this, parser.parse) }); + readonly for = (schema: ComplexSchema): IComplex.Parser => new ComplexParser(this, schema); + readonly use = (parser: IParser) => ({ parse: parseChain(this, parser.parse) }); }