diff --git a/.eslintrc b/.eslintrc index 32cdcc6cc4..1dea1769d9 100644 --- a/.eslintrc +++ b/.eslintrc @@ -6,7 +6,8 @@ "browser": true, "es6": true, "jest": true, - "node": true + "node": true, + "es2021": true }, "globals": { "BigInt": "readonly", diff --git a/package.json b/package.json index 8853f11efd..2b516bbd04 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "lodash", - "version": "5.0.0", + "version": "5.0.1", "license": "MIT", "private": true, "main": "dist/lodash.js", diff --git a/src/isWeakRef.ts b/src/isWeakRef.ts new file mode 100644 index 0000000000..e561de08f6 --- /dev/null +++ b/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; diff --git a/test/isType-checks.spec.js b/test/isType-checks.spec.js index 2a0eb3ace1..466f34300f 100644 --- a/test/isType-checks.spec.js +++ b/test/isType-checks.spec.js @@ -56,6 +56,7 @@ describe('isType checks', () => { 'isUndefined', 'isWeakMap', 'isWeakSet', + 'isWeakRef', ]; lodashStable.each(funcs, (methodName) => { diff --git a/test/isWeakRef.spec.js b/test/isWeakRef.spec.js new file mode 100644 index 0000000000..d83f2884a7 --- /dev/null +++ b/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); + } + }); +}); diff --git a/test/utils.js b/test/utils.js index fc4ed1e5cb..3a852ab292 100644 --- a/test/utils.js +++ b/test/utils.js @@ -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; @@ -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) { @@ -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) {`, @@ -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) {`, @@ -1301,6 +1305,7 @@ export { symbol, weakMap, weakSet, + weakRef, add, doubled, isEven,