Skip to content

Commit d664c15

Browse files
Add isClassInstance (#35)
1 parent 7947d57 commit d664c15

File tree

6 files changed

+58
-5
lines changed

6 files changed

+58
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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 |

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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
},

src/lib/is-class-instance/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const isClassInstance = (instance: any, className = "") =>
2+
instance?.constructor?.toString().startsWith(`class ${className}`) || false;

src/lib/unnest/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { getTypeof } from "../get-typeof";
2+
import { isClassInstance } from "../is-class-instance";
23

34
interface HandleArrayParams {
45
acc: Record<string, any>;
@@ -53,10 +54,7 @@ const handleArray = ({
5354

5455
const 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;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
});

0 commit comments

Comments
 (0)