Просмотр исходного кода

chore: improves JSDoc annotations

e22m4u 2 лет назад
Родитель
Сommit
fb55727b4e
3 измененных файлов с 26 добавлено и 22 удалено
  1. 4 3
      src/errorf.js
  2. 12 9
      src/errorf.spec.js
  3. 10 10
      src/format.js

+ 4 - 3
src/errorf.js

@@ -7,10 +7,11 @@ export class Errorf extends Error {
   /**
   /**
    * Constructor.
    * Constructor.
    *
    *
-   * @param {string} pattern
+   * @param {string|undefined} pattern
    * @param {any} args
    * @param {any} args
    */
    */
-  constructor(pattern, ...args) {
-    super(format(pattern, ...args));
+  constructor(pattern = undefined, ...args) {
+    const message = pattern != null ? format(pattern, ...args) : undefined;
+    super(message);
   }
   }
 }
 }

+ 12 - 9
src/errorf.spec.js

@@ -2,15 +2,18 @@ import {expect} from 'chai';
 import {Errorf} from './errorf.js';
 import {Errorf} from './errorf.js';
 
 
 describe('Errorf', function () {
 describe('Errorf', function () {
-  it('interpolates the given arguments', function () {
-    const throwable = () => {
-      throw new Errorf(
-        'It requires one of %l, but %v given.',
-        [true, false, 'y', 'n'],
-        new Map(),
-      );
-    };
-    expect(throwable).to.throw(
+  it('does not require a message argument', function () {
+    const error = new Errorf();
+    expect(error).to.be.instanceof(Error);
+  });
+
+  it('interpolates the given message', function () {
+    const error = new Errorf(
+      '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.',
       'It requires one of true, false, "y", "n", but Map given.',
     );
     );
   });
   });

+ 10 - 10
src/format.js

@@ -13,19 +13,19 @@ import {valueToString} from './value-to-string.js';
  * v - value (valueToString.js)
  * v - value (valueToString.js)
  * l - list (arrayToList.js)
  * l - list (arrayToList.js)
  *
  *
- * @param {any} fmt
+ * @param {string} pattern
  * @return {string}
  * @return {string}
  */
  */
-export function format(fmt) {
-  if (fmt instanceof Date) {
-    fmt = fmt.toISOString();
-  } else if (typeof fmt !== 'string') {
-    fmt = String(fmt);
+export function format(pattern) {
+  if (pattern instanceof Date) {
+    pattern = pattern.toISOString();
+  } else if (typeof pattern !== 'string') {
+    pattern = String(pattern);
   }
   }
   const re = /(%?)(%([sdjvl]))/g;
   const re = /(%?)(%([sdjvl]))/g;
   const args = Array.prototype.slice.call(arguments, 1);
   const args = Array.prototype.slice.call(arguments, 1);
   if (args.length) {
   if (args.length) {
-    fmt = fmt.replace(re, function (match, escaped, ptn, flag) {
+    pattern = pattern.replace(re, function (match, escaped, ptn, flag) {
       let arg = args.shift();
       let arg = args.shift();
       switch (flag) {
       switch (flag) {
         // eslint-disable-next-line no-fallthrough
         // eslint-disable-next-line no-fallthrough
@@ -51,8 +51,8 @@ export function format(fmt) {
     });
     });
   }
   }
   // arguments remain after formatting
   // arguments remain after formatting
-  if (args.length) fmt += ' ' + args.join(' ');
+  if (args.length) pattern += ' ' + args.join(' ');
   // update escaped %% values
   // update escaped %% values
-  fmt = fmt.replace(/%{2,2}/g, '%');
-  return '' + fmt;
+  pattern = pattern.replace(/%{2,2}/g, '%');
+  return '' + pattern;
 }
 }