Browse Source

chore: adds InvalidArgumentError class

e22m4u 3 months ago
parent
commit
034ba6abb5

+ 21 - 2
README.md

@@ -155,11 +155,30 @@ format('An array of %l', ['foo', 10, true]);
 import {Errorf} from '@e22m4u/js-format';
 import {Errorf} from '@e22m4u/js-format';
 
 
 throw new Errorf(
 throw new Errorf(
-  'It requires one of %l, but %v given.',
+  'It requires one of %l, but %v was given.',
   [true, false, 'y', 'n'],
   [true, false, 'y', 'n'],
   new Map(),
   new Map(),
 );
 );
-// Error: It requires one of true, false, "y", "n", but Map given.
+// Error: It requires one of true, false, "y", "n", but Map was given.
+```
+
+## `InvalidArgumentError`
+
+Класс является псевдонимом для `Errorf`, полностью наследуя его поведение.
+В некоторых ситуациях может быть более осмысленным, чем базовый класс.
+
+```js
+import {InvalidArgumentError} from '@e22m4u/js-format';
+
+function capitalize(input) {
+  if (typeof input !== 'string')
+    throw new InvalidArgumentError(
+      'The `capitalize` function requires the input argument ' +
+        'to be a String, but %v was given.',
+      input,
+    );
+  // ...
+}
 ```
 ```
 
 
 ## Тесты
 ## Тесты

+ 18 - 6
dist/cjs/index.cjs

@@ -21,7 +21,10 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
 var index_exports = {};
 var index_exports = {};
 __export(index_exports, {
 __export(index_exports, {
   Errorf: () => Errorf,
   Errorf: () => Errorf,
-  format: () => format
+  InvalidArgumentError: () => InvalidArgumentError,
+  arrayToString: () => arrayToString,
+  format: () => format,
+  valueToString: () => valueToString
 });
 });
 module.exports = __toCommonJS(index_exports);
 module.exports = __toCommonJS(index_exports);
 
 
@@ -58,14 +61,14 @@ function valueToString(input) {
 }
 }
 __name(valueToString, "valueToString");
 __name(valueToString, "valueToString");
 
 
-// src/array-to-list.js
+// src/array-to-string.js
 var SEPARATOR = ", ";
 var SEPARATOR = ", ";
-function arrayToList(input) {
+function arrayToString(input) {
   if (Array.isArray(input) && input.length)
   if (Array.isArray(input) && input.length)
     return input.map(valueToString).join(SEPARATOR);
     return input.map(valueToString).join(SEPARATOR);
   return valueToString(input);
   return valueToString(input);
 }
 }
-__name(arrayToList, "arrayToList");
+__name(arrayToString, "arrayToString");
 
 
 // src/format.js
 // src/format.js
 function format(pattern) {
 function format(pattern) {
@@ -93,7 +96,7 @@ function format(pattern) {
           arg = valueToString(arg);
           arg = valueToString(arg);
           break;
           break;
         case "l":
         case "l":
-          arg = arrayToList(arg);
+          arg = arrayToString(arg);
           break;
           break;
       }
       }
       if (!escaped) return arg;
       if (!escaped) return arg;
@@ -122,8 +125,17 @@ var _Errorf = class _Errorf extends Error {
 };
 };
 __name(_Errorf, "Errorf");
 __name(_Errorf, "Errorf");
 var Errorf = _Errorf;
 var Errorf = _Errorf;
+
+// src/invalid-argument-error.js
+var _InvalidArgumentError = class _InvalidArgumentError extends Errorf {
+};
+__name(_InvalidArgumentError, "InvalidArgumentError");
+var InvalidArgumentError = _InvalidArgumentError;
 // Annotate the CommonJS export names for ESM import in node:
 // Annotate the CommonJS export names for ESM import in node:
 0 && (module.exports = {
 0 && (module.exports = {
   Errorf,
   Errorf,
-  format
+  InvalidArgumentError,
+  arrayToString,
+  format,
+  valueToString
 });
 });

+ 0 - 6
src/array-to-list.d.ts

@@ -1,6 +0,0 @@
-/**
- * Array to list.
- *
- * @param input
- */
-export declare function arrayToList(input: any): string;

