Skip to content

Commit

Permalink
Meta tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Oct 3, 2022
1 parent 4b926e8 commit 5f6ef36
Show file tree
Hide file tree
Showing 32 changed files with 79 additions and 80 deletions.
26 changes: 12 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"node": "./dev-only.js"
}
},
"sideEffects": false,
"engines": {
"node": ">=14.16"
},
Expand Down Expand Up @@ -57,7 +58,7 @@
"object"
],
"dependencies": {
"@sindresorhus/is": "^5.1.0",
"@sindresorhus/is": "^5.3.0",
"callsites": "^4.0.0",
"dot-prop": "^7.2.0",
"lodash.isequal": "^4.5.0",
Expand All @@ -66,22 +67,21 @@
"devDependencies": {
"@sindresorhus/tsconfig": "^3.0.1",
"@types/lodash.isequal": "^4.5.6",
"@types/node": "^17.0.42",
"@types/node": "^18.8.0",
"@types/vali-date": "^1.0.0",
"ava": "^4.3.0",
"c8": "^7.11.3",
"del-cli": "^4.0.1",
"expect-type": "^0.13.0",
"ava": "^4.3.3",
"c8": "^7.12.0",
"del-cli": "^5.0.0",
"expect-type": "^0.14.2",
"gh-pages": "^4.0.0",
"ts-node": "^10.8.1",
"typedoc": "^0.22.17",
"typescript": "^4.7.3",
"xo": "^0.50.0"
"ts-node": "^10.9.1",
"typedoc": "^0.23.15",
"typescript": "^4.8.4",
"xo": "^0.52.3"
},
"browser": {
"./dist/utils/infer-label.js": "./dist/utils/infer-label.browser.js"
},
"sideEffects": false,
"xo": {
"ignores": [
"example.js",
Expand All @@ -90,13 +90,11 @@
],
"rules": {
"no-useless-return": "off",
"ava/no-ignored-test-files": "off",
"@typescript-eslint/explicit-function-return-type": "error",
"@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/no-unused-vars": "off"
"@typescript-eslint/restrict-template-expressions": "off"
}
},
"ava": {
Expand Down
14 changes: 7 additions & 7 deletions source/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import callsites from 'callsites';
import {inferLabel} from './utils/infer-label.js';
import {BasePredicate, isPredicate} from './predicates/base-predicate.js';
import modifiers, {Modifiers} from './modifiers.js';
import predicates, {Predicates} from './predicates.js';
import {isPredicate, type BasePredicate} from './predicates/base-predicate.js';
import modifiers, {type Modifiers} from './modifiers.js';
import predicates, {type Predicates} from './predicates.js';
import test from './test.js';

/**
Expand All @@ -27,7 +27,7 @@ type User = Infer<typeof userPredicate>;
export type Infer<P> = P extends BasePredicate<infer T> ? T : never;

// Extends is only necessary for the generated documentation to be cleaner. The loaders below infer the correct type.
export interface Ow extends Modifiers, Predicates {
export type Ow = {
/**
Test if the value matches the predicate. Throws an `ArgumentError` if the test fails.
Expand Down Expand Up @@ -59,12 +59,12 @@ export interface Ow extends Modifiers, Predicates {
@param predicate - Predicate used in the validator function.
*/
create: (<T>(predicate: BasePredicate<T>) => ReusableValidator<T>) & (<T>(label: string, predicate: BasePredicate<T>) => ReusableValidator<T>);
}
} & Modifiers & Predicates;

/**
A reusable validator.
*/
export interface ReusableValidator<T> {
export type ReusableValidator<T> = {
/**
Test if the value matches the predicate. Throws an `ArgumentError` if the test fails.
Expand All @@ -73,7 +73,7 @@ export interface ReusableValidator<T> {
*/
// eslint-disable-next-line @typescript-eslint/prefer-function-type, @typescript-eslint/no-redundant-type-constituents
(value: unknown | T, label?: string): void;
}
};

