Skip to content

Instantly share code, notes, and snippets.

@zachvictor
Last active July 11, 2021 10:38
Show Gist options
  • Save zachvictor/bf90a4b6025e3e0e766852257a656fae to your computer and use it in GitHub Desktop.
Save zachvictor/bf90a4b6025e3e0e766852257a656fae to your computer and use it in GitHub Desktop.
urtype() is a one-line wrapper for Object.prototype.toString.call() that extracts the second word as the "urtype" (Anglophone misappropriation of Germanic prefix -- guilty as charged)
const urtype = (x) => Object.prototype.toString.call(x).split(' ')[1].slice(0, -1)
// examples
[
['undefined primitive', undefined],
['undefined via void operator', void ''],
['null', null],
['boolean primitive', true],
['number primitive', 123],
['string primitive', 'I feel pretty'],
['symbol primitive', Symbol('Oh so pretty')],
['modest BigInt', 1n],
['cheeky BigInt', BigInt(Number.MAX_SAFE_INTEGER) + 1n],
['object', {foo: 'bar'}],
['array', ['I', 'feel', 'pretty', 'and', 'witty', 'and', 'gay']],
['Map', new Map()],
['Int8Array', new Int8Array()],
].forEach(([label, testInput]) => console.log(`${label} : ${urtype(testInput)}`))
// undefined primitive : Undefined
// undefined via void operator : Undefined
// null : Null
// boolean primitive : Boolean
// number primitive : Number
// string primitive : String
// symbol primitive : Symbol
// modest BigInt : BigInt
// cheeky BigInt : BigInt
// object : Object
// array : Array
// Map : Map
// Int8Array : Int8Array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment