Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output ESM instead of CommonJS #225

Merged
merged 22 commits into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm install --force
- run: npm test
- uses: codecov/codecov-action@v1
if: matrix.node-version == 14
Expand Down
3 changes: 1 addition & 2 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict';
const {default: ow} = require('./dist');
import ow from './dist/index.js'

const logError = fn => {
try {
Expand Down
34 changes: 18 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
"email": "[email protected]",
"url": "https://sindresorhus.com"
},
"type": "module",
"main": "dist/index.js",
"engines": {
"node": ">=12"
},
"scripts": {
"test": "xo && nyc ava",
"test": "xo && c8 --reporter=html --reporter=text ava",
SimonSiefke marked this conversation as resolved.
Show resolved Hide resolved
"docs": "typedoc source/index.ts",
"build": "del-cli dist && tsc",
"prepare": "npm run build",
Expand Down Expand Up @@ -61,15 +62,15 @@
"@types/lodash.isequal": "^4.5.5",
"@types/node": "^16.10.2",
"@types/vali-date": "^1.0.0",
"ava": "^2.4.0",
"ava": "^3.15.0",
"c8": "^7.10.0",
"del-cli": "^4.0.0",
"expect-type": "^0.12.0",
"gh-pages": "^3.2.3",
"nyc": "^15.1.0",
"ts-node": "^10.2.1",
"ts-node": "^10.3.1",
"typedoc": "^0.22.5",
"typescript": "^4.4.3",
"xo": "^0.38.2"
"typescript": "^4.5.0-beta",
"xo": "github:Spence-S/xo#9d4286767a4e580eb90b85dac8cd796c0c3e905b"
},
"browser": {
"./dist/utils/infer-label.js": "./dist/utils/infer-label.browser.js"
Expand All @@ -93,23 +94,24 @@
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/restrict-template-expressions": "off",
"@typescript-eslint/no-unused-vars": "off",
"import/no-anonymous-default-export": "off"
"@typescript-eslint/no-unused-vars": "off"
}
},
"ava": {
"babel": false,
"compileEnhancements": false,
"nonSemVerExperiments": {
"configurableModuleFormat": true
},
"nodeArguments": [
"--loader=ts-node/esm",
"--experimental-specifier-resolution=node"
],
"files": [
"test/**",
"!test/fixtures/**"
],
"extensions": [
"ts"
],
"require": [
"ts-node/register"
]
"extensions": {
"ts": "module"
}
},
"nyc": {
"reporter": [
Expand Down
2 changes: 1 addition & 1 deletion source/argument-error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {generateStackTrace} from './utils/generate-stack';
import {generateStackTrace} from './utils/generate-stack.js';

const wrapStackTrace = (error: ArgumentError, stack: string): string => `${error.name}: ${error.message}\n${stack}`;

Expand Down
22 changes: 11 additions & 11 deletions source/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import callsites from 'callsites';
import {inferLabel} from './utils/infer-label';
import {Predicate} from './predicates/predicate';
import {BasePredicate, isPredicate} from './predicates/base-predicate';
import modifiers, {Modifiers} from './modifiers';
import predicates, {Predicates} from './predicates';
import test from './test';
import {inferLabel} from './utils/infer-label.js';
import {Predicate} from './predicates/predicate.js';
import {BasePredicate, isPredicate} from './predicates/base-predicate.js';
import modifiers, {Modifiers} from './modifiers.js';
import predicates, {Predicates} from './predicates.js';
import test from './test.js';

/**
@hidden
Expand Down Expand Up @@ -124,7 +124,7 @@ Object.defineProperties(ow, {
} catch {
return false;
}
}
},
},
create: {
value: <T>(labelOrPredicate: BasePredicate<T> | string | undefined, predicate?: BasePredicate<T>) => (value: unknown, label?: string): asserts value is T => {
Expand All @@ -137,8 +137,8 @@ Object.defineProperties(ow, {
}

test(value, label ?? (labelOrPredicate!), predicate!);
}
}
},
},
});

// Can't use `export default predicates(modifiers(ow)) as Ow` because the variable needs a type annotation to avoid a compiler error when used:
Expand All @@ -149,5 +149,5 @@ const _ow: Ow = predicates(modifiers(ow)) as Ow;
export default _ow;

export {BasePredicate, Predicate};
export * from './predicates';
export {ArgumentError} from './argument-error';
export * from './predicates.js';
export {ArgumentError} from './argument-error.js';
12 changes: 7 additions & 5 deletions source/modifiers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {BasePredicate} from '.';
import predicates, {Predicates} from './predicates';
import predicates, {Predicates} from './predicates.js';
import {BasePredicate} from './index.js';

type Optionalify<P> = P extends BasePredicate<infer X>
? P & BasePredicate<X | undefined>
Expand All @@ -14,12 +14,14 @@ export interface Modifiers {
};
}

export default <T>(object: T): T & Modifiers => {
const modifiers = <T>(object: T): T & Modifiers => {
Object.defineProperties(object, {
optional: {
get: (): Predicates => predicates({}, {optional: true})
}
get: (): Predicates => predicates({}, {optional: true}),
},
});

return object as T & Modifiers;
};

export default modifiers;
12 changes: 6 additions & 6 deletions source/operators/not.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import randomId from '../utils/random-id';
import {validatorSymbol} from '../predicates/predicate';
import type {Predicate, Validator} from '../predicates/predicate';
import randomId from '../utils/random-id.js';
import {validatorSymbol} from '../predicates/predicate.js';
import type {Predicate, Validator} from '../predicates/predicate.js';

/**
Operator which inverts the following validation.
Expand All @@ -17,9 +17,9 @@ export const not = (predicate: Predicate): Predicate => {
const placeholder = randomId();

validator.message = (value: unknown, label: string): string => (
negatedMessage ?
negatedMessage(value, label) :
message(value, placeholder).replace(/ to /, '$&not ').replace(placeholder, label)
negatedMessage
? negatedMessage(value, label)
: message(value, placeholder).replace(/ to /, '$&not ').replace(placeholder, label)
);

validator.validator = (value: unknown): unknown => !fn(value);
Expand Down
117 changes: 60 additions & 57 deletions source/predicates.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import type {Buffer} from 'node:buffer';
import {TypedArray} from 'type-fest';
import {StringPredicate} from './predicates/string';
import {NumberPredicate} from './predicates/number';
import {BigIntPredicate} from './predicates/bigint';
import {BooleanPredicate} from './predicates/boolean';
import {Predicate, PredicateOptions} from './predicates/predicate';
import {ArrayPredicate} from './predicates/array';
import {ObjectPredicate, Shape} from './predicates/object';
import {DatePredicate} from './predicates/date';
import {ErrorPredicate} from './predicates/error';
import {MapPredicate} from './predicates/map';
import {WeakMapPredicate} from './predicates/weak-map';
import {SetPredicate} from './predicates/set';
import {WeakSetPredicate} from './predicates/weak-set';
import {TypedArrayPredicate} from './predicates/typed-array';
import {ArrayBufferPredicate} from './predicates/array-buffer';
import {DataViewPredicate} from './predicates/data-view';
import {BasePredicate} from './predicates/base-predicate';
import {AnyPredicate} from './predicates/any';
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 {ArrayPredicate} from './predicates/array.js';
import {ObjectPredicate, Shape} from './predicates/object.js';
import {DatePredicate} from './predicates/date.js';
import {ErrorPredicate} from './predicates/error.js';
import {MapPredicate} from './predicates/map.js';
import {WeakMapPredicate} from './predicates/weak-map.js';
import {SetPredicate} from './predicates/set.js';
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 {AnyPredicate} from './predicates/any.js';

export interface Predicates {
/**
Expand Down Expand Up @@ -210,121 +211,123 @@ export interface Predicates {
& ((...predicate: BasePredicate[]) => AnyPredicate);
}

export default <T>(object: T, options?: PredicateOptions): T & Predicates => {
const predicates = <T>(object: T, options?: PredicateOptions): T & Predicates => {
Object.defineProperties(object, {
string: {
get: (): StringPredicate => new StringPredicate(options)
get: (): StringPredicate => new StringPredicate(options),
},
number: {
get: (): NumberPredicate => new NumberPredicate(options)
get: (): NumberPredicate => new NumberPredicate(options),
},
bigint: {
get: (): BigIntPredicate => new BigIntPredicate(options)
get: (): BigIntPredicate => new BigIntPredicate(options),
},
boolean: {
get: (): BooleanPredicate => new BooleanPredicate(options)
get: (): BooleanPredicate => new BooleanPredicate(options),
},
undefined: {
get: (): Predicate => new Predicate('undefined', options)
get: (): Predicate => new Predicate('undefined', options),
},
null: {
get: (): Predicate => new Predicate('null', options)
get: (): Predicate => new Predicate('null', options),
},
nullOrUndefined: {
get: (): Predicate => new Predicate('nullOrUndefined', options)
get: (): Predicate => new Predicate('nullOrUndefined', options),
},
nan: {
get: (): Predicate => new Predicate('nan', options)
get: (): Predicate => new Predicate('nan', options),
},
symbol: {
get: (): Predicate => new Predicate('symbol', options)
get: (): Predicate => new Predicate('symbol', options),
},
array: {
get: (): ArrayPredicate => new ArrayPredicate(options)
get: (): ArrayPredicate => new ArrayPredicate(options),
},
object: {
get: (): ObjectPredicate => new ObjectPredicate(options)
get: (): ObjectPredicate => new ObjectPredicate(options),
},
date: {
get: (): DatePredicate => new DatePredicate(options)
get: (): DatePredicate => new DatePredicate(options),
},
error: {
get: (): ErrorPredicate => new ErrorPredicate(options)
get: (): ErrorPredicate => new ErrorPredicate(options),
},
map: {
get: (): MapPredicate => new MapPredicate(options)
get: (): MapPredicate => new MapPredicate(options),
},
weakMap: {
get: (): WeakMapPredicate => new WeakMapPredicate(options)
get: (): WeakMapPredicate => new WeakMapPredicate(options),
},
set: {
get: (): SetPredicate => new SetPredicate(options)
get: (): SetPredicate => new SetPredicate(options),
},
weakSet: {
get: (): WeakSetPredicate => new WeakSetPredicate(options)
get: (): WeakSetPredicate => new WeakSetPredicate(options),
},
function: {
get: (): Predicate => new Predicate('Function', options)
get: (): Predicate => new Predicate('Function', options),
},
buffer: {
get: (): Predicate => new Predicate('Buffer', options)
get: (): Predicate => new Predicate('Buffer', options),
},
regExp: {
get: (): Predicate<RegExp> => new Predicate('RegExp', options)
get: (): Predicate<RegExp> => new Predicate('RegExp', options),
},
promise: {
get: (): Predicate => new Predicate('Promise', options)
get: (): Predicate => new Predicate('Promise', options),
},
typedArray: {
get: (): TypedArrayPredicate<TypedArray> => new TypedArrayPredicate('TypedArray', options)
get: (): TypedArrayPredicate<TypedArray> => new TypedArrayPredicate('TypedArray', options),
},
int8Array: {
get: (): TypedArrayPredicate<Int8Array> => new TypedArrayPredicate('Int8Array', options)
get: (): TypedArrayPredicate<Int8Array> => new TypedArrayPredicate('Int8Array', options),
},
uint8Array: {
get: (): TypedArrayPredicate<Uint8Array> => new TypedArrayPredicate('Uint8Array', options)
get: (): TypedArrayPredicate<Uint8Array> => new TypedArrayPredicate('Uint8Array', options),
},
uint8ClampedArray: {
get: (): TypedArrayPredicate<Uint8Array> => new TypedArrayPredicate('Uint8ClampedArray', options)
get: (): TypedArrayPredicate<Uint8Array> => new TypedArrayPredicate('Uint8ClampedArray', options),
},
int16Array: {
get: (): TypedArrayPredicate<Int16Array> => new TypedArrayPredicate('Int16Array', options)
get: (): TypedArrayPredicate<Int16Array> => new TypedArrayPredicate('Int16Array', options),
},
uint16Array: {
get: (): TypedArrayPredicate<Uint16Array> => new TypedArrayPredicate('Uint16Array', options)
get: (): TypedArrayPredicate<Uint16Array> => new TypedArrayPredicate('Uint16Array', options),
},
int32Array: {
get: (): TypedArrayPredicate<Int32Array> => new TypedArrayPredicate('Int32Array', options)
get: (): TypedArrayPredicate<Int32Array> => new TypedArrayPredicate('Int32Array', options),
},
uint32Array: {
get: (): TypedArrayPredicate<Uint32Array> => new TypedArrayPredicate('Uint32Array', options)
get: (): TypedArrayPredicate<Uint32Array> => new TypedArrayPredicate('Uint32Array', options),
},
float32Array: {
get: (): TypedArrayPredicate<Float32Array> => new TypedArrayPredicate('Float32Array', options)
get: (): TypedArrayPredicate<Float32Array> => new TypedArrayPredicate('Float32Array', options),
},
float64Array: {
get: (): TypedArrayPredicate<Float64Array> => new TypedArrayPredicate('Float64Array', options)
get: (): TypedArrayPredicate<Float64Array> => new TypedArrayPredicate('Float64Array', options),
},
arrayBuffer: {
get: (): ArrayBufferPredicate<ArrayBuffer> => new ArrayBufferPredicate('ArrayBuffer', options)
get: (): ArrayBufferPredicate<ArrayBuffer> => new ArrayBufferPredicate('ArrayBuffer', options),
},
sharedArrayBuffer: {
get: (): ArrayBufferPredicate<SharedArrayBuffer> => new ArrayBufferPredicate('SharedArrayBuffer', options)
get: (): ArrayBufferPredicate<SharedArrayBuffer> => new ArrayBufferPredicate('SharedArrayBuffer', options),
},
dataView: {
get: (): DataViewPredicate => new DataViewPredicate(options)
get: (): DataViewPredicate => new DataViewPredicate(options),
},
iterable: {
get: (): Predicate => new Predicate('Iterable', options)
get: (): Predicate => new Predicate('Iterable', options),
},
any: {
value: (...predicates: BasePredicate[]): AnyPredicate => new AnyPredicate(predicates, options)
}
value: (...predicates: BasePredicate[]): AnyPredicate => new AnyPredicate(predicates, options),
},
});

return object as T & Predicates;
};

export default predicates;

export {
StringPredicate,
NumberPredicate,
Expand All @@ -342,5 +345,5 @@ export {
ArrayBufferPredicate,
DataViewPredicate,
AnyPredicate,
Shape
Shape,
};
Loading