Skip to content

Commit be21277

Browse files
committed
Fix compatibility with Node
1 parent 061dbf0 commit be21277

File tree

5 files changed

+26
-9
lines changed

5 files changed

+26
-9
lines changed

CHANGELOG.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [1.1.4]
11+
12+
### Fixed
13+
14+
- Fix compatibility with Node and `global`/`globalThis`
15+
16+
### Changed
17+
18+
- Improve typing
19+
1020
## [1.1.3]
1121

1222
### Fixed
@@ -67,7 +77,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6777

6878
First version
6979

70-
[unreleased]: https://github.com/MacFJA/js-serializer/compare/1.1.3...HEAD
80+
[unreleased]: https://github.com/MacFJA/js-serializer/compare/1.1.4...HEAD
81+
[1.1.4]: https://github.com/MacFJA/js-serializer/releases/tag/1.1.4
7182
[1.1.3]: https://github.com/MacFJA/js-serializer/releases/tag/1.1.3
7283
[1.1.2]: https://github.com/MacFJA/js-serializer/releases/tag/1.1.2
7384
[1.1.1]: https://github.com/MacFJA/js-serializer/releases/tag/1.1.1

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@macfja/serializer",
3-
"version": "1.1.3",
3+
"version": "1.1.4",
44
"description": "Transform any object, class, array, primitive to a serialized string and vice-versa",
55
"main": "./dist/index.js",
66
"bugs": {

src/index.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,10 +370,13 @@ export function addGlobalAllowedClass(classConstructor: ClassDefinition<any>) {
370370
* @param toPlain The function to transform the class instance (1rst param) to a JS plain object. The second parameter is the transformation process function. If undefined is return the normal transformation is used
371371
* @param fromPlain The function to transform plain js object into a class instance. The second parameter is the transformation process function. If undefined is return the normal transformation is used
372372
*/
373-
export function addClassHandler(
373+
export function addClassHandler<Instance = any, Plain extends object = object>(
374374
classname: string,
375-
toPlain: (source: any, next: (data: any) => PlainType) => object | undefined,
376-
fromPlain: (source: object, next: (data: any) => any) => any | undefined
375+
toPlain: (
376+
source: Instance,
377+
next: (data: any) => PlainType
378+
) => Plain | undefined,
379+
fromPlain: (source: Plain, next: (data: any) => any) => Instance | undefined
377380
): void {
378381
classHandlers[classname] = { toPlain, fromPlain };
379382
}

src/native.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,11 @@ export function addNativeClasses(addClassHandler, addGlobalAllowedClass) {
124124
"BigInt64Array",
125125
"BigUint64Array",
126126
].forEach((constructor) => {
127-
if (typeof global[constructor] !== "undefined") {
128-
addGlobalAllowedClass(global[constructor]);
127+
const globalObject =
128+
(typeof globalThis !== "undefined" && globalThis) ||
129+
(typeof global !== "undefined" && global);
130+
if (globalObject && typeof globalObject[constructor] !== "undefined") {
131+
addGlobalAllowedClass(globalObject[constructor]);
129132
}
130133
});
131134
}

0 commit comments

Comments
 (0)