added all files
This commit is contained in:
+8
@@ -0,0 +1,8 @@
|
||||
/**
|
||||
Generates a complete message from all errors generated by predicates.
|
||||
|
||||
@param errors - The errors generated by the predicates.
|
||||
@param isAny - If this function is called from the any argument.
|
||||
@hidden
|
||||
*/
|
||||
export declare const generateArgumentErrorMessage: (errors: Map<string, Set<string>>, isAny?: boolean) => string;
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.generateArgumentErrorMessage = void 0;
|
||||
/**
|
||||
Generates a complete message from all errors generated by predicates.
|
||||
|
||||
@param errors - The errors generated by the predicates.
|
||||
@param isAny - If this function is called from the any argument.
|
||||
@hidden
|
||||
*/
|
||||
const generateArgumentErrorMessage = (errors, isAny = false) => {
|
||||
const message = [];
|
||||
const errorArray = [...errors.entries()];
|
||||
const anyErrorWithoutOneItemOnly = errorArray.some(([, array]) => array.size !== 1);
|
||||
// If only one error "key" is present, enumerate all of those errors only.
|
||||
if (errorArray.length === 1) {
|
||||
const [, returnedErrors] = errorArray[0];
|
||||
if (!isAny && returnedErrors.size === 1) {
|
||||
const [errorMessage] = returnedErrors;
|
||||
return errorMessage;
|
||||
}
|
||||
for (const entry of returnedErrors) {
|
||||
message.push(`${isAny ? ' - ' : ''}${entry}`);
|
||||
}
|
||||
return message.join('\n');
|
||||
}
|
||||
// If every predicate returns just one error, enumerate them as is.
|
||||
if (!anyErrorWithoutOneItemOnly) {
|
||||
return errorArray.map(([, [item]]) => ` - ${item}`).join('\n');
|
||||
}
|
||||
// Else, iterate through all the errors and enumerate them.
|
||||
for (const [key, value] of errorArray) {
|
||||
message.push(`Errors from the "${key}" predicate:`);
|
||||
for (const entry of value) {
|
||||
message.push(` - ${entry}`);
|
||||
}
|
||||
}
|
||||
return message.join('\n');
|
||||
};
|
||||
exports.generateArgumentErrorMessage = generateArgumentErrorMessage;
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
/**
|
||||
Generates a useful stacktrace that points to the user's code where the error happened on platforms without the `Error.captureStackTrace()` method.
|
||||
|
||||
@hidden
|
||||
*/
|
||||
export declare const generateStackTrace: () => string;
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.generateStackTrace = void 0;
|
||||
/**
|
||||
Generates a useful stacktrace that points to the user's code where the error happened on platforms without the `Error.captureStackTrace()` method.
|
||||
|
||||
@hidden
|
||||
*/
|
||||
const generateStackTrace = () => {
|
||||
const stack = new RangeError('INTERNAL_OW_ERROR').stack;
|
||||
return stack;
|
||||
};
|
||||
exports.generateStackTrace = generateStackTrace;
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
@hidden
|
||||
*/
|
||||
export interface CollectionLike<T> {
|
||||
has: (item: T) => boolean;
|
||||
}
|
||||
declare const _default: <T>(source: CollectionLike<T>, items: readonly T[], maxValues?: number) => true | T[];
|
||||
/**
|
||||
Retrieve the missing values in a collection based on an array of items.
|
||||
|
||||
@hidden
|
||||
|
||||
@param source - Source collection to search through.
|
||||
@param items - Items to search for.
|
||||
@param maxValues - Maximum number of values after the search process is stopped. Default: 5.
|
||||
*/
|
||||
export default _default;
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
/**
|
||||
Retrieve the missing values in a collection based on an array of items.
|
||||
|
||||
@hidden
|
||||
|
||||
@param source - Source collection to search through.
|
||||
@param items - Items to search for.
|
||||
@param maxValues - Maximum number of values after the search process is stopped. Default: 5.
|
||||
*/
|
||||
exports.default = (source, items, maxValues = 5) => {
|
||||
const missingValues = [];
|
||||
for (const value of items) {
|
||||
if (source.has(value)) {
|
||||
continue;
|
||||
}
|
||||
missingValues.push(value);
|
||||
if (missingValues.length === maxValues) {
|
||||
return missingValues;
|
||||
}
|
||||
}
|
||||
return missingValues.length === 0 ? true : missingValues;
|
||||
};
|
||||
+1
@@ -0,0 +1 @@
|
||||
export declare const inferLabel: () => void;
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.inferLabel = void 0;
|
||||
const inferLabel = () => { };
|
||||
exports.inferLabel = inferLabel;
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
import { CallSite } from 'callsites';
|
||||
/**
|
||||
Infer the label of the caller.
|
||||
|
||||
@hidden
|
||||
|
||||
@param callsites - List of stack frames.
|
||||
*/
|
||||
export declare const inferLabel: (callsites: readonly CallSite[]) => void | string;
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.inferLabel = void 0;
|
||||
const fs = require("fs");
|
||||
const is_valid_identifier_1 = require("./is-valid-identifier");
|
||||
const is_node_1 = require("./node/is-node");
|
||||
// Regex to extract the label out of the `ow` function call
|
||||
const labelRegex = /^.*?\((?<label>.*?)[,)]/;
|
||||
/**
|
||||
Infer the label of the caller.
|
||||
|
||||
@hidden
|
||||
|
||||
@param callsites - List of stack frames.
|
||||
*/
|
||||
const inferLabel = (callsites) => {
|
||||
var _a;
|
||||
if (!is_node_1.default) {
|
||||
// Exit if we are not running in a Node.js environment
|
||||
return;
|
||||
}
|
||||
// Grab the stackframe with the `ow` function call
|
||||
const functionCallStackFrame = callsites[1];
|
||||
if (!functionCallStackFrame) {
|
||||
return;
|
||||
}
|
||||
const fileName = functionCallStackFrame.getFileName();
|
||||
const lineNumber = functionCallStackFrame.getLineNumber();
|
||||
const columnNumber = functionCallStackFrame.getColumnNumber();
|
||||
if (fileName === null || lineNumber === null || columnNumber === null) {
|
||||
return;
|
||||
}
|
||||
let content = [];
|
||||
try {
|
||||
content = fs.readFileSync(fileName, 'utf8').split('\n');
|
||||
}
|
||||
catch {
|
||||
return;
|
||||
}
|
||||
let line = content[lineNumber - 1];
|
||||
if (!line) {
|
||||
// Exit if the line number couldn't be found
|
||||
return;
|
||||
}
|
||||
line = line.slice(columnNumber - 1);
|
||||
const match = labelRegex.exec(line);
|
||||
if (!((_a = match === null || match === void 0 ? void 0 : match.groups) === null || _a === void 0 ? void 0 : _a.label)) {
|
||||
// Exit if we didn't find a label
|
||||
return;
|
||||
}
|
||||
const token = match.groups.label;
|
||||
if (is_valid_identifier_1.default(token) || is_valid_identifier_1.default(token.split('.').pop())) {
|
||||
return token;
|
||||
}
|
||||
return;
|
||||
};
|
||||
exports.inferLabel = inferLabel;
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
declare const _default: (string: string | undefined) => string | boolean | undefined;
|
||||
/**
|
||||
Test if the string is a valid JavaScript identifier.
|
||||
|
||||
@param string - String to test.
|
||||
*/
|
||||
export default _default;
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const identifierRegex = /^[a-z$_][$\w]*$/i;
|
||||
const reservedSet = new Set([
|
||||
'undefined',
|
||||
'null',
|
||||
'true',
|
||||
'false',
|
||||
'super',
|
||||
'this',
|
||||
'Infinity',
|
||||
'NaN'
|
||||
]);
|
||||
/**
|
||||
Test if the string is a valid JavaScript identifier.
|
||||
|
||||
@param string - String to test.
|
||||
*/
|
||||
exports.default = (string) => string && !reservedSet.has(string) && identifierRegex.test(string);
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
import { BasePredicate } from '..';
|
||||
export interface Shape {
|
||||
[key: string]: BasePredicate | Shape;
|
||||
}
|
||||
/**
|
||||
Extracts a regular type from a shape definition.
|
||||
|
||||
@example
|
||||
```
|
||||
const myShape = {
|
||||
foo: ow.string,
|
||||
bar: {
|
||||
baz: ow.boolean
|
||||
}
|
||||
}
|
||||
|
||||
type X = TypeOfShape<typeof myShape> // {foo: string; bar: {baz: boolean}}
|
||||
```
|
||||
|
||||
This is used in the `ow.object.partialShape(…)` and `ow.object.exactShape(…)` functions.
|
||||
*/
|
||||
export declare type TypeOfShape<S extends BasePredicate | Shape> = S extends BasePredicate<infer X> ? X : S extends Shape ? {
|
||||
[K in keyof S]: TypeOfShape<S[K]>;
|
||||
} : never;
|
||||
/**
|
||||
Test if the `object` matches the `shape` partially.
|
||||
|
||||
@hidden
|
||||
|
||||
@param object - Object to test against the provided shape.
|
||||
@param shape - Shape to test the object against.
|
||||
@param parent - Name of the parent property.
|
||||
*/
|
||||
export declare function partial(object: Record<string, any>, shape: Shape, parent?: string): boolean | string;
|
||||
/**
|
||||
Test if the `object` matches the `shape` exactly.
|
||||
|
||||
@hidden
|
||||
|
||||
@param object - Object to test against the provided shape.
|
||||
@param shape - Shape to test the object against.
|
||||
@param parent - Name of the parent property.
|
||||
*/
|
||||
export declare function exact(object: Record<string, any>, shape: Shape, parent?: string, isArray?: boolean): boolean | string;
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.exact = exports.partial = void 0;
|
||||
const is_1 = require("@sindresorhus/is");
|
||||
const test_1 = require("../test");
|
||||
const base_predicate_1 = require("../predicates/base-predicate");
|
||||
/**
|
||||
Test if the `object` matches the `shape` partially.
|
||||
|
||||
@hidden
|
||||
|
||||
@param object - Object to test against the provided shape.
|
||||
@param shape - Shape to test the object against.
|
||||
@param parent - Name of the parent property.
|
||||
*/
|
||||
function partial(object, shape, parent) {
|
||||
try {
|
||||
for (const key of Object.keys(shape)) {
|
||||
const label = parent ? `${parent}.${key}` : key;
|
||||
if (base_predicate_1.isPredicate(shape[key])) {
|
||||
test_1.default(object[key], label, shape[key]);
|
||||
}
|
||||
else if (is_1.default.plainObject(shape[key])) {
|
||||
const result = partial(object[key], shape[key], label);
|
||||
if (result !== true) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (error) {
|
||||
return error.message;
|
||||
}
|
||||
}
|
||||
exports.partial = partial;
|
||||
/**
|
||||
Test if the `object` matches the `shape` exactly.
|
||||
|
||||
@hidden
|
||||
|
||||
@param object - Object to test against the provided shape.
|
||||
@param shape - Shape to test the object against.
|
||||
@param parent - Name of the parent property.
|
||||
*/
|
||||
function exact(object, shape, parent, isArray) {
|
||||
try {
|
||||
const objectKeys = new Set(Object.keys(object));
|
||||
for (const key of Object.keys(shape)) {
|
||||
objectKeys.delete(key);
|
||||
const label = parent ? `${parent}.${key}` : key;
|
||||
if (base_predicate_1.isPredicate(shape[key])) {
|
||||
test_1.default(object[key], label, shape[key]);
|
||||
}
|
||||
else if (is_1.default.plainObject(shape[key])) {
|
||||
if (!Object.prototype.hasOwnProperty.call(object, key)) {
|
||||
return `Expected \`${label}\` to exist`;
|
||||
}
|
||||
const result = exact(object[key], shape[key], label);
|
||||
if (result !== true) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (objectKeys.size > 0) {
|
||||
const firstKey = [...objectKeys.keys()][0];
|
||||
const label = parent ? `${parent}.${firstKey}` : firstKey;
|
||||
return `Did not expect ${isArray ? 'element' : 'property'} \`${label}\` to exist, got \`${object[firstKey]}\``;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (error) {
|
||||
return error.message;
|
||||
}
|
||||
}
|
||||
exports.exact = exact;
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
declare const _default: boolean;
|
||||
export default _default;
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
"use strict";
|
||||
var _a;
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = Boolean((_a = process === null || process === void 0 ? void 0 : process.versions) === null || _a === void 0 ? void 0 : _a.node);
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
import { Predicate } from '../predicates/predicate';
|
||||
declare const _default: (object: unknown, predicate: Predicate) => boolean | string;
|
||||
/**
|
||||
Test all the values in the object against a provided predicate.
|
||||
|
||||
@hidden
|
||||
|
||||
@param predicate - Predicate to test every value in the given object against.
|
||||
*/
|
||||
export default _default;
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const is_1 = require("@sindresorhus/is");
|
||||
const test_1 = require("../test");
|
||||
const ofTypeDeep = (object, predicate) => {
|
||||
if (!is_1.default.plainObject(object)) {
|
||||
test_1.default(object, 'deep values', predicate, false);
|
||||
return true;
|
||||
}
|
||||
return Object.values(object).every(value => ofTypeDeep(value, predicate));
|
||||
};
|
||||
/**
|
||||
Test all the values in the object against a provided predicate.
|
||||
|
||||
@hidden
|
||||
|
||||
@param predicate - Predicate to test every value in the given object against.
|
||||
*/
|
||||
exports.default = (object, predicate) => {
|
||||
try {
|
||||
return ofTypeDeep(object, predicate);
|
||||
}
|
||||
catch (error) {
|
||||
return error.message;
|
||||
}
|
||||
};
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
import { BasePredicate } from '../predicates/base-predicate';
|
||||
declare const _default: <T>(source: IterableIterator<T> | Set<T> | T[], name: string, predicate: BasePredicate<T>) => boolean | string;
|
||||
/**
|
||||
Test all the values in the collection against a provided predicate.
|
||||
|
||||
@hidden
|
||||
@param source Source collection to test.
|
||||
@param name The name to call the collection of values, such as `values` or `keys`.
|
||||
@param predicate Predicate to test every item in the source collection against.
|
||||
*/
|
||||
export default _default;
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const test_1 = require("../test");
|
||||
/**
|
||||
Test all the values in the collection against a provided predicate.
|
||||
|
||||
@hidden
|
||||
@param source Source collection to test.
|
||||
@param name The name to call the collection of values, such as `values` or `keys`.
|
||||
@param predicate Predicate to test every item in the source collection against.
|
||||
*/
|
||||
exports.default = (source, name, predicate) => {
|
||||
try {
|
||||
for (const item of source) {
|
||||
test_1.default(item, name, predicate, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (error) {
|
||||
return error.message;
|
||||
}
|
||||
};
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
declare const _default: () => string;
|
||||
export default _default;
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = () => Math.random().toString(16).slice(2);
|
||||
Reference in New Issue
Block a user