Skip to content

Commit 550ae64

Browse files
committed
refactor(core): type-guards utils moved from sanitizer
1 parent 72c1b3f commit 550ae64

File tree

3 files changed

+62
-64
lines changed

3 files changed

+62
-64
lines changed

packages/core/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ export { HawkUserManager } from './users/hawk-user-manager';
44
export type { Logger, LogType } from './logger/logger';
55
export { isLoggerSet, setLogger, resetLogger, log } from './logger/logger';
66
export { validateUser, validateContext, isValidEventPayload, isValidBreadcrumb } from './utils/validation';
7-
export { isPlainObject } from './utils/type-guards';
7+
export { isPlainObject, isArray, isClassPrototype, isClassInstance, isString } from './utils/type-guards';
8+
export { Sanitizer } from './modules/sanitizer';

packages/core/src/modules/sanitizer.ts

Lines changed: 6 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
2-
import { isPlainObject } from '../utils/validation';
2+
import { isArray, isClassInstance, isClassPrototype, isPlainObject, isString } from '../utils/type-guards';
33

44
/**
55
* Custom type handler for Sanitizer.
@@ -55,15 +55,6 @@ export class Sanitizer {
5555
*/
5656
private static readonly customHandlers: SanitizerTypeHandler[] = [];
5757

58-
/**
59-
* Check if passed variable is an object
60-
*
61-
* @param target - variable to check
62-
*/
63-
public static isObject(target: any): boolean {
64-
return isPlainObject(target);
65-
}
66-
6758
/**
6859
* Register a custom type handler.
6960
* Handlers are checked before built-in type checks, in reverse registration order
@@ -96,7 +87,7 @@ export class Sanitizer {
9687
/**
9788
* If value is an Array, apply sanitizing for each element
9889
*/
99-
if (Sanitizer.isArray(data)) {
90+
if (isArray(data)) {
10091
return this.sanitizeArray(data, depth + 1, seen);
10192
}
10293

@@ -112,26 +103,26 @@ export class Sanitizer {
112103
* If values is a not-constructed class, it will be formatted as "<class SomeClass>"
113104
* class Editor {...} -> <class Editor>
114105
*/
115-
if (Sanitizer.isClassPrototype(data)) {
106+
if (isClassPrototype(data)) {
116107
return Sanitizer.formatClassPrototype(data);
117108

118109
/**
119110
* If values is a some class instance, it will be formatted as "<instance of SomeClass>"
120111
* new Editor() -> <instance of Editor>
121112
*/
122-
} else if (Sanitizer.isClassInstance(data)) {
113+
} else if (isClassInstance(data)) {
123114
return Sanitizer.formatClassInstance(data);
124115

125116
/**
126117
* If values is an object, do recursive call
127118
*/
128-
} else if (Sanitizer.isObject(data)) {
119+
} else if (isPlainObject(data)) {
129120
return Sanitizer.sanitizeObject(data, depth + 1, seen);
130121

131122
/**
132123
* If values is a string, trim it for max-length
133124
*/
134-
} else if (Sanitizer.isString(data)) {
125+
} else if (isString(data)) {
135126
return Sanitizer.trimString(data);
136127
}
137128

@@ -199,54 +190,6 @@ export class Sanitizer {
199190
return result;
200191
}
201192

202-
/**
203-
* Check if passed variable is an array
204-
*
205-
* @param target - variable to check
206-
*/
207-
private static isArray(target: any): boolean {
208-
return Array.isArray(target);
209-
}
210-
211-
/**
212-
* Check if passed variable is a not-constructed class
213-
*
214-
* @param target - variable to check
215-
*/
216-
private static isClassPrototype(target: any): boolean {
217-
if (!target || !target.constructor) {
218-
return false;
219-
}
220-
221-
/**
222-
* like
223-
* "function Function {
224-
* [native code]
225-
* }"
226-
*/
227-
const constructorStr = target.constructor.toString();
228-
229-
return constructorStr.includes('[native code]') && constructorStr.includes('Function');
230-
}
231-
232-
/**
233-
* Check if passed variable is a constructed class instance
234-
*
235-
* @param target - variable to check
236-
*/
237-
private static isClassInstance(target: any): boolean {
238-
return target && target.constructor && (/^class \S+ {/).test(target.constructor.toString());
239-
}
240-
241-
/**
242-
* Check if passed variable is a string
243-
*
244-
* @param target - variable to check
245-
*/
246-
private static isString(target: any): boolean {
247-
return typeof target === 'string';
248-
}
249-
250193
/**
251194
* Return name of a passed class
252195
*

packages/core/src/utils/type-guards.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
2+
13
/**
24
* Checks if value is a plain object (not null, array, Date, Map, etc.)
35
*
@@ -12,3 +14,55 @@ export function isPlainObject(value: unknown): value is Record<string, unknown>
1214

1315
return proto === Object.prototype || proto === null;
1416
}
17+
18+
/**
19+
* Check if passed variable is an array
20+
*
21+
* @param value - variable to check
22+
* @returns `true` if value is an array, otherwise `false`
23+
*/
24+
export function isArray(value: any): value is any[] {
25+
return Array.isArray(value);
26+
}
27+
28+
/**
29+
* Check if passed variable is a not-constructed class
30+
*
31+
* @param value - variable to check
32+
* @returns `true` if value is a class prototype, otherwise `false`
33+
*/
34+
export function isClassPrototype(value: any): boolean {
35+
if (!value || !value.constructor) {
36+
return false;
37+
}
38+
39+
/**
40+
* like
41+
* "function Function {
42+
* [native code]
43+
* }"
44+
*/
45+
const constructorStr = value.constructor.toString();
46+
47+
return constructorStr.includes('[native code]') && constructorStr.includes('Function');
48+
}
49+
50+
/**
51+
* Check if passed variable is a constructed class instance
52+
*
53+
* @param value - variable to check
54+
* @returns `true` if value is a class instance, otherwise `false`
55+
*/
56+
export function isClassInstance(value: any): boolean {
57+
return !!(value && value.constructor && (/^class \S+ {/).test(value.constructor.toString()));
58+
}
59+
60+
/**
61+
* Check if passed variable is a string
62+
*
63+
* @param value - variable to check
64+
* @returns `true` if value is a string, otherwise `false`
65+
*/
66+
export function isString(value: any): value is string {
67+
return typeof value === 'string';
68+
}

0 commit comments

Comments
 (0)