Update to NPM version

This commit is contained in:
Lucas Patenaude
2024-04-11 04:23:19 -06:00
parent 886d197fa7
commit 6d6ef4f257
8225 changed files with 863748 additions and 1 deletions

101
ProjectSourceCode/node_modules/assert-options/README.md generated vendored Normal file
View File

@@ -0,0 +1,101 @@
# assert-options
Smart `options`-object handling, with one line of code:
* throw detailed error on invalid options
* set default values for missing options
Strongly-typed, built with TypeScript 4.x `strict` mode, for JavaScript clients.
## Rationale
* Passing in invalid or misspelled option names is one of the most common errors in JavaScript.
* Assigning defaults is the most common operation for methods that take options.
This module automates proper options handling - parsing + setting defaults in one line.
Although this library is implemented in TypeScript, its objective is mainly to help JavaScript clients,
because TypeScript itself can handle invalid options and defaults natively.
## Installation
```
$ npm i assert-options
```
## Usage
```js
const { assertOptions } = require('assert-options');
function functionWithOptions(options) {
options = assertOptions(options, {first: 123, second: null});
// options is a safe object here, with all missing defaults set.
}
```
When default values are not needed, you can just use an array of strings:
```js
function functionWithOptions(options) {
options = assertOptions(options, ['first', 'second']);
// the result is exactly the same as using the following:
// options = assertOptions(options, {first: undefined, second: undefined});
// options is a safe object here, without defaults.
}
```
You can override how errors are thrown, by creating the `assert` function yourself,
and specifying a custom handler:
```js
const {createAssert} = require('assert-options');
// must implement IOptionsErrorHandler protocol
class MyErrorHanler {
handle(err, ctx) {
// throw different errors, based on "err"
// for reference, see DefaultErrorHandler implementation
}
}
const assert = createAssert(new MyErrorHanler());
// note that the default assertOptions is created like this:
// const assertOptions = createAssert(new DefaultErrorHandler());
```
## API
### `assertOptions(options, defaults)`
* When `options` is `null`/`undefined`, new `{}` is returned, applying `defaults` as specified.
* When `options` contains an unknown property, [Error] `Option "name" is not recognized.` is thrown.
* When a property in `options` is missing or `undefined`, its value is set from the `defaults`,
provided it is available and its value is not `undefined`.
* When `options` is not `null`/`undefined`, it must be of type `object`, or else [TypeError] is thrown:
`Invalid "options" parameter: value`.
* Parameter `defaults` is required, as a non-`null` object or an array of strings, or else [TypeError]
is thrown: `Invalid "defaults" parameter: value`.
### `createAssert(handler)`
Creates a new assert function, using a custom error handler that implements `IOptionsErrorHandler` protocol.
For example, the default `assertOptions` is created internally like this:
```js
const {createOptions, DefaultErrorHandler} = require('assert-options');
const assertOptions = createAssert(new DefaultErrorHandler());
```
[Error]:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
[TypeError]:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError

View File

@@ -0,0 +1,10 @@
import { IOptionsErrorHandler } from './handler';
import { AssertFunc } from './types';
/**
* Creates an options-assert function that uses specified error handler.
*/
export declare function createAssert(errHandler: IOptionsErrorHandler): AssertFunc;
/**
* Default options-assert function.
*/
export declare const assertOptions: AssertFunc;

View File

@@ -0,0 +1,43 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.assertOptions = exports.createAssert = void 0;
const handler_1 = require("./handler");
const types_1 = require("./types");
/**
* Creates an options-assert function that uses specified error handler.
*/
function createAssert(errHandler) {
return function (options, defaults) {
if (options !== null && options !== undefined && typeof options !== 'object') {
return errHandler.handle(types_1.OptionsError.invalidOptionsParam, { options, defaults });
}
const isArray = Array.isArray(defaults);
if (!isArray && (!defaults || typeof defaults !== 'object')) {
return errHandler.handle(types_1.OptionsError.invalidDefaultsParam, { options, defaults });
}
if (options) {
for (const key of Object.keys(options)) {
if ((isArray && defaults.indexOf(key) === -1) || (!isArray && !(key in defaults))) {
return errHandler.handle(types_1.OptionsError.optionNotRecognized, { options, defaults, key });
}
}
}
else {
options = {};
}
if (!isArray) {
const defs = defaults;
for (const d of Object.keys(defs)) {
if (options[d] === undefined && defs[d] !== undefined) {
options[d] = defs[d];
}
}
}
return options;
};
}
exports.createAssert = createAssert;
/**
* Default options-assert function.
*/
exports.assertOptions = createAssert(new handler_1.DefaultErrorHandler());

View File

@@ -0,0 +1,16 @@
/**
* Protocol for handling options-related issues.
*/
import { IOptionsErrorContext, NamedValues, OptionsError } from './types';
export interface IOptionsErrorHandler {
/**
* This method is normally expected to throw an error, based on "err"
*/
handle(err: OptionsError, ctx: IOptionsErrorContext): NamedValues;
}
/**
* Default handler for options-related issues.
*/
export declare class DefaultErrorHandler implements IOptionsErrorHandler {
handle(err: OptionsError, ctx: IOptionsErrorContext): NamedValues;
}

View File

@@ -0,0 +1,26 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DefaultErrorHandler = void 0;
/**
* Protocol for handling options-related issues.
*/
const types_1 = require("./types");
/**
* Default handler for options-related issues.
*/
class DefaultErrorHandler {
handle(err, ctx) {
switch (err) {
case types_1.OptionsError.invalidOptionsParam:
throw new TypeError(`Invalid "options" parameter: ${JSON.stringify(ctx.options)}`);
case types_1.OptionsError.invalidDefaultsParam:
throw new TypeError(`Invalid "defaults" parameter: ${JSON.stringify(ctx.defaults)}`);
case types_1.OptionsError.optionNotRecognized:
throw new Error(`Option "${ctx.key}" is not recognized.`);
// istanbul ignore next:
default:
return ctx.options; // this will never happen
}
}
}
exports.DefaultErrorHandler = DefaultErrorHandler;

View File

@@ -0,0 +1,3 @@
export { assertOptions, createAssert } from './assert';
export { OptionsError, IOptionsErrorContext, NamedValues, AssertFunc } from './types';
export { IOptionsErrorHandler, DefaultErrorHandler } from './handler';

View File

@@ -0,0 +1,10 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DefaultErrorHandler = exports.OptionsError = exports.createAssert = exports.assertOptions = void 0;
var assert_1 = require("./assert");
Object.defineProperty(exports, "assertOptions", { enumerable: true, get: function () { return assert_1.assertOptions; } });
Object.defineProperty(exports, "createAssert", { enumerable: true, get: function () { return assert_1.createAssert; } });
var types_1 = require("./types");
Object.defineProperty(exports, "OptionsError", { enumerable: true, get: function () { return types_1.OptionsError; } });
var handler_1 = require("./handler");
Object.defineProperty(exports, "DefaultErrorHandler", { enumerable: true, get: function () { return handler_1.DefaultErrorHandler; } });

View File

@@ -0,0 +1,26 @@
/**
* All errors that can occur inside an assert function.
*/
export declare enum OptionsError {
invalidOptionsParam = 0,
invalidDefaultsParam = 1,
optionNotRecognized = 2
}
/**
* Error-related context available for options-related issues.
*/
export interface IOptionsErrorContext {
options: any;
defaults: any;
key?: string;
}
/**
* Standard syntax for named values.
*/
export type NamedValues = {
[name: string]: any;
};
/**
* Assert function signature.
*/
export type AssertFunc = (options: NamedValues | null | undefined, defaults: NamedValues | string[]) => NamedValues;

View File

@@ -0,0 +1,12 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OptionsError = void 0;
/**
* All errors that can occur inside an assert function.
*/
var OptionsError;
(function (OptionsError) {
OptionsError[OptionsError["invalidOptionsParam"] = 0] = "invalidOptionsParam";
OptionsError[OptionsError["invalidDefaultsParam"] = 1] = "invalidDefaultsParam";
OptionsError[OptionsError["optionNotRecognized"] = 2] = "optionNotRecognized";
})(OptionsError = exports.OptionsError || (exports.OptionsError = {}));

View File

@@ -0,0 +1,49 @@
{
"name": "assert-options",
"version": "0.8.0",
"description": "Generic options parameter handling.",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
"scripts": {
"all": "tsc && npm test && npm run lint",
"lint": "tslint --fix ./src/**/*.ts ./test/**/*.ts",
"test": "nyc mocha -r ts-node/register test/**/*.spec.ts"
},
"files": [
"dist/src",
"dist/index.js"
],
"homepage": "https://github.com/vitaly-t/assert-options",
"repository": {
"type": "git",
"url": "https://github.com/vitaly-t/assert-options.git"
},
"bugs": {
"url": "https://github.com/vitaly-t/assert-options/issues",
"email": "vitaly.tomilov@gmail.com"
},
"keywords": [
"assert",
"options"
],
"author": {
"name": "Vitaly Tomilov",
"email": "vitaly.tomilov@gmail.com"
},
"license": "MIT",
"engines": {
"node": ">=10.0.0"
},
"devDependencies": {
"@types/chai": "4.3.4",
"@types/mocha": "10.0.0",
"@types/node": "18.11.9",
"chai": "4.3.7",
"mocha": "10.1.0",
"mocha-lcov-reporter": "1.3.0",
"nyc": "15.1.0",
"ts-node": "10.9.1",
"tslint": "6.1.3",
"typescript": "4.9.3"
}
}