File tree Expand file tree Collapse file tree 6 files changed +58
-5
lines changed
Expand file tree Collapse file tree 6 files changed +58
-5
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010### Added
1111
1212- ` nest `
13+ - ` isClassInstance `
1314
1415### Changed
1516
Original file line number Diff line number Diff line change @@ -81,6 +81,7 @@ npm i @techmmunity/utils
8181| ` isAlphanumeric ` | Return true if value is a number or a letter |
8282| ` isBetween ` | Return true if value is a number between 2 values |
8383| ` isBrazilianPhone ` | Return true if value is a brazilian phone |
84+ | ` isClassInstance ` | Return true if value is a class instance |
8485| ` isCnpj ` | Return true if value is a CNPJ |
8586| ` isCpf ` | Return true if value is a CPF |
8687| ` isDarkHexColor ` | Return true if value is a dark hex color |
Original file line number Diff line number Diff line change @@ -21,7 +21,7 @@ module.exports = {
2121 coverageThreshold : {
2222 global : {
2323 statements : 99.1 ,
24- branches : 96.4 ,
24+ branches : 97.5 ,
2525 functions : 100 ,
2626 lines : 99.68 ,
2727 } ,
Original file line number Diff line number Diff line change 1+ export const isClassInstance = ( instance : any , className = "" ) =>
2+ instance ?. constructor ?. toString ( ) . startsWith ( `class ${ className } ` ) || false ;
Original file line number Diff line number Diff line change 11import { getTypeof } from "../get-typeof" ;
2+ import { isClassInstance } from "../is-class-instance" ;
23
34interface HandleArrayParams {
45 acc : Record < string , any > ;
@@ -53,10 +54,7 @@ const handleArray = ({
5354
5455const handleObject = ( { acc, key, value } : HandleObjectParams ) => {
5556 // Classes or classes instances should be the same
56- if (
57- getTypeof ( value ) === "class" ||
58- value . constructor ?. toString ( ) . startsWith ( "class" )
59- ) {
57+ if ( getTypeof ( value ) === "class" || isClassInstance ( value ) ) {
6058 acc [ key ] = value ;
6159
6260 return ;
Original file line number Diff line number Diff line change 1+ import { isClassInstance } from "../lib/is-class-instance" ;
2+
3+ /**
4+ *
5+ * True
6+ *
7+ */
8+
9+ describe ( "isClassInstance (return True)" , ( ) => {
10+ class Foo {
11+ public bar = "bar" ;
12+ }
13+
14+ const foo = new Foo ( ) ;
15+
16+ it ( "with valid instance" , ( ) => {
17+ expect ( isClassInstance ( foo ) ) . toBe ( true ) ;
18+ } ) ;
19+ } ) ;
20+
21+ /**
22+ *
23+ * False
24+ *
25+ */
26+
27+ describe ( "isClassInstance (return False)" , ( ) => {
28+ it ( "with string" , ( ) => {
29+ expect ( isClassInstance ( "foo" ) ) . toBe ( false ) ;
30+ } ) ;
31+
32+ it ( "with array" , ( ) => {
33+ expect ( isClassInstance ( [ ] ) ) . toBe ( false ) ;
34+ } ) ;
35+
36+ it ( "with object" , ( ) => {
37+ expect ( isClassInstance ( { } ) ) . toBe ( false ) ;
38+ } ) ;
39+
40+ it ( "with undefined" , ( ) => {
41+ expect ( isClassInstance ( undefined ) ) . toBe ( false ) ;
42+ } ) ;
43+
44+ it ( "with Symbol" , ( ) => {
45+ expect ( isClassInstance ( Symbol ( "foo" ) ) ) . toBe ( false ) ;
46+ } ) ;
47+
48+ it ( "with Map" , ( ) => {
49+ expect ( isClassInstance ( new Map ( ) ) ) . toBe ( false ) ;
50+ } ) ;
51+ } ) ;
You can’t perform that action at this time.
0 commit comments