Skip to content

Commit 387cb49

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

File tree

3 files changed

+56
-64
lines changed

3 files changed

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

0 commit comments

Comments
 (0)