-
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(tokey): adding util to convert value to a string key
- Loading branch information
1 parent
97e60ed
commit 8d19068
Showing
2 changed files
with
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { toKey } from './toKey'; | ||
|
||
describe('toKey', () => { | ||
describe('when converting value to a string key', () => { | ||
it('should get true', () => { | ||
const symbol = Symbol(1); | ||
expect(toKey(1)).toEqual('1'); | ||
expect(toKey(symbol)).toEqual(symbol); | ||
expect(toKey(-0)).toEqual('-0'); | ||
}); | ||
}); | ||
}); |
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,19 @@ | ||
import { isSymbol } from './isSymbol'; | ||
|
||
/** Used as references for various `Number` constants. */ | ||
const INFINITY = 1 / 0; | ||
|
||
/** | ||
* Converts `value` to a string key if it's not a string or symbol. | ||
* | ||
* @private | ||
* @param {*} value The value to inspect. | ||
* @returns {string|symbol} Returns the key. | ||
*/ | ||
export function toKey(value: any): string | symbol { | ||
if (typeof value === 'string' || isSymbol(value)) { | ||
return value; | ||
} | ||
const result = `${value}`; | ||
return result === '0' && 1 / value === -INFINITY ? '-0' : result; | ||
} |