Browse Source

chore: uses the class name if ES6 class given

e22m4u 1 year ago
parent
commit
e962aae3fb

+ 1 - 1
README.md

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

+ 16 - 6
src/array-to-list.spec.js

@@ -79,7 +79,7 @@ describe('arrayToList', function () {
       expect(res).to.be.eq('Function');
     });
 
-    it('returns a string representation of the given shorthand function', function () {
+    it('returns a string representation of the given anonymous function', function () {
       const res = arrayToList(() => undefined);
       expect(res).to.be.eq('Function');
     });
@@ -90,10 +90,15 @@ describe('arrayToList', function () {
       expect(res).to.be.eq('Function');
     });
 
-    it('returns a string representation of the given class', function () {
+    it('returns a string representation of the given anonymous class', function () {
+      const res = arrayToList(class {});
+      expect(res).to.be.eq('Class');
+    });
+
+    it('returns a string representation of the given named class', function () {
       class MyClass {}
       const res = arrayToList(MyClass);
-      expect(res).to.be.eq('Function');
+      expect(res).to.be.eq('MyClass');
     });
 
     it('returns a string representation of the given class constructor', function () {
@@ -210,7 +215,7 @@ describe('arrayToList', function () {
       expect(res).to.be.eq('Function');
     });
 
-    it('returns an element representation of the given shorthand function', function () {
+    it('returns an element representation of the given anonymous function', function () {
       const res = arrayToList([() => undefined]);
       expect(res).to.be.eq('Function');
     });
@@ -221,10 +226,15 @@ describe('arrayToList', function () {
       expect(res).to.be.eq('Function');
     });
 
-    it('returns an element representation of the given class', function () {
+    it('returns an element representation of the given anonymous class', function () {
+      const res = arrayToList([class {}]);
+      expect(res).to.be.eq('Class');
+    });
+
+    it('returns an element representation of the given named class', function () {
       class MyClass {}
       const res = arrayToList([MyClass]);
-      expect(res).to.be.eq('Function');
+      expect(res).to.be.eq('MyClass');
     });
 
     it('returns an element representation of the given class constructor', function () {

+ 52 - 17
src/format.spec.js

@@ -85,7 +85,7 @@ describe('format', function () {
       expect(res).to.be.eq('function () {}');
     });
 
-    it('converts the given pattern of a shorthand function to a string', function () {
+    it('converts the given pattern of an anonymous function to a string', function () {
       const res = format(() => undefined);
       expect(res).to.be.eq('() => undefined');
     });
@@ -96,7 +96,12 @@ describe('format', function () {
       expect(res).to.be.eq('function foo() {}');
     });
 
-    it('converts the given pattern of a class to a string', function () {
+    it('converts the given pattern of an anonymous class to a string', function () {
+      const res = format(class {});
+      expect(res).to.be.eq('class {}');
+    });
+
+    it('converts the given pattern of a named class to a string', function () {
       class MyClass {}
       const res = format(MyClass);
       expect(res).to.be.eq('class MyClass {}');
@@ -227,7 +232,7 @@ describe('format', function () {
       expect(res).to.be.eq('function () {}');
     });
 
-    it('returns a string representation of the given shorthand function', function () {
+    it('returns a string representation of the given anonymous function', function () {
       const res = format('%s', () => undefined);
       expect(res).to.be.eq('() => undefined');
     });
@@ -238,7 +243,12 @@ describe('format', function () {
       expect(res).to.be.eq('function foo() {}');
     });
 
-    it('returns a string representation of the given class', function () {
+    it('returns a string representation of the given anonymous class', function () {
+      const res = format('%s', class {});
+      expect(res).to.be.eq('class {}');
+    });
+
+    it('returns a string representation of the given named class', function () {
       class MyClass {}
       const res = format('%s', MyClass);
       expect(res).to.be.eq('class MyClass {}');
@@ -354,7 +364,7 @@ describe('format', function () {
       expect(res).to.be.eq('NaN');
     });
 
-    it('returns a string representation of the given shorthand function', function () {
+    it('returns a string representation of the given anonymous function', function () {
       const res = format('%d', () => undefined);
       expect(res).to.be.eq('NaN');
     });
@@ -365,7 +375,12 @@ describe('format', function () {
       expect(res).to.be.eq('NaN');
     });
 
-    it('returns a string representation of the given class', function () {
+    it('returns a string representation of the given anonymous class', function () {
+      const res = format('%d', class {});
+      expect(res).to.be.eq('NaN');
+    });
+
+    it('returns a string representation of the given named class', function () {
       class MyClass {}
       const res = format('%d', MyClass);
       expect(res).to.be.eq('NaN');
@@ -471,7 +486,7 @@ describe('format', function () {
       expect(res).to.be.eq('undefined');
     });
 
-    it('returns a string representation of the given shorthand function', function () {
+    it('returns a string representation of the given anonymous function', function () {
       const res = format('%j', () => undefined);
       expect(res).to.be.eq('undefined');
     });
@@ -482,7 +497,12 @@ describe('format', function () {
       expect(res).to.be.eq('undefined');
     });
 
-    it('returns a string representation of the given class', function () {
+    it('returns a string representation of the given anonymous class', function () {
+      const res = format('%j', class {});
+      expect(res).to.be.eq('undefined');
+    });
+
+    it('returns a string representation of the given named class', function () {
       class MyClass {}
       const res = format('%j', MyClass);
       expect(res).to.be.eq('undefined');
@@ -602,7 +622,7 @@ describe('format', function () {
       expect(res).to.be.eq('Function');
     });
 
-    it('returns a string representation of the given shorthand function', function () {
+    it('returns a string representation of the given anonymous function', function () {
       const res = format('%v', () => undefined);
       expect(res).to.be.eq('Function');
     });
@@ -613,10 +633,15 @@ describe('format', function () {
       expect(res).to.be.eq('Function');
     });
 
-    it('returns a string representation of the given class', function () {
+    it('returns a string representation of the given anonymous class', function () {
+      const res = format('%v', class {});
+      expect(res).to.be.eq('Class');
+    });
+
+    it('returns a string representation of the given named class', function () {
       class MyClass {}
       const res = format('%v', MyClass);
-      expect(res).to.be.eq('Function');
+      expect(res).to.be.eq('MyClass');
     });
 
     it('returns a string representation of the given class constructor', function () {
@@ -724,7 +749,7 @@ describe('format', function () {
         expect(res).to.be.eq('Function');
       });
 
-      it('returns a string representation of the given shorthand function', function () {
+      it('returns a string representation of the given anonymous function', function () {
         const res = format('%l', () => undefined);
         expect(res).to.be.eq('Function');
       });
@@ -735,10 +760,15 @@ describe('format', function () {
         expect(res).to.be.eq('Function');
       });
 
-      it('returns a string representation of the given class', function () {
+      it('returns a string representation of the given anonymous class', function () {
+        const res = format('%l', class {});
+        expect(res).to.be.eq('Class');
+      });
+
+      it('returns a string representation of the given named class', function () {
         class MyClass {}
         const res = format('%l', MyClass);
-        expect(res).to.be.eq('Function');
+        expect(res).to.be.eq('MyClass');
       });
 
       it('returns a string representation of the given class constructor', function () {
@@ -855,7 +885,7 @@ describe('format', function () {
         expect(res).to.be.eq('Function');
       });
 
-      it('returns an element representation of the given shorthand function', function () {
+      it('returns an element representation of the given anonymous function', function () {
         const res = format('%l', [() => undefined]);
         expect(res).to.be.eq('Function');
       });
@@ -866,10 +896,15 @@ describe('format', function () {
         expect(res).to.be.eq('Function');
       });
 
-      it('returns an element representation of the given class', function () {
+      it('returns an element representation of the given anonymous class', function () {
+        const res = format('%l', [class {}]);
+        expect(res).to.be.eq('Class');
+      });
+
+      it('returns an element representation of the given named class', function () {
         class MyClass {}
         const res = format('%l', [MyClass]);
-        expect(res).to.be.eq('Function');
+        expect(res).to.be.eq('MyClass');
       });
 
       it('returns an element representation of the given class constructor', function () {

+ 1 - 0
src/utils/index.d.ts

@@ -0,0 +1 @@
+export * from './is-class.js';

+ 1 - 0
src/utils/index.js

@@ -0,0 +1 @@
+export * from './is-class.js';

+ 7 - 0
src/utils/is-class.d.ts

@@ -0,0 +1,7 @@
+/**
+ * Returns true if the given value is ES6 class.
+ *
+ * @param {*} value
+ * @returns {boolean}
+ */
+export declare function isClass(value: unknown): boolean;

+ 13 - 0
src/utils/is-class.js

@@ -0,0 +1,13 @@
+/**
+ * Returns true if the given value is ES6 class.
+ *
+ * @param {*} value
+ * @returns {boolean}
+ */
+export function isClass(value) {
+  if (!value) return false;
+  return (
+    typeof value === 'function' &&
+    /^class\s/.test(Function.prototype.toString.call(value))
+  );
+}

+ 25 - 0
src/utils/is-class.spec.js

@@ -0,0 +1,25 @@
+import {expect} from 'chai';
+import {isClass} from './is-class.js';
+
+describe('isClass', function () {
+  it('returns true if a given value is a class', function () {
+    class MyClass {}
+    expect(isClass(MyClass)).to.be.true;
+  });
+
+  it('returns false if a given value is not a class', function () {
+    expect(isClass(Date)).to.be.false;
+    expect(isClass(Number)).to.be.false;
+    expect(isClass(String)).to.be.false;
+    expect(isClass(function myFunction() {})).to.be.false;
+    expect(isClass(function () {})).to.be.false;
+    expect(isClass(() => undefined)).to.be.false;
+    expect(isClass('string')).to.be.false;
+    expect(isClass(10)).to.be.false;
+    expect(isClass(true)).to.be.false;
+    expect(isClass({})).to.be.false;
+    expect(isClass([])).to.be.false;
+    expect(isClass(undefined)).to.be.false;
+    expect(isClass(null)).to.be.false;
+  });
+});

+ 3 - 0
src/value-to-string.js

@@ -1,3 +1,5 @@
+import {isClass} from './utils/index.js';
+
 /**
  * Value to string.
  *
@@ -9,6 +11,7 @@ export function valueToString(input) {
   if (typeof input === 'string') return `"${input}"`;
   if (typeof input === 'number' || typeof input === 'boolean')
     return String(input);
+  if (isClass(input)) return input.name ? input.name : 'Class';
   if (input.constructor && input.constructor.name)
     return input.constructor.name;
   if (typeof input === 'object' && input.constructor == null) return 'Object';

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

@@ -88,7 +88,7 @@ describe('valueToString', function () {
     expect(res).to.be.eq('Function');
   });
 
-  it('returns a string representation of the given shorthand function', function () {
+  it('returns a string representation of the given anonymous function', function () {
     const res = valueToString(() => undefined);
     expect(res).to.be.eq('Function');
   });
@@ -99,10 +99,15 @@ describe('valueToString', function () {
     expect(res).to.be.eq('Function');
   });
 
-  it('returns a string representation of the given class', function () {
+  it('returns a string representation of the given anonymous class', function () {
+    const res = valueToString(class {});
+    expect(res).to.be.eq('Class');
+  });
+
+  it('returns a string representation of the given named class', function () {
     class MyClass {}
     const res = valueToString(MyClass);
-    expect(res).to.be.eq('Function');
+    expect(res).to.be.eq('MyClass');
   });
 
   it('returns a string representation of the given class constructor', function () {