Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "isWeakRef" function #5787

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc
Expand Up @@ -6,7 +6,8 @@
"browser": true,
"es6": true,
"jest": true,
"node": true
"node": true,
"es2021": true
},
"globals": {
"BigInt": "readonly",
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "lodash",
"version": "5.0.0",
"version": "5.0.1",
"license": "MIT",
"private": true,
"main": "dist/lodash.js",
Expand Down
23 changes: 23 additions & 0 deletions src/isWeakRef.ts
@@ -0,0 +1,23 @@
import getTag from './.internal/getTag.js';
import isObjectLike from './isObjectLike.js';

/**
* Checks if `value` is classified as a `WeakRef` object.
*
* @since 5.0.1
* @category Lang
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is a weak ref, else `false`.
* @example
*
* isWeakRef(new WeakRef)
* // => true
*
* isWeakRef(new WeakMap)
* // => false
*/
function isWeakRef(value) {
return isObjectLike(value) && getTag(value) === '[object WeakRef]';
}

export default isWeakRef;
1 change: 1 addition & 0 deletions test/isType-checks.spec.js
Expand Up @@ -56,6 +56,7 @@ describe('isType checks', () => {
'isUndefined',
'isWeakMap',
'isWeakSet',
'isWeakRef',
];

lodashStable.each(funcs, (methodName) => {
Expand Down
43 changes: 43 additions & 0 deletions test/isWeakRef.spec.js
@@ -0,0 +1,43 @@
import lodashStable from 'lodash';
import { weakRef, falsey, stubFalse, args, slice, set, map, weakSet, weakMap, symbol, realm } from './utils';
import isWeakRef from '../src/isWeakRef';

describe('isWeakRef', () => {
it('should return `true` for weak refs', () => {
if (WeakRef) {
expect(isWeakRef(weakRef)).toBe(true);
}
});

it('should return `false` for non weak refs', () => {
const expected = lodashStable.map(falsey, stubFalse);

const actual = lodashStable.map(falsey, (value, index) =>
index ? isWeakRef(value) : isWeakRef(),
);

expect(actual).toEqual(expected);

expect(isWeakRef(args)).toBe(false);
expect(isWeakRef([1, 2, 3])).toBe(false);
expect(isWeakRef(true)).toBe(false);
expect(isWeakRef(new Date())).toBe(false);
expect(isWeakRef(new Error())).toBe(false);
expect(isWeakRef(slice)).toBe(false);
expect(isWeakRef({ a: 1 })).toBe(false);
expect(isWeakRef(1)).toBe(false);
expect(isWeakRef(/x/)).toBe(false);
expect(isWeakRef('a')).toBe(false);
expect(isWeakRef(set)).toBe(false);
expect(isWeakRef(map)).toBe(false);
expect(isWeakRef(weakSet)).toBe(false);
expect(isWeakRef(weakMap)).toBe(false);
expect(isWeakRef(symbol)).toBe(false);
});

it('should work with weak refs from another realm', () => {
if (realm.weakRef) {
expect(isWeakRef(realm.weakRef)).toBe(true);
}
});
});
5 changes: 5 additions & 0 deletions test/utils.js
Expand Up @@ -74,6 +74,7 @@ const Symbol = root.Symbol;
const Uint8Array = root.Uint8Array;
const WeakMap = root.WeakMap;
const WeakSet = root.WeakSet;
const WeakRef = root.WeakRef;

const arrayBuffer = ArrayBuffer ? new ArrayBuffer(2) : undefined;
const map = Map ? new Map() : undefined;
Expand All @@ -82,6 +83,7 @@ const set = Set ? new Set() : undefined;
const symbol = Symbol ? Symbol('a') : undefined;
const weakMap = WeakMap ? new WeakMap() : undefined;
const weakSet = WeakSet ? new WeakSet() : undefined;
const weakRef = WeakRef ? new WeakRef() : undefined;

/** Math helpers. */
const add = function (x, y) {
Expand Down Expand Up @@ -1152,6 +1154,7 @@ lodashStable.attempt(() => {
" 'undefined': undefined,",
" 'weakMap': root.WeakMap ? new root.WeakMap : undefined,",
" 'weakSet': root.WeakSet ? new root.WeakSet : undefined",
" 'weakRef': root.WeakRef ? new root.WeakRef : undefined",
' };',
'',
` ['${arrayViews.join("', '")}'].forEach(function(type) {`,
Expand Down Expand Up @@ -1208,6 +1211,7 @@ lodashStable.attempt(() => {
" 'undefined': undefined,",
" 'weakMap': root.WeakMap ? new root.WeakMap : undefined,",
" 'weakSet': root.WeakSet ? new root.WeakSet : undefined",
" 'weakRef': root.WeakRef ? new root.WeakRef : undefined",
'};',
'',
`_.each(['${arrayViews.join("', '")}'], function(type) {`,
Expand Down Expand Up @@ -1301,6 +1305,7 @@ export {
symbol,
weakMap,
weakSet,
weakRef,
add,
doubled,
isEven,
Expand Down