Skip to main content
Module

x/rambda/source/type.spec.js

Faster and smaller alternative to Ramda
Latest
File
import { type as typeRamda } from 'ramda'
import { type } from './type.js'
test('with buffer', () => { expect(type(new Buffer.from('foo'))).toBe('Uint8Array')})
test('with array buffer', () => { expect(type(new ArrayBuffer(8))).toBe('ArrayBuffer')})
test('with big int', () => { expect(type(BigInt(9007199254740991))).toBe('BigInt')})
test('with generators', () => { function* generator(){ yield 1 yield 2 yield 3 }
const gen = generator() expect(type(generator)).toBe('GeneratorFunction') expect(type(gen)).toBe('Generator')})
test('with Date', () => { const date = new Date('December 17, 1995 03:24:00') expect(type(date)).toBe('Date')})
test('with infinity', () => { expect(type(Infinity)).toBe('Number')})
test('with weak map', () => { expect(type(new WeakMap())).toBe('WeakMap')})
test('with map', () => { expect(type(new Map())).toBe('Map')})
test('with symbol', () => { expect(type(Symbol())).toBe('Symbol')})
test('with simple promise', () => { expect(type(Promise.resolve(1))).toBe('Promise')})
test('with new Boolean', () => { expect(type(new Boolean(true))).toBe('Boolean')})
test('with new String', () => { expect(type(new String('I am a String object'))).toBe('String')})
test('with new Number', () => { expect(type(new Number(1))).toBe('Number')})
test('with error', () => { expect(type(Error('foo'))).toBe('Error') expect(typeRamda(Error('foo'))).toBe('Error')})
test('with error - wrong @types/ramda test', () => { // @types/ramda expect the result to be 'Error' but it is not class ExtendedError extends Error{} expect(type(ExtendedError)).toBe('Function') expect(typeRamda(ExtendedError)).toBe('Function')})
test('with new promise', () => { const delay = ms => new Promise(resolve => { setTimeout(() => { resolve(ms + 110) }, ms) })
expect(type(delay(10))).toBe('Promise')})
test('async function', () => { expect(type(async () => {})).toBe('Promise')})
test('async arrow', () => { const asyncArrow = async () => {} expect(type(asyncArrow)).toBe('Promise')})
test('function', () => { const fn1 = () => {} const fn2 = function (){}
function fn3(){}
;[ () => {}, fn1, fn2, fn3 ].map(val => { expect(type(val)).toBe('Function') })})
test('object', () => { expect(type({})).toBe('Object')})
test('number', () => { expect(type(1)).toBe('Number')})
test('boolean', () => { expect(type(false)).toBe('Boolean')})
test('string', () => { expect(type('foo')).toBe('String')})
test('null', () => { expect(type(null)).toBe('Null')})
test('array', () => { expect(type([])).toBe('Array') expect(type([ 1, 2, 3 ])).toBe('Array')})
test('regex', () => { expect(type(/\s/g)).toBe('RegExp')})
test('undefined', () => { expect(type(undefined)).toBe('Undefined')})
test('not a number', () => { expect(type(Number('s'))).toBe('NaN')})
test('set', () => { const exampleSet = new Set([ 1, 2, 3 ]) expect(type(exampleSet)).toBe('Set') expect(typeRamda(exampleSet)).toBe('Set')})
test('function inside object 1', () => { const obj = { f(){ return 4 }, }
expect(type(obj.f)).toBe('Function') expect(typeRamda(obj.f)).toBe('Function')})
test('function inside object 2', () => { const name = 'f' const obj = { [ name ](){ return 4 }, } expect(type(obj.f)).toBe('Function') expect(typeRamda(obj.f)).toBe('Function')})