Skip to content

Commit

Permalink
Merge pull request ntix#34 from MrAntix/main
Browse files Browse the repository at this point in the history
chore: fix eslint
  • Loading branch information
ntix authored Feb 2, 2024
2 parents 01cc390 + 71ea800 commit 30e5afb
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 94 deletions.
5 changes: 4 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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,
};
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"publishConfig": {
"access": "public"
},
"type": "module",
"main": "dist/index.umd.js",
"module": "dist/index.js",
"typings": "dist/types/index.d.ts",
Expand Down
4 changes: 2 additions & 2 deletions src/parsing/NextBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
*
* recursive
*/
export type NextBuilder<B, exclude extends keyof B = never, include extends keyof B = never> =
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
? (...args: A) => NextBuilder<B, e | Exclude<exclude, include>, i> // cascade excludes and includes
: B[key];
};
4 changes: 2 additions & 2 deletions src/parsing/ParseErrors.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/parsing/arrays/IArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export namespace IArray {
readonly each: <U = T>(parser: IParser<U>) => NextBuilder<Parser<U>, 'of' | 'each'>

readonly unique: (distinctor: (item: T) => unknown) => NextBuilder<Parser<T>, 'of' | 'each' | 'unique'>;
readonly minLength: (value: number, exclusive?: boolean) => NextBuilder<Parser<T>, 'of' | 'each'| 'minLength'>;
readonly minLength: (value: number, exclusive?: boolean) => NextBuilder<Parser<T>, 'of' | 'each'| 'minLength'>;
readonly maxLength: (value: number, exclusive?: boolean) => NextBuilder<Parser<T>, 'of' | 'each' | 'maxLength'>;

}
Expand Down
6 changes: 4 additions & 2 deletions src/parsing/dates/DATE_SETTINGS.ts
Original file line number Diff line number Diff line change
@@ -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' })
Expand Down
2 changes: 1 addition & 1 deletion src/parsing/dates/dates-parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,5 @@ describe('dates-parser', () => {
expect(result.value).toEqual(new Date(2000, 1, 1));

DATE_SETTINGS.parseDayFirst = original;
})
});
});
2 changes: 1 addition & 1 deletion src/parsing/dictionaries/Dictionary.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@

export type Dictionary<T> = { [k: string]: T; };
export interface Dictionary<T> { [k: string]: T; }
32 changes: 16 additions & 16 deletions src/parsing/dictionaries/parseAllDictionary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@ import { Dictionary } from './Dictionary';
* @returns IParseResult<Dictionary<T>>
*/
export function parseAllDictionary<T>(
parse: IParse<T>
parse: IParse<T>
): IParse<Dictionary<T>> {

return value => {
if (value == null) return createParseResult(null);
return value => {
if (value == null) return createParseResult(null);

return Object.keys(value).reduce<IParseResult<Dictionary<T>>>(
(r, n) => {
return Object.keys(value).reduce<IParseResult<Dictionary<T>>>(
(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<Dictionary<T>>({})
return createParseResult(
{ ...r.value, [n]: result.value },
errors
);
};

}, createParseResult<Dictionary<T>>({})
);
};
}
14 changes: 7 additions & 7 deletions src/parsing/json/JsonParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import { provideParseJson } from './provideParseJson';

export class JsonParser<T> implements IJson.Parser<T> {

constructor(
private parent: IParser<unknown>,
private parseCurrent: IParse<T> = provideParseJson<T>()
) { }
constructor(
private parent: IParser<unknown>,
private parseCurrent: IParse<T> = provideParseJson<T>()
) { }

readonly parse = parseChain<T>(this.parent, this.parseCurrent);
readonly parse = parseChain<T>(this.parent, this.parseCurrent);

readonly for = <T>(schema: ComplexSchema<T>): IComplex.Parser<T> => new ComplexParser(this, schema);
readonly use = <T>(parser: IParser<T>) => ({ parse: parseChain<T>(this, parser.parse) });
readonly for = <U>(schema: ComplexSchema<U>): IComplex.Parser<U> => new ComplexParser(this, schema);
readonly use = <U>(parser: IParser<U>) => ({ parse: parseChain<U>(this, parser.parse) });
}
66 changes: 33 additions & 33 deletions src/parsing/json/json-parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') });
});
});
});
25 changes: 12 additions & 13 deletions src/parsing/json/provideParseJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@ import { IParseResult } from '../IParseResult';
import { ParseErrors } from '../ParseErrors';
import { createParseResult } from '../createParseResult';


export function provideParseJson<T>() {

return (value: T): IParseResult<T> => {
if (isNullOrEmpty(value))
return createParseResult(null);
return (value: T): IParseResult<T> => {
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);
};
}
28 changes: 14 additions & 14 deletions src/predicates/isObject.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});

0 comments on commit 30e5afb

Please sign in to comment.