Skip to content

Commit

Permalink
Merge pull request #14 from andrew-pyle/main
Browse files Browse the repository at this point in the history
Add example for global polyfill to README.md
  • Loading branch information
WebReflection authored Apr 26, 2023
2 parents 8a3b9d9 + 78384c5 commit 4253987
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ An env agnostic serializer and deserializer with recursion ability and types bey
Serialized values can be safely stringified as *JSON* too, and deserialization resurrect all values, even recursive, or more complex than what *JSON* allows.


### Example
### Examples

Check the [100% test coverage](./test/index.js) to know even more.

Expand All @@ -35,6 +35,23 @@ const serialized = serialize({any: 'serializable'});
const deserialized = deserialize(serialized);
```

#### Global Polyfill
Note: Only monkey patch the global if needed. This polyfill works just fine as an explicit import: `import structuredClone from "@ungap/structured-clone"`
```js
// Attach the polyfill as a Global function
import structuredClone from "@ungap/structured-clone";
if (!("structuredClone" in globalThis)) {
globalThis.structuredClone = structuredClone;
}

// Or don't monkey patch
import structuredClone from "@ungap/structured-clone"
// Just use it in the file
structuredClone()
```

**Note**: Do not attach this module's default export directly to the global scope, whithout a conditional guard to detect a native implementation. In environments where there is a native global implementation of `structuredClone()` already, assignment to the global object will result in an infinite loop when `globalThis.structuredClone()` is called. See the example above for a safe way to provide the polyfill globally in your project.

### Extra Features

There is no middle-ground between the structured clone algorithm and JSON:
Expand Down

0 comments on commit 4253987

Please sign in to comment.