UNPKG

@qntm-code/utils

Version:

A collection of useful utility functions with associated TypeScript types. All functions have been unit tested.

286 lines (285 loc) 12.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const moment = require("moment"); const typeOf_1 = require("./typeOf"); describe(`typeOf`, () => { it(`undefined`, () => { expect((0, typeOf_1.typeOf)(undefined)).toBe(typeOf_1.ValueType.undefined); }); it(`null`, () => { expect((0, typeOf_1.typeOf)(null)).toBe(typeOf_1.ValueType.null); }); it(`moment`, () => { expect((0, typeOf_1.typeOf)(moment())).toBe(typeOf_1.ValueType.moment); }); describe(`booleans`, () => { it(`true`, () => { expect((0, typeOf_1.typeOf)(true)).toBe(typeOf_1.ValueType.boolean); }); it(`false`, () => { expect((0, typeOf_1.typeOf)(false)).toBe(typeOf_1.ValueType.boolean); }); it(`new Boolean(true)`, () => { expect((0, typeOf_1.typeOf)(new Boolean(true))).toBe(typeOf_1.ValueType.boolean); }); it(`new Boolean(false)`, () => { expect((0, typeOf_1.typeOf)(new Boolean(false))).toBe(typeOf_1.ValueType.boolean); }); }); describe(`numbers`, () => { it(`0`, () => { expect((0, typeOf_1.typeOf)(0)).toBe(typeOf_1.ValueType.number); }); it(`1`, () => { expect((0, typeOf_1.typeOf)(1)).toBe(typeOf_1.ValueType.number); }); it(`new Number(0)`, () => { expect((0, typeOf_1.typeOf)(new Number(0))).toBe(typeOf_1.ValueType.number); }); it(`new Number(1)`, () => { expect((0, typeOf_1.typeOf)(new Number(1))).toBe(typeOf_1.ValueType.number); }); }); describe(`strings`, () => { it(`''`, () => { expect((0, typeOf_1.typeOf)('')).toBe(typeOf_1.ValueType.string); }); it(`'a'`, () => { expect((0, typeOf_1.typeOf)('a')).toBe(typeOf_1.ValueType.string); }); it(`new String('')`, () => { expect((0, typeOf_1.typeOf)(new String(''))).toBe(typeOf_1.ValueType.string); }); it(`new String('a')`, () => { expect((0, typeOf_1.typeOf)(new String('a'))).toBe(typeOf_1.ValueType.string); }); it(`template strings`, () => { const b = 'b'; expect((0, typeOf_1.typeOf)(`a ${b} c`)).toBe(typeOf_1.ValueType.string); }); }); describe(`objects`, () => { it('arguments', () => { (function () { // eslint-disable-next-line prefer-rest-params expect((0, typeOf_1.typeOf)(arguments)).toBe(typeOf_1.ValueType.arguments); })(); }); it(`buffers`, () => { expect((0, typeOf_1.typeOf)(Buffer.from(''))).toBe(typeOf_1.ValueType.buffer); }); it(`class`, () => { class Test { } expect((0, typeOf_1.typeOf)(new Test())).toBe(typeOf_1.ValueType.object); }); it(`literal`, () => { expect((0, typeOf_1.typeOf)({})).toBe(typeOf_1.ValueType.object); }); it(`created null`, () => { expect((0, typeOf_1.typeOf)(Object.create(null))).toBe(typeOf_1.ValueType.object); }); it(`created object`, () => { expect((0, typeOf_1.typeOf)(Object.create({}))).toBe(typeOf_1.ValueType.object); }); }); it(`dates`, () => { expect((0, typeOf_1.typeOf)(new Date())).toBe(typeOf_1.ValueType.date); }); describe(`arrays`, () => { it(`[]`, () => { expect((0, typeOf_1.typeOf)([])).toBe(typeOf_1.ValueType.array); }); it(`[1]`, () => { expect((0, typeOf_1.typeOf)([1])).toBe(typeOf_1.ValueType.array); }); it(`new Array()`, () => { // eslint-disable-next-line @typescript-eslint/no-array-constructor expect((0, typeOf_1.typeOf)(new Array())).toBe(typeOf_1.ValueType.array); }); it(`new Array(1)`, () => { expect((0, typeOf_1.typeOf)(new Array(1))).toBe(typeOf_1.ValueType.array); }); }); describe(`RegExp`, () => { it(`/a/`, () => { expect((0, typeOf_1.typeOf)(/a/)).toBe(typeOf_1.ValueType.regexp); }); it(`new RegExp('a')`, () => { expect((0, typeOf_1.typeOf)(new RegExp('a'))).toBe(typeOf_1.ValueType.regexp); }); }); describe(`functions`, () => { it(`() => {}`, () => { // eslint-disable-next-line @typescript-eslint/no-empty-function expect((0, typeOf_1.typeOf)(() => { })).toBe(typeOf_1.ValueType.function); }); it(`function () {}`, () => { // eslint-disable-next-line @typescript-eslint/no-empty-function expect((0, typeOf_1.typeOf)(function () { })).toBe(typeOf_1.ValueType.function); }); it(`function* () {}`, () => { // eslint-disable-next-line @typescript-eslint/no-empty-function expect((0, typeOf_1.typeOf)(function* () { })).toBe(typeOf_1.ValueType.generatorfunction); }); it(`new Function()`, () => { // eslint-disable-next-line @typescript-eslint/no-implied-eval expect((0, typeOf_1.typeOf)(new Function())).toBe(typeOf_1.ValueType.function); }); }); it(`errors`, () => { expect((0, typeOf_1.typeOf)(new Error())).toBe(typeOf_1.ValueType.error); }); describe(`Promises`, () => { it(`resolved Promise`, () => { expect((0, typeOf_1.typeOf)(Promise.resolve())).toBe(typeOf_1.ValueType.promise); }); it(`rejected Promise`, () => { // eslint-disable-next-line @typescript-eslint/no-empty-function expect((0, typeOf_1.typeOf)(Promise.reject().catch(() => { }))).toBe(typeOf_1.ValueType.promise); }); }); describe(`generators`, () => { it(`generator function`, () => { function* generator() { yield 1; } expect((0, typeOf_1.typeOf)(generator())).toBe(typeOf_1.ValueType.generator); }); }); describe(`Map`, () => { it(`new Map()`, () => { expect((0, typeOf_1.typeOf)(new Map())).toBe(typeOf_1.ValueType.map); }); it(`Map.set`, () => { const map = new Map(); // eslint-disable-next-line @typescript-eslint/unbound-method expect((0, typeOf_1.typeOf)(map.set)).toBe(typeOf_1.ValueType.function); }); it(`Map.get`, () => { const map = new Map(); // eslint-disable-next-line @typescript-eslint/unbound-method expect((0, typeOf_1.typeOf)(map.get)).toBe(typeOf_1.ValueType.function); }); it(`Map.add`, () => { const map = new Map(); // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access expect((0, typeOf_1.typeOf)(map.add)).toBe(typeOf_1.ValueType.undefined); }); }); describe(`WeakMap`, () => { it(`new WeakMap()`, () => { expect((0, typeOf_1.typeOf)(new WeakMap())).toBe(typeOf_1.ValueType.weakmap); }); it(`WeakMap.set`, () => { const map = new WeakMap(); // eslint-disable-next-line @typescript-eslint/unbound-method expect((0, typeOf_1.typeOf)(map.set)).toBe(typeOf_1.ValueType.function); }); it(`WeakMap.get`, () => { const map = new WeakMap(); // eslint-disable-next-line @typescript-eslint/unbound-method expect((0, typeOf_1.typeOf)(map.get)).toBe(typeOf_1.ValueType.function); }); it(`WeakMap.add`, () => { const map = new WeakMap(); // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access expect((0, typeOf_1.typeOf)(map.add)).toBe(typeOf_1.ValueType.undefined); }); }); describe(`Set`, () => { it(`new Set()`, () => { expect((0, typeOf_1.typeOf)(new Set())).toBe(typeOf_1.ValueType.set); }); it(`Set.set`, () => { const set = new Set(); // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access expect((0, typeOf_1.typeOf)(set.set)).toBe(typeOf_1.ValueType.undefined); }); it(`Set.get`, () => { const set = new Set(); // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access expect((0, typeOf_1.typeOf)(set.get)).toBe(typeOf_1.ValueType.undefined); }); it(`Set.add`, () => { const set = new Set(); // eslint-disable-next-line @typescript-eslint/unbound-method expect((0, typeOf_1.typeOf)(set.add)).toBe(typeOf_1.ValueType.function); }); }); describe(`WeakSet`, () => { it(`new WeakSet()`, () => { expect((0, typeOf_1.typeOf)(new WeakSet())).toBe(typeOf_1.ValueType.weakset); }); it(`WeakSet.weakset`, () => { const weakset = new WeakSet(); // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access expect((0, typeOf_1.typeOf)(weakset.set)).toBe(typeOf_1.ValueType.undefined); }); it(`WeakSet.get`, () => { const weakset = new WeakSet(); // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access expect((0, typeOf_1.typeOf)(weakset.get)).toBe(typeOf_1.ValueType.undefined); }); it(`WeakSet.add`, () => { const weakset = new WeakSet(); // eslint-disable-next-line @typescript-eslint/unbound-method expect((0, typeOf_1.typeOf)(weakset.add)).toBe(typeOf_1.ValueType.function); }); }); it(`Set Iterator`, () => { expect((0, typeOf_1.typeOf)(new Set().values())).toBe(typeOf_1.ValueType.setiterator); }); it(`Map Iterator`, () => { expect((0, typeOf_1.typeOf)(new Map().values())).toBe(typeOf_1.ValueType.mapiterator); }); it(`Array Iterator`, () => { expect((0, typeOf_1.typeOf)([].values())).toBe(typeOf_1.ValueType.arrayiterator); }); it(`String Iterator`, () => { expect((0, typeOf_1.typeOf)(''[Symbol.iterator]())).toBe(typeOf_1.ValueType.stringiterator); }); describe(`Symbol`, () => { it(`Symbol`, () => { expect((0, typeOf_1.typeOf)(Symbol())).toBe(typeOf_1.ValueType.symbol); }); it(`Symbol prototype`, () => { expect((0, typeOf_1.typeOf)(Symbol.prototype)).toBe(typeOf_1.ValueType.symbol); }); }); describe(`Typed Arrays`, () => { it(`Int8Array`, () => { expect((0, typeOf_1.typeOf)(new Int8Array())).toBe(typeOf_1.ValueType.int8array); }); it(`Uint8Array`, () => { expect((0, typeOf_1.typeOf)(new Uint8Array())).toBe(typeOf_1.ValueType.uint8array); }); it(`Uint8ClampedArray`, () => { expect((0, typeOf_1.typeOf)(new Uint8ClampedArray())).toBe(typeOf_1.ValueType.uint8clampedarray); }); it(`Int16Array`, () => { expect((0, typeOf_1.typeOf)(new Int16Array())).toBe(typeOf_1.ValueType.int16array); }); it(`Uint16Array`, () => { expect((0, typeOf_1.typeOf)(new Uint16Array())).toBe(typeOf_1.ValueType.uint16array); }); it(`Int32Array`, () => { expect((0, typeOf_1.typeOf)(new Int32Array())).toBe(typeOf_1.ValueType.int32array); }); it(`Uint32Array`, () => { expect((0, typeOf_1.typeOf)(new Uint32Array())).toBe(typeOf_1.ValueType.uint32array); }); it(`Float32Array`, () => { expect((0, typeOf_1.typeOf)(new Float32Array())).toBe(typeOf_1.ValueType.float32array); }); it(`Float64Array`, () => { expect((0, typeOf_1.typeOf)(new Float64Array())).toBe(typeOf_1.ValueType.float64array); }); it(`BigInt64Array`, () => { expect((0, typeOf_1.typeOf)(new BigInt64Array())).toBe(typeOf_1.ValueType.bigint64array); }); it(`BigUint64Array`, () => { expect((0, typeOf_1.typeOf)(new BigUint64Array())).toBe(typeOf_1.ValueType.biguint64array); }); }); });