Skip to content

Commit 68d1303

Browse files
feat: add util to function to get global object
1 parent 8646c65 commit 68d1303

File tree

5 files changed

+49
-13
lines changed

5 files changed

+49
-13
lines changed

src/metadata/MetadataStorage.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ValidationMetadata } from './ValidationMetadata';
22
import { ConstraintMetadata } from './ConstraintMetadata';
33
import { ValidationSchema } from '../validation-schema/ValidationSchema';
44
import { ValidationSchemaToMetadataTransformer } from '../validation-schema/ValidationSchemaToMetadataTransformer';
5+
import { getGlobal } from '../utils';
56

67
/**
78
* Storage all metadatas.
@@ -114,11 +115,11 @@ export class MetadataStorage {
114115
* Metadata storage follows the best practices and stores metadata in a global variable.
115116
*/
116117
export function getMetadataStorage(): MetadataStorage {
117-
if (typeof window !== 'undefined') {
118-
window.global = window;
118+
const global = getGlobal();
119+
120+
if (!global.classValidatorMetadataStorage) {
121+
global.classValidatorMetadataStorage = new MetadataStorage();
119122
}
120-
if (!(global as any).classValidatorMetadataStorage)
121-
(global as any).classValidatorMetadataStorage = new MetadataStorage();
122123

123124
return (global as any).classValidatorMetadataStorage;
124125
}

src/utils/convert-to-array.util.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Convert Map, Set to Array
3+
*/
4+
export function convertToArray<T>(val: Array<T> | Set<T> | Map<any, T>): Array<T> {
5+
if (val instanceof Map) {
6+
return Array.from(val.values());
7+
}
8+
return Array.isArray(val) ? val : Array.from(val);
9+
}

src/utils/get-global.util.ts

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* This function returns the global object across Node and browsers.
3+
*
4+
* Note: `globalThis` is the standardized approach however it has been added to
5+
* Node.js in version 12. We need to include this snippet until Node 12 EOL.
6+
*/
7+
export function getGlobal() {
8+
if (typeof globalThis !== 'undefined') {
9+
return globalThis;
10+
}
11+
12+
if (typeof global !== 'undefined') {
13+
return global;
14+
}
15+
16+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
17+
// @ts-ignore: Cannot find name 'window'.
18+
if (typeof window !== 'undefined') {
19+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
20+
// @ts-ignore: Cannot find name 'window'.
21+
return window;
22+
}
23+
24+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
25+
// @ts-ignore: Cannot find name 'self'.
26+
if (typeof self !== 'undefined') {
27+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
28+
// @ts-ignore: Cannot find name 'self'.
29+
return self;
30+
}
31+
}

src/utils/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './convert-to-array.util';
2+
export * from './get-global.util';
3+
export * from './is-promise.util'

src/utils.ts renamed to src/utils/is-promise.util.ts

+1-9
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,4 @@ export function isPromise<T = any>(p: any): p is Promise<T> {
44
return p !== null && typeof p === 'object' && typeof p.then === 'function';
55
}
66

7-
/**
8-
* Convert Map, Set to Array
9-
*/
10-
export function convertToArray<T>(val: Array<T> | Set<T> | Map<any, T>): Array<T> {
11-
if (val instanceof Map) {
12-
return Array.from(val.values());
13-
}
14-
return Array.isArray(val) ? val : Array.from(val);
15-
}
7+

0 commit comments

Comments
 (0)