Browse Source

feat: adds symbol description to output

e22m4u 2 days ago
parent
commit
150f654030
7 changed files with 71 additions and 35 deletions
  1. 25 9
      dist/cjs/index.cjs
  2. 4 4
      src/array-to-string.spec.js
  3. 1 1
      src/errorf.d.ts
  4. 3 3
      src/errorf.js
  5. 9 9
      src/format.spec.js
  6. 22 6
      src/value-to-string.js
  7. 7 3
      src/value-to-string.spec.js

+ 25 - 9
dist/cjs/index.cjs

@@ -50,14 +50,30 @@ var BASE_CTOR_NAMES = [
   "Date"
 ];
 function valueToString(input) {
-  if (input == null) return String(input);
-  if (typeof input === "string") return `"${input}"`;
-  if (typeof input === "number" || typeof input === "boolean")
+  if (input == null) {
     return String(input);
-  if (isClass(input)) return input.name ? input.name : "Class";
-  if (input.constructor && input.constructor.name)
+  }
+  if (typeof input === "string") {
+    return `"${input}"`;
+  }
+  if (typeof input === "number" || typeof input === "boolean") {
+    return String(input);
+  }
+  if (typeof input === "symbol") {
+    if (input.description != null) {
+      return `Symbol("${input.description}")`;
+    }
+    return "Symbol";
+  }
+  if (isClass(input)) {
+    return input.name ? input.name : "Class";
+  }
+  if (input.constructor && input.constructor.name) {
     return BASE_CTOR_NAMES.includes(input.constructor.name) ? input.constructor.name : `${input.constructor.name} (instance)`;
-  if (typeof input === "object" && input.constructor == null) return "Object";
+  }
+  if (typeof input === "object" && input.constructor == null) {
+    return "Object";
+  }
   return String(input);
 }
 __name(valueToString, "valueToString");
@@ -116,10 +132,10 @@ var _Errorf = class _Errorf extends Error {
   /**
    * Constructor.
    *
-   * @param {string|undefined} pattern
-   * @param {*} args
+   * @param {string} [pattern]
+   * @param {...*} args
    */
-  constructor(pattern = void 0, ...args) {
+  constructor(pattern, ...args) {
     const message = pattern != null ? format(pattern, ...args) : void 0;
     super(message);
   }

+ 4 - 4
src/array-to-string.spec.js

@@ -113,9 +113,9 @@ describe('arrayToString', function () {
       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 symbol with a description', function () {
       const res = arrayToString(Symbol('foo'));
-      expect(res).to.be.eq('Symbol');
+      expect(res).to.be.eq('Symbol("foo")');
     });
 
     it('returns a string representation of the given undefined', function () {
@@ -249,9 +249,9 @@ describe('arrayToString', function () {
       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 symbol with a description', function () {
       const res = arrayToString([Symbol('foo')]);
-      expect(res).to.be.eq('Symbol');
+      expect(res).to.be.eq('Symbol("foo")');
     });
 
     it('returns an element representation of the given undefined', function () {

+ 1 - 1
src/errorf.d.ts

@@ -8,5 +8,5 @@ export declare class Errorf extends Error {
    * @param pattern
    * @param args
    */
-  constructor(pattern: string, ...args: any[]);
+  constructor(pattern?: string, ...args: any[]);
 }

+ 3 - 3
src/errorf.js

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

+ 9 - 9
src/format.spec.js

@@ -119,7 +119,7 @@ describe('format', function () {
       expect(res).to.be.eq('Symbol()');
     });
 
-    it('converts the given pattern of a named symbol to a string', function () {
+    it('converts the given pattern of a symbol with a description to a string', function () {
       const res = format('%s', Symbol('foo'));
       expect(res).to.be.eq('Symbol(foo)');
     });
@@ -266,7 +266,7 @@ describe('format', function () {
       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 symbol with a description', function () {
       const res = format('%s', Symbol('foo'));
       expect(res).to.be.eq('Symbol(foo)');
     });
@@ -520,7 +520,7 @@ describe('format', function () {
       expect(res).to.be.eq('undefined');
     });
 
-    it('returns a string representation of the given named symbol', function () {
+    it('returns a string representation of the given symbol with a description', function () {
       const res = format('%j', Symbol('foo'));
       expect(res).to.be.eq('undefined');
     });
@@ -656,9 +656,9 @@ describe('format', function () {
       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 symbol with a description', function () {
       const res = format('%v', Symbol('foo'));
-      expect(res).to.be.eq('Symbol');
+      expect(res).to.be.eq('Symbol("foo")');
     });
 
     it('returns a string representation of the given undefined', function () {
@@ -783,9 +783,9 @@ describe('format', function () {
         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 symbol with a description', function () {
         const res = format('%l', Symbol('foo'));
-        expect(res).to.be.eq('Symbol');
+        expect(res).to.be.eq('Symbol("foo")');
       });
 
       it('returns a string representation of the given undefined', function () {
@@ -919,9 +919,9 @@ describe('format', function () {
         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 symbol with a description', function () {
         const res = format('%l', [Symbol('foo')]);
-        expect(res).to.be.eq('Symbol');
+        expect(res).to.be.eq('Symbol("foo")');
       });
 
       it('returns an element representation of the given undefined', function () {

+ 22 - 6
src/value-to-string.js

@@ -20,15 +20,31 @@ const BASE_CTOR_NAMES = [
  * @returns {string}
  */
 export function valueToString(input) {
-  if (input == null) return String(input);
-  if (typeof input === 'string') return `"${input}"`;
-  if (typeof input === 'number' || typeof input === 'boolean')
+  if (input == null) {
     return String(input);
-  if (isClass(input)) return input.name ? input.name : 'Class';
-  if (input.constructor && input.constructor.name)
+  }
+  if (typeof input === 'string') {
+    return `"${input}"`;
+  }
+  if (typeof input === 'number' || typeof input === 'boolean') {
+    return String(input);
+  }
+  if (typeof input === 'symbol') {
+    if (input.description != null) {
+      return `Symbol("${input.description}")`;
+    }
+    return 'Symbol';
+  }
+  if (isClass(input)) {
+    return input.name ? input.name : 'Class';
+  }
+  if (input.constructor && input.constructor.name) {
     return BASE_CTOR_NAMES.includes(input.constructor.name)
       ? input.constructor.name
       : `${input.constructor.name} (instance)`;
-  if (typeof input === 'object' && input.constructor == null) return 'Object';
+  }
+  if (typeof input === 'object' && input.constructor == null) {
+    return 'Object';
+  }
   return String(input);
 }

+ 7 - 3
src/value-to-string.spec.js

@@ -122,9 +122,13 @@ describe('valueToString', function () {
     expect(res).to.be.eq('Symbol');
   });
 
-  it('returns a string representation of the given named symbol', function () {
-    const res = valueToString(Symbol('foo'));
-    expect(res).to.be.eq('Symbol');
+  it('returns a string representation of the given symbol with a description', function () {
+    expect(valueToString(Symbol('foo'))).to.be.eq('Symbol("foo")');
+    expect(valueToString(Symbol(''))).to.be.eq('Symbol("")');
+    expect(valueToString(Symbol(10))).to.be.eq('Symbol("10")');
+    expect(valueToString(Symbol(0))).to.be.eq('Symbol("0")');
+    expect(valueToString(Symbol(true))).to.be.eq('Symbol("true")');
+    expect(valueToString(Symbol(false))).to.be.eq('Symbol("false")');
   });
 
   it('returns a string representation of the given undefined', function () {