Skip to content

Commit 25c1420

Browse files
authored
Merge pull request #6 from typescript-package/develop
v2.0.0
2 parents 0ed8023 + ccc41fe commit 25c1420

File tree

5 files changed

+39
-7
lines changed

5 files changed

+39
-7
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@typescript-package/core",
3-
"version": "1.2.0",
3+
"version": "2.0.0",
44
"author": "wwwdev.io <[email protected]>",
55
"description": "A TypeScript library with features used across other `typescript-package` libraries.",
66
"license": "MIT",

src/lib/type-of.func.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
* @returns {string} The return value is a `string` of the object class name.
55
* @author https://javascript.plainenglish.io/the-best-way-to-type-check-in-vanilla-js-55197b4f45ec
66
*/
7-
export const typeOf = (value: any): string =>
8-
Object.prototype.toString.call(value).slice(8, -1).toLowerCase();
7+
export const typeOf = (value: any): string =>
8+
Object.prototype.toString.call(value).slice(8, -1);

src/lib/value.class.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,31 @@
33
* @export
44
* @abstract
55
* @class Value
6-
* @template Type
6+
* @template Type The type of the privately stored `#value`.
77
*/
8+
89
export abstract class Value<Type> {
10+
/**
11+
* @description Returns the `string` tag representation of the `Value` class when used in `Object.prototype.toString.call(instance)`.
12+
* The `tag` getter returns the actual class name defined with the `Symbol.toStringTag()` of the child class.
13+
* @public
14+
* @readonly
15+
* @type {string}
16+
*/
17+
public get [Symbol.toStringTag]() {
18+
return 'Value';
19+
}
20+
21+
/**
22+
* @description Returns the string tag of the current instance defined by the `Symbol.toStringTag`.
23+
* @public
24+
* @returns {string | undefined} The extracted class name, such as `'Value'`, or `undefined` if extraction fails.
25+
*/
26+
public get tag(): string | undefined {
27+
const tag = Object.prototype.toString.call(this).slice(8, -1);
28+
return tag !== 'Object' ? tag : undefined;
29+
}
30+
931
/**
1032
* @description Returns the privately stored value of generic type variable `Type`.
1133
* @public
@@ -16,7 +38,6 @@ export abstract class Value<Type> {
1638
return this.#value;
1739
}
1840

19-
2041
/**
2142
* @description Privately stored value of generic type variable `Type`.
2243
* @type {Type}

src/test/value.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Value } from "../lib";
2+
3+
export class TestValue<Type> extends Value<Type> {
4+
public override get [Symbol.toStringTag]() {
5+
return 'TestValue';
6+
}
7+
}
8+
9+
const value = new TestValue('test');
10+
11+
console.log(`tag, `, value.tag);

0 commit comments

Comments
 (0)