-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { isset } from "./isset"; | ||
|
||
/** | ||
* Wraps target into a {@link WeakRef}, if target is not already a weak reference | ||
* | ||
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakRef | ||
* | ||
* @template {WeakKey} T | ||
* | ||
* @param {WeakRef<T> | T | undefined} target | ||
* | ||
* @return {WeakRef<T> | undefined} Returns undefined if given target is undefined | ||
*/ | ||
export function toWeakRef<T extends WeakKey>(target: WeakRef<T> | T | undefined): WeakRef<T> | undefined | ||
{ | ||
if (!isset(target)) { | ||
return undefined; | ||
} | ||
|
||
if (target instanceof WeakRef) { | ||
return target; | ||
} | ||
|
||
return new WeakRef<T>(target); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { toWeakRef } from "@aedart/support/misc"; | ||
|
||
describe('@aedart/support/misc', () => { | ||
describe('toWeakRef', () => { | ||
|
||
it('returns undefined when target is null or undefined', () => { | ||
const targets = [ | ||
null, | ||
undefined | ||
]; | ||
|
||
targets.forEach((target, index) => { | ||
expect(toWeakRef(target)) | ||
.withContext(`Target at index ${index} should be undefined`) | ||
.toBeUndefined(); | ||
}); | ||
}); | ||
|
||
it('returns weak reference when target already a weak reference', () => { | ||
|
||
const obj = { name: 'John' }; | ||
|
||
const target = new WeakRef(obj); | ||
|
||
expect(toWeakRef(target)) | ||
.withContext('Invalid target returned') | ||
.toBe(target); | ||
}); | ||
|
||
it('wraps target into weak reference', () => { | ||
|
||
const obj = { name: 'John' }; | ||
|
||
const result = toWeakRef(obj); | ||
|
||
expect(result) | ||
.withContext('No weak reference returned') | ||
.toBeInstanceOf(WeakRef); | ||
|
||
expect(result?.deref()) | ||
.withContext('Dereference failed') | ||
.toBe(obj); | ||
}); | ||
}); | ||
}); |