Browse Source

fix: instance postfix

e22m4u 1 year ago
parent
commit
244a67f14a
5 changed files with 23 additions and 8 deletions
  1. 1 1
      README.md
  2. 2 2
      src/array-to-list.spec.js
  3. 3 3
      src/format.spec.js
  4. 16 1
      src/value-to-string.js
  5. 1 1
      src/value-to-string.spec.js

+ 1 - 1
README.md

@@ -56,7 +56,7 @@ class MyClass {}
 
 format('> %v', 'MyClass');     // > "MyClass"
 format('> %v', MyClass);       // > MyClass
-format('> %v', new MyClass()); // > MyClass (экземпляр)
+format('> %v', new MyClass()); // > MyClass (instance)
 ```
 
 ### Спецификатор `%l`

+ 2 - 2
src/array-to-list.spec.js

@@ -71,7 +71,7 @@ describe('arrayToList', function () {
     it('returns a string representation of the given class instance', function () {
       class MyClass {}
       const res = arrayToList(new MyClass());
-      expect(res).to.be.eq('MyClass');
+      expect(res).to.be.eq('MyClass (instance)');
     });
 
     it('returns a string representation of the given function', function () {
@@ -207,7 +207,7 @@ describe('arrayToList', function () {
     it('returns an element representation of the given class instance', function () {
       class MyClass {}
       const res = arrayToList([new MyClass()]);
-      expect(res).to.be.eq('MyClass');
+      expect(res).to.be.eq('MyClass (instance)');
     });
 
     it('returns an element representation of the given function', function () {

+ 3 - 3
src/format.spec.js

@@ -614,7 +614,7 @@ describe('format', function () {
     it('returns a string representation of the given class instance', function () {
       class MyClass {}
       const res = format('%v', new MyClass());
-      expect(res).to.be.eq('MyClass');
+      expect(res).to.be.eq('MyClass (instance)');
     });
 
     it('returns a string representation of the given function', function () {
@@ -741,7 +741,7 @@ describe('format', function () {
       it('returns a string representation of the given class instance', function () {
         class MyClass {}
         const res = format('%l', new MyClass());
-        expect(res).to.be.eq('MyClass');
+        expect(res).to.be.eq('MyClass (instance)');
       });
 
       it('returns a string representation of the given function', function () {
@@ -877,7 +877,7 @@ describe('format', function () {
       it('returns an element representation of the given class instance', function () {
         class MyClass {}
         const res = format('%l', [new MyClass()]);
-        expect(res).to.be.eq('MyClass');
+        expect(res).to.be.eq('MyClass (instance)');
       });
 
       it('returns an element representation of the given function', function () {

+ 16 - 1
src/value-to-string.js

@@ -1,5 +1,18 @@
 import {isClass} from './utils/index.js';
 
+const BASE_CTOR_NAMES = [
+  'String',
+  'Number',
+  'Boolean',
+  'Object',
+  'Array',
+  'Function',
+  'Symbol',
+  'Map',
+  'Set',
+  'Date',
+];
+
 /**
  * Value to string.
  *
@@ -13,7 +26,9 @@ export function valueToString(input) {
     return String(input);
   if (isClass(input)) return input.name ? input.name : 'Class';
   if (input.constructor && input.constructor.name)
-    return 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';
   return String(input);
 }

+ 1 - 1
src/value-to-string.spec.js

@@ -80,7 +80,7 @@ describe('valueToString', function () {
   it('returns a string representation of the given class instance', function () {
     class MyClass {}
     const res = valueToString(new MyClass());
-    expect(res).to.be.eq('MyClass');
+    expect(res).to.be.eq('MyClass (instance)');
   });
 
   it('returns a string representation of the given function', function () {