-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(issymbol): adding new util for checking if a value is a symbol
- Loading branch information
1 parent
29c981f
commit 6ddf3d1
Showing
3 changed files
with
49 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { isSymbol } from './isSymbol'; | ||
|
||
describe('isSymbol', () => { | ||
it('should return `true` for symbols', () => { | ||
expect(isSymbol(Symbol())).toEqual(true); | ||
expect(isSymbol(Object(Symbol()))).toEqual(true); | ||
}); | ||
|
||
it('should return `false` for non-symbols', () => { | ||
expect(isSymbol()).toEqual(false); | ||
expect(isSymbol(null)).toEqual(false); | ||
expect(isSymbol(undefined)).toEqual(false); | ||
expect(isSymbol(false)).toEqual(false); | ||
expect(isSymbol(0)).toEqual(false); | ||
expect(isSymbol(NaN)).toEqual(false); | ||
expect(isSymbol('')).toEqual(false); | ||
expect(isSymbol([1, 2, 3])).toEqual(false); | ||
expect(isSymbol(new Date())).toEqual(false); | ||
expect(isSymbol(new Error())).toEqual(false); | ||
expect(isSymbol(Array.prototype.slice)).toEqual(false); | ||
expect(isSymbol({ '0': 1, length: 1 })).toEqual(false); | ||
expect(isSymbol(/x/)).toEqual(false); | ||
}); | ||
}); |
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,24 @@ | ||
import { getTag } from './getTag'; | ||
|
||
/** | ||
* Checks if `value` is classified as a `Symbol` primitive or object. | ||
* | ||
* @since 3.0.5 | ||
* @category Lang | ||
* @param {*} value The value to check. | ||
* @returns {boolean} Returns `true` if `value` is a symbol, else `false`. | ||
* @example | ||
* | ||
* isSymbol(Symbol.iterator) | ||
* // => true | ||
* | ||
* isSymbol('abc') | ||
* // => false | ||
*/ | ||
export function isSymbol(value?: any): boolean { | ||
const type = typeof value; | ||
return ( | ||
type === 'symbol' || | ||
(type === 'object' && value !== null && getTag(value) === '[object Symbol]') | ||
); | ||
} |