-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip In ts4tooling typeOf.ts module started with unit tests
- Loading branch information
Brent S.A. Cowgill
committed
Feb 20, 2023
1 parent
7b8711b
commit a79d558
Showing
5 changed files
with
154 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
44 changes: 44 additions & 0 deletions
44
bin/template/typescript/ts4tooling/src/__tests__/typeOf.test.ts
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,44 @@ | ||
import { describe, expect, test } from '@jest/globals' | ||
import typeOf from '../typeOf' | ||
|
||
describe('typeOf() module tests', function descTypeOfSuite() { | ||
test('primitive types', function testTypeOfPrimitive() { | ||
expect(typeOf(void 0)).toBe('undefined') | ||
|
||
expect(typeOf(false)).toBe('boolean') | ||
expect(typeOf(true)).toBe('boolean') | ||
|
||
expect(typeOf(0)).toBe('number') | ||
expect(typeOf(0.5)).toBe('number') | ||
expect(typeOf(-1)).toBe('number') | ||
expect(typeOf(1)).toBe('number') | ||
expect(typeOf(NaN)).toBe('number') | ||
expect(typeOf(Infinity)).toBe('number') | ||
|
||
// expect(typeOf(0n)).toBe('number') | ||
|
||
expect(typeOf('')).toBe('string') | ||
expect(typeOf('whatever')).toBe('string') | ||
}) // primitive types | ||
|
||
test('abnormal primitive types', function testTypeOfAbnormalPrimitive() { | ||
expect(typeOf(null)).toBe('null') | ||
}) // abnormal primitive types | ||
|
||
test('object-like types', function testTypeOfPrimitive() { | ||
expect(typeOf(/matchThis/)).toBe('object:RegExp') | ||
expect(typeOf([])).toBe('object:Array') | ||
expect(typeOf({})).toBe('object') | ||
expect(typeOf(function () {})).toBe('function') | ||
expect(typeOf(() => {})).toBe('function') | ||
expect(typeOf(new Date())).toBe('object:Date') | ||
expect(typeOf(new Set())).toBe('object:Set') | ||
expect(typeOf(new Map())).toBe('object:Map') | ||
|
||
const anon = new (function (x, y) { this.x = x; this.y = y })(1, -1) | ||
const anon2 = new (function (x, y, z) { this.x = x; this.y = y, this.z = z })(1, -1, 0) | ||
expect(typeOf(anon)).toBe('object:Anon') // Anon0 | ||
expect(typeOf(anon2)).toBe('object:Anon') // Anon1 | ||
expect(typeOf(anon2)).not.toBe(typeOf(anon)) | ||
}) // object-like types | ||
}) |
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 @@ | ||
export default function typeOf(source): string { | ||
const primitive = typeof source | ||
let type : string = primitive | ||
if (source === null) { | ||
type = 'null' | ||
} | ||
else if (primitive === 'object') { | ||
const konstructor = source.constructor.name || 'Anon' | ||
type += konstructor !== 'Object' ? ':' + konstructor : '' | ||
} | ||
return type | ||
} |