From 48cb4c1c855b9174b456b8871621ae760140c2fd Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Wed, 30 Sep 2020 14:16:09 +0200 Subject: [PATCH] Upgrade dependencies --- package.json | 24 +++++++++++------------ source/index.ts | 30 +++++++++++------------------ source/predicates.ts | 22 ++++++++++----------- source/predicates/any.ts | 4 ++-- source/predicates/array.ts | 8 ++++---- source/predicates/base-predicate.ts | 1 + source/predicates/predicate.ts | 4 +++- source/predicates/string.ts | 4 ++-- source/utils/has-items.ts | 2 +- source/utils/is-valid-identifier.ts | 2 +- source/utils/match-shape.ts | 8 ++++---- source/utils/of-type-deep.ts | 4 ++-- source/utils/of-type.ts | 4 ++-- test/custom-message.ts | 2 +- test/custom-predicate.ts | 2 +- test/nan.ts | 2 +- 16 files changed, 59 insertions(+), 64 deletions(-) diff --git a/package.json b/package.json index ded1626..d50c567 100644 --- a/package.json +++ b/package.json @@ -48,16 +48,16 @@ "object" ], "dependencies": { - "type-fest": "^0.11.0" + "type-fest": "^0.17.0" }, "devDependencies": { - "@sindresorhus/is": "^2.1.0", + "@sindresorhus/is": "^3.1.2", "@sindresorhus/tsconfig": "^0.7.0", "@types/lodash.isequal": "^4.5.2", - "@types/node": "^13.7.4", + "@types/node": "^14.11.2", "@types/vali-date": "^1.0.0", - "@typescript-eslint/eslint-plugin": "^2.20.0", - "@typescript-eslint/parser": "^2.20.0", + "@typescript-eslint/eslint-plugin": "^4.3.0", + "@typescript-eslint/parser": "^4.3.0", "add-asset-webpack-plugin": "^1.0.0", "add-module-exports-webpack-plugin": "^1.0.0", "ava": "^2.0.0", @@ -66,16 +66,16 @@ "codecov": "^3.1.0", "del-cli": "^3.0.0", "dot-prop": "^5.2.0", - "eslint-config-xo-typescript": "^0.26.0", + "eslint-config-xo-typescript": "^0.33.0", "license-webpack-plugin": "^2.0.2", "lodash.isequal": "^4.5.0", "nyc": "^15.0.0", - "typedoc": "^0.16.10", - "typescript": "~3.8.2", + "typedoc": "^0.19.2", + "typescript": "~4.0.3", "vali-date": "^1.0.0", "webpack": "^4.33.0", "webpack-cli": "^3.3.10", - "xo": "^0.26.1" + "xo": "^0.33.1" }, "browser": { "./dist/source/utils/infer-label.js": "./dist/source/utils/infer-label.browser.js" @@ -94,14 +94,14 @@ ], "rules": { "no-useless-return": "off", - "@typescript-eslint/generic-type-naming": "off", - "@typescript-eslint/member-naming": "off", "ava/no-ignored-test-files": "off", "@typescript-eslint/explicit-function-return-type": "off", "@typescript-eslint/ban-types": "off", "@typescript-eslint/no-explicit-any": "off", "@typescript-eslint/no-empty-function": "off", - "@typescript-eslint/restrict-template-expressions": "off" + "@typescript-eslint/restrict-template-expressions": "off", + "@typescript-eslint/no-unused-vars": "off", + "import/no-anonymous-default-export": "off" } }, "ava": { diff --git a/source/index.ts b/source/index.ts index 3dd5347..0c58d52 100644 --- a/source/index.ts +++ b/source/index.ts @@ -14,44 +14,36 @@ export type Main = (value: T, label: string | Function, predicate: BasePredic // Extends is only necessary for the generated documentation to be cleaner. The loaders below infer the correct type. export interface Ow extends Modifiers, Predicates { /** - Test if the value matches the predicate. Throws an `ArgumentError` if the test fails. + Returns `true` if the value matches the predicate, otherwise returns `false`. @param value - Value to test. @param predicate - Predicate to test against. */ - (value: T, predicate: BasePredicate): void; + isValid: (value: T, predicate: BasePredicate) => value is T; /** - Test if `value` matches the provided `predicate`. Throws an `ArgumentError` with the specified `label` if the test fails. + Create a reusable validator. - @param value - Value to test. - @param label - Label which should be used in error messages. - @param predicate - Predicate to test against. + @param predicate - Predicate used in the validator function. */ - (value: T, label: string, predicate: BasePredicate): void; + create: ((predicate: BasePredicate) => ReusableValidator) & ((label: string, predicate: BasePredicate) => ReusableValidator); /** - Returns `true` if the value matches the predicate, otherwise returns `false`. + Test if the value matches the predicate. Throws an `ArgumentError` if the test fails. @param value - Value to test. @param predicate - Predicate to test against. */ - isValid(value: T, predicate: BasePredicate): value is T; - - /** - Create a reusable validator. - - @param predicate - Predicate used in the validator function. - */ - create(predicate: BasePredicate): ReusableValidator; + (value: T, predicate: BasePredicate): void; /** - Create a reusable validator. + Test if `value` matches the provided `predicate`. Throws an `ArgumentError` with the specified `label` if the test fails. + @param value - Value to test. @param label - Label which should be used in error messages. - @param predicate - Predicate used in the validator function. + @param predicate - Predicate to test against. */ - create(label: string, predicate: BasePredicate): ReusableValidator; + (value: T, label: string, predicate: BasePredicate): void; } /** diff --git a/source/predicates.ts b/source/predicates.ts index 17fb456..e852f98 100644 --- a/source/predicates.ts +++ b/source/predicates.ts @@ -191,17 +191,17 @@ export interface Predicates { /** Test that the value matches at least one of the given predicates. */ - any(p1: BasePredicate): AnyPredicate; - any(p1: BasePredicate, p2: BasePredicate): AnyPredicate; - any(p1: BasePredicate, p2: BasePredicate, p3: BasePredicate): AnyPredicate; - any(p1: BasePredicate, p2: BasePredicate, p3: BasePredicate, p4: BasePredicate): AnyPredicate; - any(p1: BasePredicate, p2: BasePredicate, p3: BasePredicate, p4: BasePredicate, p5: BasePredicate): AnyPredicate; - any(p1: BasePredicate, p2: BasePredicate, p3: BasePredicate, p4: BasePredicate, p5: BasePredicate, p6: BasePredicate): AnyPredicate; - any(p1: BasePredicate, p2: BasePredicate, p3: BasePredicate, p4: BasePredicate, p5: BasePredicate, p6: BasePredicate, p7: BasePredicate): AnyPredicate; - any(p1: BasePredicate, p2: BasePredicate, p3: BasePredicate, p4: BasePredicate, p5: BasePredicate, p6: BasePredicate, p7: BasePredicate, p8: BasePredicate): AnyPredicate; - any(p1: BasePredicate, p2: BasePredicate, p3: BasePredicate, p4: BasePredicate, p5: BasePredicate, p6: BasePredicate, p7: BasePredicate, p8: BasePredicate, p9: BasePredicate): AnyPredicate; - any(p1: BasePredicate, p2: BasePredicate, p3: BasePredicate, p4: BasePredicate, p5: BasePredicate, p6: BasePredicate, p7: BasePredicate, p8: BasePredicate, p9: BasePredicate, p10: BasePredicate): AnyPredicate; - any(...predicate: BasePredicate[]): AnyPredicate; + any: ((p1: BasePredicate) => AnyPredicate) + & ((p1: BasePredicate, p2: BasePredicate) => AnyPredicate) + & ((p1: BasePredicate, p2: BasePredicate, p3: BasePredicate) => AnyPredicate) + & ((p1: BasePredicate, p2: BasePredicate, p3: BasePredicate, p4: BasePredicate) => AnyPredicate) + & ((p1: BasePredicate, p2: BasePredicate, p3: BasePredicate, p4: BasePredicate, p5: BasePredicate) => AnyPredicate) + & ((p1: BasePredicate, p2: BasePredicate, p3: BasePredicate, p4: BasePredicate, p5: BasePredicate, p6: BasePredicate) => AnyPredicate) + & ((p1: BasePredicate, p2: BasePredicate, p3: BasePredicate, p4: BasePredicate, p5: BasePredicate, p6: BasePredicate, p7: BasePredicate) => AnyPredicate) + & ((p1: BasePredicate, p2: BasePredicate, p3: BasePredicate, p4: BasePredicate, p5: BasePredicate, p6: BasePredicate, p7: BasePredicate, p8: BasePredicate) => AnyPredicate) + & ((p1: BasePredicate, p2: BasePredicate, p3: BasePredicate, p4: BasePredicate, p5: BasePredicate, p6: BasePredicate, p7: BasePredicate, p8: BasePredicate, p9: BasePredicate) => AnyPredicate) + & ((p1: BasePredicate, p2: BasePredicate, p3: BasePredicate, p4: BasePredicate, p5: BasePredicate, p6: BasePredicate, p7: BasePredicate, p8: BasePredicate, p9: BasePredicate, p10: BasePredicate) => AnyPredicate) + & ((...predicate: BasePredicate[]) => AnyPredicate); } export default (object: T, options?: PredicateOptions): T & Predicates => { diff --git a/source/predicates/any.ts b/source/predicates/any.ts index 64777aa..b563378 100644 --- a/source/predicates/any.ts +++ b/source/predicates/any.ts @@ -21,12 +21,12 @@ export class AnyPredicate implements BasePredicate { try { main(value, label, predicate); return; - } catch (error) { + } catch (error: unknown) { if (value === undefined && this.options.optional === true) { return; } - errors.push(`- ${error.message}`); + errors.push(`- ${(error as Error).message}`); } } diff --git a/source/predicates/array.ts b/source/predicates/array.ts index 12fba51..382c223 100644 --- a/source/predicates/array.ts +++ b/source/predicates/array.ts @@ -81,7 +81,7 @@ export class ArrayPredicate extends Predicate { includes(...searchElements: readonly T[]) { return this.addValidator({ message: (value, label) => `Expected ${label} to include all elements of \`${JSON.stringify(searchElements)}\`, got \`${JSON.stringify(value)}\``, - validator: value => searchElements.every(el => value.includes(el)) + validator: value => searchElements.every(element => value.includes(element)) }); } @@ -93,7 +93,7 @@ export class ArrayPredicate extends Predicate { includesAny(...searchElements: readonly T[]) { return this.addValidator({ message: (value, label) => `Expected ${label} to include any element of \`${JSON.stringify(searchElements)}\`, got \`${JSON.stringify(value)}\``, - validator: value => searchElements.some(el => value.includes(el)) + validator: value => searchElements.some(element => value.includes(element)) }); } @@ -151,8 +151,8 @@ export class ArrayPredicate extends Predicate { } return true; - } catch (error_) { - error = error_.message; + } catch (error_: unknown) { + error = (error_ as Error).message; return false; } } diff --git a/source/predicates/base-predicate.ts b/source/predicates/base-predicate.ts index e9c584a..26cd223 100644 --- a/source/predicates/base-predicate.ts +++ b/source/predicates/base-predicate.ts @@ -14,5 +14,6 @@ export const isPredicate = (value: unknown): value is BasePredicate => Boolean(( @hidden */ export interface BasePredicate { + // eslint-disable-next-line @typescript-eslint/method-signature-style [testSymbol](value: T, main: Main, label: string | Function): void; } diff --git a/source/predicates/predicate.ts b/source/predicates/predicate.ts index baa9b39..12d18f7 100644 --- a/source/predicates/predicate.ts +++ b/source/predicates/predicate.ts @@ -18,8 +18,10 @@ export type ValidatorMessageBuilder = (value: T, label?: string) => string; @hidden */ export interface Validator { + // eslint-disable-next-line @typescript-eslint/method-signature-style message(value: T, label?: string, result?: any): string; + // eslint-disable-next-line @typescript-eslint/method-signature-style validator(value: T): unknown; /** @@ -27,7 +29,7 @@ export interface Validator { When absent, the return value of `message()` is used and 'not' is inserted after the first 'to', e.g. `Expected 'smth' to be empty` -> `Expected 'smth' to not be empty`. */ - negatedMessage?(value: T, label: string): string; + negatedMessage?(value: T, label: string): string; // eslint-disable-line @typescript-eslint/method-signature-style } /** diff --git a/source/predicates/string.ts b/source/predicates/string.ts index 82b9b46..9e778d3 100644 --- a/source/predicates/string.ts +++ b/source/predicates/string.ts @@ -165,7 +165,7 @@ export class StringPredicate extends Predicate { get alphabetical() { return this.addValidator({ message: (value, label) => `Expected ${label} to be alphabetical, got \`${value}\``, - validator: value => /^[a-z]+$/ig.test(value) + validator: value => /^[a-z]+$/gi.test(value) }); } @@ -175,7 +175,7 @@ export class StringPredicate extends Predicate { get numeric() { return this.addValidator({ message: (value, label) => `Expected ${label} to be numeric, got \`${value}\``, - validator: value => /^(?:\+|-)?\d+$/i.test(value) + validator: value => /^[+-]?\d+$/i.test(value) }); } diff --git a/source/utils/has-items.ts b/source/utils/has-items.ts index 8731e59..78712ec 100644 --- a/source/utils/has-items.ts +++ b/source/utils/has-items.ts @@ -2,7 +2,7 @@ @hidden */ export interface CollectionLike { - has(item: T): boolean; + has: (item: T) => boolean; } /** diff --git a/source/utils/is-valid-identifier.ts b/source/utils/is-valid-identifier.ts index 027d818..9c9947d 100644 --- a/source/utils/is-valid-identifier.ts +++ b/source/utils/is-valid-identifier.ts @@ -1,4 +1,4 @@ -const identifierRegex = /^[a-z$_][a-z$_\d]*$/i; +const identifierRegex = /^[a-z$_][$\w]*$/i; const reservedSet = new Set([ 'undefined', diff --git a/source/utils/match-shape.ts b/source/utils/match-shape.ts index 5ae3ba7..3534145 100644 --- a/source/utils/match-shape.ts +++ b/source/utils/match-shape.ts @@ -33,8 +33,8 @@ export function partial(object: {[key: string]: any}, shape: Shape, parent?: str } return true; - } catch (error) { - return error.message; + } catch (error: unknown) { + return (error as Error).message; } } @@ -78,7 +78,7 @@ export function exact(object: {[key: string]: any}, shape: Shape, parent?: strin } return true; - } catch (error) { - return error.message; + } catch (error: unknown) { + return (error as Error).message; } } diff --git a/source/utils/of-type-deep.ts b/source/utils/of-type-deep.ts index 968f536..e99fb90 100644 --- a/source/utils/of-type-deep.ts +++ b/source/utils/of-type-deep.ts @@ -21,7 +21,7 @@ Test all the values in the object against a provided predicate. export default (object: unknown, predicate: Predicate): boolean | string => { try { return ofTypeDeep(object, predicate); - } catch (error) { - return error.message; + } catch (error: unknown) { + return (error as Error).message; } }; diff --git a/source/utils/of-type.ts b/source/utils/of-type.ts index 880de1c..2ebaf48 100644 --- a/source/utils/of-type.ts +++ b/source/utils/of-type.ts @@ -15,7 +15,7 @@ export default (source: IterableIterator | Set | T[], predicate: BasePr } return true; - } catch (error) { - return error.message; + } catch (error: unknown) { + return (error as Error).message; } }; diff --git a/test/custom-message.ts b/test/custom-message.ts index aa0414e..f4b5bec 100644 --- a/test/custom-message.ts +++ b/test/custom-message.ts @@ -1,5 +1,5 @@ import test from 'ava'; -import {default as ow, Predicate} from '../source'; +import ow, {Predicate} from '../source'; class CustomPredicate extends Predicate { constructor() { diff --git a/test/custom-predicate.ts b/test/custom-predicate.ts index 598d77a..24720a1 100644 --- a/test/custom-predicate.ts +++ b/test/custom-predicate.ts @@ -1,5 +1,5 @@ import test from 'ava'; -import {default as ow, Predicate} from '../source'; +import ow, {Predicate} from '../source'; class CustomPredicate extends Predicate { constructor() { diff --git a/test/nan.ts b/test/nan.ts index 2a7f8ab..4a5436e 100644 --- a/test/nan.ts +++ b/test/nan.ts @@ -3,7 +3,7 @@ import ow from '../source'; test('nan', t => { t.notThrows(() => { - ow(NaN, ow.nan); + ow(Number.NaN, ow.nan); }); t.notThrows(() => {