Skip to content

Commit 3ed0219

Browse files
committed
refactor(core): type-guards utils moved from sanitizer
1 parent 0a0bf23 commit 3ed0219

File tree

3 files changed

+58
-64
lines changed

3 files changed

+58
-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: 50 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
*
@@ -7,3 +9,51 @@
79
export function isPlainObject(value: unknown): value is Record<string, unknown> {
810
return Object.prototype.toString.call(value) === '[object Object]';
911
}
12+
13+
/**
14+
* Check if passed variable is an array
15+
*
16+
* @param value - variable to check
17+
*/
18+
export function isArray(value: any): value is any[] {
19+
return Array.isArray(value);
20+
}
21+
22+
/**
23+
* Check if passed variable is a not-constructed class
24+
*
25+
* @param value - variable to check
26+
*/
27+
export function isClassPrototype(value: any): boolean {
28+
if (!value || !value.constructor) {
29+
return false;
30+
}
31+
32+
/**
33+
* like
34+
* "function Function {
35+
* [native code]
36+
* }"
37+
*/
38+
const constructorStr = value.constructor.toString();
39+
40+
return constructorStr.includes('[native code]') && constructorStr.includes('Function');
41+
}
42+
43+
/**
44+
* Check if passed variable is a constructed class instance
45+
*
46+
* @param value - variable to check
47+
*/
48+
export function isClassInstance(value: any): boolean {
49+
return value && value.constructor && (/^class \S+ {/).test(value.constructor.toString());
50+
}
51+
52+
/**
53+
* Check if passed variable is a string
54+
*
55+
* @param value - variable to check
56+
*/
57+
export function isString(value: any): value is string {
58+
return typeof value === 'string';
59+
}

0 commit comments

Comments
 (0)