+ 6 - 0
src/array-to-string.d.ts

@@ -0,0 +1,6 @@
+/**
+ * Array to string.
+ *
+ * @param input
+ */
+export declare function arrayToString(input: any): string;

+ 2 - 2
src/array-to-list.js → src/array-to-string.js

@@ -8,12 +8,12 @@ import {valueToString} from './value-to-string.js';
 const SEPARATOR = ', ';
 const SEPARATOR = ', ';
 
 
 /**
 /**
- * Array to list.
+ * Array to string.
  *
  *
  * @param {any} input
  * @param {any} input
  * @return {string}
  * @return {string}
  */
  */
-export function arrayToList(input) {
+export function arrayToString(input) {
   if (Array.isArray(input) && input.length)
   if (Array.isArray(input) && input.length)
     return input.map(valueToString).join(SEPARATOR);
     return input.map(valueToString).join(SEPARATOR);
   return valueToString(input);
   return valueToString(input);

+ 52 - 52
src/array-to-list.spec.js → src/array-to-string.spec.js

@@ -1,265 +1,265 @@
 import {expect} from 'chai';
 import {expect} from 'chai';
-import {arrayToList} from './array-to-list.js';
+import {arrayToString} from './array-to-string.js';
 
 
-describe('arrayToList', function () {
+describe('arrayToString', function () {
   describe('non-array values', function () {
   describe('non-array values', function () {
     it('returns a string representation of the given string', function () {
     it('returns a string representation of the given string', function () {
-      const res = arrayToList('foo');
+      const res = arrayToString('foo');
       expect(res).to.be.eq('"foo"');
       expect(res).to.be.eq('"foo"');
     });
     });
 
 
     it('returns a string representation of the given empty string', function () {
     it('returns a string representation of the given empty string', function () {
-      const res = arrayToList('');
+      const res = arrayToString('');
       expect(res).to.be.eq('""');
       expect(res).to.be.eq('""');
     });
     });
 
 
     it('returns a string representation of the given number', function () {
     it('returns a string representation of the given number', function () {
-      const res = arrayToList(10);
+      const res = arrayToString(10);
       expect(res).to.be.eq('10');
       expect(res).to.be.eq('10');
     });
     });
 
 
     it('returns a string representation of the given zero', function () {
     it('returns a string representation of the given zero', function () {
-      const res = arrayToList(0);
+      const res = arrayToString(0);
       expect(res).to.be.eq('0');
       expect(res).to.be.eq('0');
     });
     });
 
 
     it('returns a string representation of the given NaN', function () {
     it('returns a string representation of the given NaN', function () {
-      const res = arrayToList(NaN);
+      const res = arrayToString(NaN);
       expect(res).to.be.eq('NaN');
       expect(res).to.be.eq('NaN');
     });
     });
 
 
     it('returns a string representation of the given Infinity', function () {
     it('returns a string representation of the given Infinity', function () {
-      const res = arrayToList(Infinity);
+      const res = arrayToString(Infinity);
       expect(res).to.be.eq('Infinity');
       expect(res).to.be.eq('Infinity');
     });
     });
 
 
     it('returns a string representation of the given true', function () {
     it('returns a string representation of the given true', function () {
-      const res = arrayToList(true);
+      const res = arrayToString(true);
       expect(res).to.be.eq('true');
       expect(res).to.be.eq('true');
     });
     });
 
 
     it('returns a string representation of the given false', function () {
     it('returns a string representation of the given false', function () {
-      const res = arrayToList(false);
+      const res = arrayToString(false);
       expect(res).to.be.eq('false');
       expect(res).to.be.eq('false');
     });
     });
 
 
     it('returns a string representation of the given key-value object', function () {
     it('returns a string representation of the given key-value object', function () {
-      const res = arrayToList({foo: 'bar'});
+      const res = arrayToString({foo: 'bar'});
       expect(res).to.be.eq('Object');
       expect(res).to.be.eq('Object');
     });
     });
 
 
     it('returns a string representation of the given object without keys', function () {
     it('returns a string representation of the given object without keys', function () {
-      const res = arrayToList({});
+      const res = arrayToString({});
       expect(res).to.be.eq('Object');
       expect(res).to.be.eq('Object');
     });
     });
 
 
     it('returns a string representation of the given object without prototype', function () {
     it('returns a string representation of the given object without prototype', function () {
-      const res = arrayToList(Object.create(null));
+      const res = arrayToString(Object.create(null));
       expect(res).to.be.eq('Object');
       expect(res).to.be.eq('Object');
     });
     });
 
 
     it('returns a string representation of the given date instance', function () {
     it('returns a string representation of the given date instance', function () {
-      const res = arrayToList(new Date());
+      const res = arrayToString(new Date());
       expect(res).to.be.eq('Date');
       expect(res).to.be.eq('Date');
     });
     });
 
 
     it('returns a string representation of the given map instance', function () {
     it('returns a string representation of the given map instance', function () {
-      const res = arrayToList(new Map());
+      const res = arrayToString(new Map());
       expect(res).to.be.eq('Map');
       expect(res).to.be.eq('Map');
     });
     });
 
 
     it('returns a string representation of the given class instance', function () {
     it('returns a string representation of the given class instance', function () {
       class MyClass {}
       class MyClass {}
-      const res = arrayToList(new MyClass());
+      const res = arrayToString(new MyClass());
       expect(res).to.be.eq('MyClass (instance)');
       expect(res).to.be.eq('MyClass (instance)');
     });
     });
 
 
     it('returns a string representation of the given function', function () {
     it('returns a string representation of the given function', function () {
-      const res = arrayToList(function () {});
+      const res = arrayToString(function () {});
       expect(res).to.be.eq('Function');
       expect(res).to.be.eq('Function');
     });
     });
 
 
     it('returns a string representation of the given anonymous function', function () {
     it('returns a string representation of the given anonymous function', function () {
-      const res = arrayToList(() => undefined);
+      const res = arrayToString(() => undefined);
       expect(res).to.be.eq('Function');
       expect(res).to.be.eq('Function');
     });
     });
 
 
     it('returns a string representation of the given named function', function () {
     it('returns a string representation of the given named function', function () {
       function foo() {}
       function foo() {}
-      const res = arrayToList(foo);
+      const res = arrayToString(foo);
       expect(res).to.be.eq('Function');
       expect(res).to.be.eq('Function');
     });
     });
 
 
     it('returns a string representation of the given anonymous class', function () {
     it('returns a string representation of the given anonymous class', function () {
-      const res = arrayToList(class {});
+      const res = arrayToString(class {});
       expect(res).to.be.eq('Class');
       expect(res).to.be.eq('Class');
     });
     });
 
 
     it('returns a string representation of the given named class', function () {
     it('returns a string representation of the given named class', function () {
       class MyClass {}
       class MyClass {}
-      const res = arrayToList(MyClass);
+      const res = arrayToString(MyClass);
       expect(res).to.be.eq('MyClass');
       expect(res).to.be.eq('MyClass');
     });
     });
 
 
     it('returns a string representation of the given class constructor', function () {
     it('returns a string representation of the given class constructor', function () {
       class MyClass {}
       class MyClass {}
-      const res = arrayToList(MyClass.constructor);
+      const res = arrayToString(MyClass.constructor);
       expect(res).to.be.eq('Function');
       expect(res).to.be.eq('Function');
     });
     });
 
 
     it('returns a string representation of the given symbol', function () {
     it('returns a string representation of the given symbol', function () {
-      const res = arrayToList(Symbol());
+      const res = arrayToString(Symbol());
       expect(res).to.be.eq('Symbol');
       expect(res).to.be.eq('Symbol');
     });
     });
 
 
     it('returns a string representation of the given named symbol', function () {
     it('returns a string representation of the given named symbol', function () {
-      const res = arrayToList(Symbol('foo'));
+      const res = arrayToString(Symbol('foo'));
       expect(res).to.be.eq('Symbol');
       expect(res).to.be.eq('Symbol');
     });
     });
 
 
     it('returns a string representation of the given undefined', function () {
     it('returns a string representation of the given undefined', function () {
-      const res = arrayToList(undefined);
+      const res = arrayToString(undefined);
       expect(res).to.be.eq('undefined');
       expect(res).to.be.eq('undefined');
     });
     });
 
 
     it('returns a string representation of the given null', function () {
     it('returns a string representation of the given null', function () {
-      const res = arrayToList(null);
+      const res = arrayToString(null);
       expect(res).to.be.eq('null');
       expect(res).to.be.eq('null');
     });
     });
   });
   });
 
 
   describe('array values', function () {
   describe('array values', function () {
     it('adds a separator between the given elements', function () {
     it('adds a separator between the given elements', function () {
-      const res = arrayToList(['foo', 1, true]);
+      const res = arrayToString(['foo', 1, true]);
       expect(res).to.be.eq('"foo", 1, true');
       expect(res).to.be.eq('"foo", 1, true');
     });
     });
 
 
     it('returns a string representation of the given empty array', function () {
     it('returns a string representation of the given empty array', function () {
-      const res = arrayToList([]);
+      const res = arrayToString([]);
       expect(res).to.be.eq('Array');
       expect(res).to.be.eq('Array');
     });
     });
 
 
     it('returns an element representation of the given string', function () {
     it('returns an element representation of the given string', function () {
-      const res = arrayToList(['foo']);
+      const res = arrayToString(['foo']);
       expect(res).to.be.eq('"foo"');
       expect(res).to.be.eq('"foo"');
     });
     });
 
 
     it('returns an element representation of the given empty string', function () {
     it('returns an element representation of the given empty string', function () {
-      const res = arrayToList(['']);
+      const res = arrayToString(['']);
       expect(res).to.be.eq('""');
       expect(res).to.be.eq('""');
     });
     });
 
 
     it('returns an element representation of the given number', function () {
     it('returns an element representation of the given number', function () {
-      const res = arrayToList([10]);
+      const res = arrayToString([10]);
       expect(res).to.be.eq('10');
       expect(res).to.be.eq('10');
     });
     });
 
 
     it('returns an element representation of the given zero', function () {
     it('returns an element representation of the given zero', function () {
-      const res = arrayToList([0]);
+      const res = arrayToString([0]);
       expect(res).to.be.eq('0');
       expect(res).to.be.eq('0');
     });
     });
 
 
     it('returns an element representation of the given NaN', function () {
     it('returns an element representation of the given NaN', function () {
-      const res = arrayToList([NaN]);
+      const res = arrayToString([NaN]);
       expect(res).to.be.eq('NaN');
       expect(res).to.be.eq('NaN');
     });
     });
 
 
     it('returns an element representation of the given Infinity', function () {
     it('returns an element representation of the given Infinity', function () {
-      const res = arrayToList([Infinity]);
+      const res = arrayToString([Infinity]);
       expect(res).to.be.eq('Infinity');
       expect(res).to.be.eq('Infinity');
     });
     });
 
 
     it('returns an element representation of the given true', function () {
     it('returns an element representation of the given true', function () {
-      const res = arrayToList([true]);
+      const res = arrayToString([true]);
       expect(res).to.be.eq('true');
       expect(res).to.be.eq('true');
     });
     });
 
 
     it('returns an element representation of the given false', function () {
     it('returns an element representation of the given false', function () {
-      const res = arrayToList([false]);
+      const res = arrayToString([false]);
       expect(res).to.be.eq('false');
       expect(res).to.be.eq('false');
     });
     });
 
 
     it('returns an element representation of the given key-value object', function () {
     it('returns an element representation of the given key-value object', function () {
-      const res = arrayToList([{foo: 'bar'}]);
+      const res = arrayToString([{foo: 'bar'}]);
       expect(res).to.be.eq('Object');
       expect(res).to.be.eq('Object');
     });
     });
 
 
     it('returns an element representation of the given object without keys', function () {
     it('returns an element representation of the given object without keys', function () {
-      const res = arrayToList([{}]);
+      const res = arrayToString([{}]);
       expect(res).to.be.eq('Object');
       expect(res).to.be.eq('Object');
     });
     });
 
 
     it('returns an element representation of the given object without prototype', function () {
     it('returns an element representation of the given object without prototype', function () {
-      const res = arrayToList([Object.create(null)]);
+      const res = arrayToString([Object.create(null)]);
       expect(res).to.be.eq('Object');
       expect(res).to.be.eq('Object');
     });
     });
 
 
     it('returns an element representation of the given date instance', function () {
     it('returns an element representation of the given date instance', function () {
-      const res = arrayToList([new Date()]);
+      const res = arrayToString([new Date()]);
       expect(res).to.be.eq('Date');
       expect(res).to.be.eq('Date');
     });
     });
 
 
     it('returns an element representation of the given map instance', function () {
     it('returns an element representation of the given map instance', function () {
-      const res = arrayToList([new Map()]);
+      const res = arrayToString([new Map()]);
       expect(res).to.be.eq('Map');
       expect(res).to.be.eq('Map');
     });
     });
 
 
     it('returns an element representation of the given class instance', function () {
     it('returns an element representation of the given class instance', function () {
       class MyClass {}
       class MyClass {}
-      const res = arrayToList([new MyClass()]);
+      const res = arrayToString([new MyClass()]);
       expect(res).to.be.eq('MyClass (instance)');
       expect(res).to.be.eq('MyClass (instance)');
     });
     });
 
 
     it('returns an element representation of the given function', function () {
     it('returns an element representation of the given function', function () {
-      const res = arrayToList([function () {}]);
+      const res = arrayToString([function () {}]);
       expect(res).to.be.eq('Function');
       expect(res).to.be.eq('Function');
     });
     });
 
 
     it('returns an element representation of the given anonymous function', function () {
     it('returns an element representation of the given anonymous function', function () {
-      const res = arrayToList([() => undefined]);
+      const res = arrayToString([() => undefined]);
       expect(res).to.be.eq('Function');
       expect(res).to.be.eq('Function');
     });
     });
 
 
     it('returns an element representation of the given named function', function () {
     it('returns an element representation of the given named function', function () {
       function foo() {}
       function foo() {}
-      const res = arrayToList([foo]);
+      const res = arrayToString([foo]);
       expect(res).to.be.eq('Function');
       expect(res).to.be.eq('Function');
     });
     });
 
 
     it('returns an element representation of the given anonymous class', function () {
     it('returns an element representation of the given anonymous class', function () {
-      const res = arrayToList([class {}]);
+      const res = arrayToString([class {}]);
       expect(res).to.be.eq('Class');
       expect(res).to.be.eq('Class');
     });
     });
 
 
     it('returns an element representation of the given named class', function () {
     it('returns an element representation of the given named class', function () {
       class MyClass {}
       class MyClass {}
-      const res = arrayToList([MyClass]);
+      const res = arrayToString([MyClass]);
       expect(res).to.be.eq('MyClass');
       expect(res).to.be.eq('MyClass');
     });
     });
 
 
     it('returns an element representation of the given class constructor', function () {
     it('returns an element representation of the given class constructor', function () {
       class MyClass {}
       class MyClass {}
-      const res = arrayToList([MyClass.constructor]);
+      const res = arrayToString([MyClass.constructor]);
       expect(res).to.be.eq('Function');
       expect(res).to.be.eq('Function');
     });
     });
 
 
     it('returns an element representation of the given symbol', function () {
     it('returns an element representation of the given symbol', function () {
-      const res = arrayToList([Symbol()]);
+      const res = arrayToString([Symbol()]);
       expect(res).to.be.eq('Symbol');
       expect(res).to.be.eq('Symbol');
     });
     });
 
 
     it('returns an element representation of the given named symbol', function () {
     it('returns an element representation of the given named symbol', function () {
-      const res = arrayToList([Symbol('foo')]);
+      const res = arrayToString([Symbol('foo')]);
       expect(res).to.be.eq('Symbol');
       expect(res).to.be.eq('Symbol');
     });
     });
 
 
     it('returns an element representation of the given undefined', function () {
     it('returns an element representation of the given undefined', function () {
-      const res = arrayToList([undefined]);
+      const res = arrayToString([undefined]);
       expect(res).to.be.eq('undefined');
       expect(res).to.be.eq('undefined');
     });
     });
 
 
     it('returns an element representation of the given null', function () {
     it('returns an element representation of the given null', function () {
-      const res = arrayToList([null]);
+      const res = arrayToString([null]);
       expect(res).to.be.eq('null');
       expect(res).to.be.eq('null');
     });
     });
   });
   });

+ 2 - 2
src/errorf.spec.js

@@ -2,12 +2,12 @@ import {expect} from 'chai';
 import {Errorf} from './errorf.js';
 import {Errorf} from './errorf.js';
 
 
 describe('Errorf', function () {
 describe('Errorf', function () {
-  it('does not require a message argument', function () {
+  it('should extend Error class and should not require arguments', function () {
     const error = new Errorf();
     const error = new Errorf();
     expect(error).to.be.instanceof(Error);
     expect(error).to.be.instanceof(Error);
   });
   });
 
 
-  it('interpolates the given message', function () {
+  it('should interpolate the given message', function () {
     const error = new Errorf(
     const error = new Errorf(
       'It requires one of %l, but %v given.',
       'It requires one of %l, but %v given.',
       [true, false, 'y', 'n'],
       [true, false, 'y', 'n'],

+ 1 - 1
src/format.d.ts

@@ -8,7 +8,7 @@
  *
  *
  * extras:
  * extras:
  * v - value (valueToString.js)
  * v - value (valueToString.js)
- * l - list (arrayToList.js)
+ * l - list (arrayToString.js)
  *
  *
  * @param pattern
  * @param pattern
  * @param args
  * @param args

+ 3 - 3
src/format.js

@@ -1,4 +1,4 @@
-import {arrayToList} from './array-to-list.js';
+import {arrayToString} from './array-to-string.js';
 import {valueToString} from './value-to-string.js';
 import {valueToString} from './value-to-string.js';
 
 
 /**
 /**
@@ -11,7 +11,7 @@ import {valueToString} from './value-to-string.js';
  *
  *
  * extras:
  * extras:
  * v - value (valueToString.js)
  * v - value (valueToString.js)
- * l - list (arrayToList.js)
+ * l - list (arrayToString.js)
  *
  *
  * @param {string} pattern
  * @param {string} pattern
  * @return {string}
  * @return {string}
@@ -41,7 +41,7 @@ export function format(pattern) {
           arg = valueToString(arg);
           arg = valueToString(arg);
           break;
           break;
         case 'l':
         case 'l':
-          arg = arrayToList(arg);
+          arg = arrayToString(arg);
           break;
           break;
       }
       }
       if (!escaped) return arg;
       if (!escaped) return arg;

+ 2 - 1
src/index.d.ts

@@ -1,4 +1,5 @@
 export * from './format.js';
 export * from './format.js';
 export * from './errorf.js';
 export * from './errorf.js';
-export * from './array-to-list.js';
+export * from './array-to-string.js';
 export * from './value-to-string.js';
 export * from './value-to-string.js';
+export * from './invalid-argument-error.js';

+ 3 - 0
src/index.js

@@ -1,2 +1,5 @@
 export * from './format.js';
 export * from './format.js';
 export * from './errorf.js';
 export * from './errorf.js';
+export * from './array-to-string.js';
+export * from './value-to-string.js';
+export * from './invalid-argument-error.js';

+ 6 - 0
src/invalid-argument-error.d.ts

@@ -0,0 +1,6 @@
+import {Errorf} from './errorf.js';
+
+/**
+ * Invalid argument error.
+ */
+export declare class InvalidArgumentError extends Errorf {}

+ 6 - 0
src/invalid-argument-error.js

@@ -0,0 +1,6 @@
+import {Errorf} from './errorf.js';
+
+/**
+ * Invalid argument error.
+ */
+export class InvalidArgumentError extends Errorf {}

+ 21 - 0
src/invalid-argument-error.spec.js

@@ -0,0 +1,21 @@
+import {expect} from 'chai';
+import {Errorf} from './errorf.js';
+import {InvalidArgumentError} from './invalid-argument-error.js';
+
+describe('InvalidArgumentError', function () {
+  it('should extend Errorf class and should not require arguments', function () {
+    const error = new InvalidArgumentError();
+    expect(error).to.be.instanceof(Errorf);
+  });
+
+  it('should interpolate the given message', function () {
+    const error = new InvalidArgumentError(
+      'It requires one of %l, but %v given.',
+      [true, false, 'y', 'n'],
+      new Map(),
+    );
+    expect(error.message).to.be.eq(
+      'It requires one of true, false, "y", "n", but Map given.',
+    );
+  });
+});