Skip to content

Commit

Permalink
feat(gettag): adding util function to get to string tag of a value
Browse files Browse the repository at this point in the history
  • Loading branch information
roggervalf committed Apr 30, 2020
1 parent 4e88c35 commit 29c981f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
16 changes: 16 additions & 0 deletions src/getTag.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { getTag } from './getTag';

describe('getTag', () => {
it('should get Number tag', () => {
expect(getTag(1)).toEqual('[object Number]');
});
it('should get String tag', () => {
expect(getTag('hello')).toEqual('[object String]');
});
it('should get Undefined tag', () => {
expect(getTag(undefined)).toEqual('[object Undefined]');
});
it('should get Null tag', () => {
expect(getTag(null)).toEqual('[object Null]');
});
});
21 changes: 21 additions & 0 deletions src/getTag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const toString = Object.prototype.toString;

/**
* Gets the `toStringTag` of `value`.
* @private
* @param {*} value The value to query.
* @returns {string} Returns the `toStringTag`.
* @example
* ```javascript
* getTag(1)
* // => '[object Number]'
*
* getTag(null)
* // => '[object Null]'
* ```
*/
export function getTag(value: any): string {
return toString.call(value);
}

export default getTag;
3 changes: 2 additions & 1 deletion src/memoize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { MemoizeInterface } from './types';
* @param {Function} [resolver] The function to resolve the cache key.
* @returns {Function} Returns the new memoized function.
* @example
*
* ```javascript
* const object = { 'a': 1, 'b': 2 }
* const other = { 'c': 3, 'd': 4 }
*
Expand All @@ -32,6 +32,7 @@ import { MemoizeInterface } from './types';
* values.cache.set(object, ['a', 'b'])
* values(object)
* // => ['a', 'b']
* ```
*/
export function memoize(func: Function, resolver?: Function): MemoizeInterface {
const memoized = function(...args: any): MemoizeInterface {
Expand Down
9 changes: 6 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export function instanceOfActionBlock(object: object): object is ActionBlock {
* @returns {boolean} Returns true if `object` has `principal` attribute.
* @example
* ```javascript
* instanceOfPrincipalBlock({ principal: 'something' }) => true
* instanceOfPrincipalBlock({ principal: 'something' })
* // => true
* ```
*/
export function instanceOfPrincipalBlock(
Expand All @@ -40,7 +41,8 @@ export function instanceOfPrincipalBlock(
* @returns {boolean} Returns true if `object` has `notResource` attribute.
* @example
* ```javascript
* instanceOfNotResourceBlock({ notResource: 'something' }) => true
* instanceOfNotResourceBlock({ notResource: 'something' })
* // => true
* ```
*/
export function instanceOfNotResourceBlock(
Expand All @@ -55,7 +57,8 @@ export function instanceOfNotResourceBlock(
* @returns {boolean} Returns true if `object` has `resource` attribute.
* @example
* ```javascript
* instanceOfResourceBlock({ resource: 'something' }) => true
* instanceOfResourceBlock({ resource: 'something' })
* // => true
* ```
*/
export function instanceOfResourceBlock(
Expand Down

0 comments on commit 29c981f

Please sign in to comment.