/**
Turn a `ReusableValidator` into one with a type assertion.
Expand Down
8 changes: 4 additions & 4 deletions source/modifiers.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import predicates, {Predicates} from './predicates.js';
import {BasePredicate} from './index.js';
import predicates, {type Predicates} from './predicates.js';
import type {BasePredicate} from './index.js';

type Optionalify<P> = P extends BasePredicate<infer X>
? P & BasePredicate<X | undefined>
: P;

export interface Modifiers {
export type Modifiers = {
/**
Make the following predicate optional so it doesn't fail when the value is `undefined`.
*/
readonly optional: {
[K in keyof Predicates]: Optionalify<Predicates[K]>
};
}
};

const modifiers = <T>(object: T): T & Modifiers => {
Object.defineProperties(object, {
Expand Down
10 changes: 5 additions & 5 deletions source/predicates.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type {Buffer} from 'node:buffer';
import {TypedArray} from './typed-array.js';
import type {TypedArray} from './typed-array.js';
import {StringPredicate} from './predicates/string.js';
import {NumberPredicate} from './predicates/number.js';
import {BigIntPredicate} from './predicates/bigint.js';
import {BooleanPredicate} from './predicates/boolean.js';
import {Predicate, PredicateOptions} from './predicates/predicate.js';
import {Predicate, type PredicateOptions} from './predicates/predicate.js';
import {ArrayPredicate} from './predicates/array.js';
import {ObjectPredicate} from './predicates/object.js';
import {DatePredicate} from './predicates/date.js';
Expand All @@ -16,10 +16,10 @@ import {WeakSetPredicate} from './predicates/weak-set.js';
import {TypedArrayPredicate} from './predicates/typed-array.js';
import {ArrayBufferPredicate} from './predicates/array-buffer.js';
import {DataViewPredicate} from './predicates/data-view.js';
import {BasePredicate} from './predicates/base-predicate.js';
import type {BasePredicate} from './predicates/base-predicate.js';
import {AnyPredicate} from './predicates/any.js';

export interface Predicates {
export type Predicates = {
/**
Test the value to be a string.
*/
Expand Down Expand Up @@ -209,7 +209,7 @@ export interface Predicates {
& (<T1, T2, T3, T4, T5, T6, T7, T8, T9>(p1: BasePredicate<T1>, p2: BasePredicate<T2>, p3: BasePredicate<T3>, p4: BasePredicate<T4>, p5: BasePredicate<T5>, p6: BasePredicate<T6>, p7: BasePredicate<T7>, p8: BasePredicate<T8>, p9: BasePredicate<T9>) => AnyPredicate<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8 | T9>)
& (<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(p1: BasePredicate<T1>, p2: BasePredicate<T2>, p3: BasePredicate<T3>, p4: BasePredicate<T4>, p5: BasePredicate<T5>, p6: BasePredicate<T6>, p7: BasePredicate<T7>, p8: BasePredicate<T8>, p9: BasePredicate<T9>, p10: BasePredicate<T10>) => AnyPredicate<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8 | T9 | T10>)
& ((...predicate: BasePredicate[]) => AnyPredicate);
}
};

const predicates = <T>(object: T, options?: PredicateOptions): T & Predicates => {
Object.defineProperties(object, {
Expand Down
6 changes: 3 additions & 3 deletions source/predicates/any.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {ArgumentError} from '../argument-error.js';
import {Main} from '../index.js';
import type {Main} from '../index.js';
import {generateArgumentErrorMessage} from '../utils/generate-argument-error-message.js';
import {BasePredicate, testSymbol} from './base-predicate.js';
import {PredicateOptions} from './predicate.js';
import {testSymbol, type BasePredicate} from './base-predicate.js';
import type {PredicateOptions} from './predicate.js';

/**
@hidden
Expand Down
6 changes: 3 additions & 3 deletions source/predicates/array.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import isEqual from 'lodash.isequal';
import {exact} from '../utils/match-shape.js';
import ofType from '../utils/of-type.js';
import {BasePredicate} from './base-predicate.js';
import {Predicate, PredicateOptions} from './predicate.js';
import {Shape} from './object.js';
import type {BasePredicate} from './base-predicate.js';
import {Predicate, type PredicateOptions} from './predicate.js';
import type {Shape} from './object.js';

export class ArrayPredicate<T = unknown> extends Predicate<T[]> {
/**
Expand Down
6 changes: 3 additions & 3 deletions source/predicates/base-predicate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Main} from '../index.js';
import type {Main} from '../index.js';

/**
@hidden
Expand All @@ -13,6 +13,6 @@ export const isPredicate = (value: unknown): value is BasePredicate => Boolean((
/**
@hidden
*/
export interface BasePredicate<T = unknown> {
export type BasePredicate<T = unknown> = {
[testSymbol](value: T, main: Main, label: string | Function, idLabel?: boolean): void;
}
};
2 changes: 1 addition & 1 deletion source/predicates/bigint.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Predicate, PredicateOptions} from './predicate.js';
import {Predicate, type PredicateOptions} from './predicate.js';

export class BigIntPredicate extends Predicate<bigint> {
/**
Expand Down
2 changes: 1 addition & 1 deletion source/predicates/boolean.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Predicate, PredicateOptions} from './predicate.js';
import {Predicate, type PredicateOptions} from './predicate.js';

export class BooleanPredicate extends Predicate<boolean> {
/**
Expand Down
2 changes: 1 addition & 1 deletion source/predicates/data-view.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Predicate, PredicateOptions} from './predicate.js';
import {Predicate, type PredicateOptions} from './predicate.js';

export class DataViewPredicate extends Predicate<DataView> {
/**
Expand Down
2 changes: 1 addition & 1 deletion source/predicates/date.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Predicate, PredicateOptions} from './predicate.js';
import {Predicate, type PredicateOptions} from './predicate.js';

export class DatePredicate extends Predicate<Date> {
/**
Expand Down
2 changes: 1 addition & 1 deletion source/predicates/error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Predicate, PredicateOptions} from './predicate.js';
import {Predicate, type PredicateOptions} from './predicate.js';

export class ErrorPredicate extends Predicate<Error> {
/**
Expand Down
2 changes: 1 addition & 1 deletion source/predicates/map.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import isEqual from 'lodash.isequal';
import hasItems from '../utils/has-items.js';
import ofType from '../utils/of-type.js';
import {Predicate, PredicateOptions} from './predicate.js';
import {Predicate, type PredicateOptions} from './predicate.js';

export class MapPredicate<T1 = unknown, T2 = unknown> extends Predicate<Map<T1, T2>> {
/**
Expand Down
2 changes: 1 addition & 1 deletion source/predicates/number.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import is from '@sindresorhus/is';
import {Predicate, PredicateOptions} from './predicate.js';
import {Predicate, type PredicateOptions} from './predicate.js';

export class NumberPredicate extends Predicate<number> {
/**
Expand Down
6 changes: 3 additions & 3 deletions source/predicates/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import isEqual from 'lodash.isequal';
import hasItems from '../utils/has-items.js';
import ofType from '../utils/of-type.js';
import ofTypeDeep from '../utils/of-type-deep.js';
import {partial, exact, Shape, TypeOfShape} from '../utils/match-shape.js';
import {Predicate, PredicateOptions} from './predicate.js';
import {BasePredicate} from './base-predicate.js';
import {partial, exact, type Shape, type TypeOfShape} from '../utils/match-shape.js';
import {Predicate, type PredicateOptions} from './predicate.js';
import type {BasePredicate} from './base-predicate.js';

export class ObjectPredicate<T extends object = object> extends Predicate<T> {
/**
Expand Down
16 changes: 8 additions & 8 deletions source/predicates/predicate.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import is from '@sindresorhus/is';
import {ArgumentError} from '../argument-error.js';
import {not} from '../operators/not.js';
import {Main} from '../index.js';
import type {Main} from '../index.js';
import {generateArgumentErrorMessage} from '../utils/generate-argument-error-message.js';
import {BasePredicate, testSymbol} from './base-predicate.js';
import {testSymbol, type BasePredicate} from './base-predicate.js';

/**
Function executed when the provided validation fails.
Expand All @@ -18,7 +18,7 @@ export type ValidatorMessageBuilder<T> = (value: T, label?: string) => string;
/**
@hidden
*/
export interface Validator<T> {
export type Validator<T> = {
message(value: T, label?: string, result?: any): string;

validator(value: T): unknown;
Expand All @@ -29,21 +29,21 @@ export interface Validator<T> {
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;
}
};

/**
@hidden
*/
export interface PredicateOptions {
export type PredicateOptions = {
optional?: boolean;
}
};

/**
@hidden
*/
export interface Context<T = unknown> extends PredicateOptions {
export type Context<T = unknown> = {
validators: Array<Validator<T>>;
}
} & PredicateOptions;

/**
@hidden
Expand Down
2 changes: 1 addition & 1 deletion source/predicates/set.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import isEqual from 'lodash.isequal';
import hasItems from '../utils/has-items.js';
import ofType from '../utils/of-type.js';
import {Predicate, PredicateOptions} from './predicate.js';
import {Predicate, type PredicateOptions} from './predicate.js';

export class SetPredicate<T = any> extends Predicate<Set<T>> {
/**
Expand Down
2 changes: 1 addition & 1 deletion source/predicates/string.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import is from '@sindresorhus/is';
import valiDate from 'vali-date';
import {Predicate, PredicateOptions} from './predicate.js';
import {Predicate, type PredicateOptions} from './predicate.js';

export class StringPredicate extends Predicate<string> {
/**
Expand Down
2 changes: 1 addition & 1 deletion source/predicates/typed-array.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {TypedArray} from '../typed-array.js';
import type {TypedArray} from '../typed-array.js';
import {Predicate} from './predicate.js';

export class TypedArrayPredicate<T extends TypedArray> extends Predicate<T> {
Expand Down
2 changes: 1 addition & 1 deletion source/predicates/weak-map.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import hasItems from '../utils/has-items.js';
import {Predicate, PredicateOptions} from './predicate.js';
import {Predicate, type PredicateOptions} from './predicate.js';

export class WeakMapPredicate<KeyType extends object = object> extends Predicate<WeakMap<KeyType, unknown>> {
/**
Expand Down
2 changes: 1 addition & 1 deletion source/predicates/weak-set.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import hasItems from '../utils/has-items.js';
import {Predicate, PredicateOptions} from './predicate.js';
import {Predicate, type PredicateOptions} from './predicate.js';

export class WeakSetPredicate<T extends object = object> extends Predicate<WeakSet<T>> {
/**
Expand Down
2 changes: 1 addition & 1 deletion source/test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {testSymbol, BasePredicate} from './predicates/base-predicate.js';
import {testSymbol, type BasePredicate} from './predicates/base-predicate.js';

/**
Validate the value against the provided predicate.
Expand Down
4 changes: 2 additions & 2 deletions source/utils/has-items.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
@hidden
*/
export interface CollectionLike<T> {
export type CollectionLike<T> = {
has: (item: T) => boolean;
}
};

/**
Retrieve the missing values in a collection based on an array of items.
Expand Down
6 changes: 3 additions & 3 deletions source/utils/infer-label.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as fs from 'node:fs';
import {CallSite} from 'callsites';
import fs from 'node:fs';
import type {CallSite} from 'callsites';
import isValidIdentifier from './is-valid-identifier.js';
import isNode from './node/is-node.js';

Expand Down Expand Up @@ -58,7 +58,7 @@ export const inferLabel = (callsites: readonly CallSite[]): void | string => {
return;
}

if (isValidIdentifier(token) || isValidIdentifier(token.split('.').pop())) {
if (isValidIdentifier(token) || isValidIdentifier(token.split('.').pop() ?? '')) {
return token;
}

Expand Down
2 changes: 1 addition & 1 deletion source/utils/is-valid-identifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ Test if the string is a valid JavaScript identifier.
@param string - String to test.
*/
const isValidIdentifier = (string: string | undefined): string | boolean | undefined => string && !reservedSet.has(string) && identifierRegex.test(string);
const isValidIdentifier = (string: string): boolean => Boolean(string?.length > 0 && !reservedSet.has(string) && identifierRegex.test(string));

export default isValidIdentifier;
Loading

0 comments on commit 5f6ef36

Please sign in to comment.