Skip to content

Commit

Permalink
wip In ts4tooling typeOf.ts module started with unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Brent S.A. Cowgill committed Feb 20, 2023
1 parent 7b8711b commit a79d558
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 2 deletions.
95 changes: 95 additions & 0 deletions bin/template/typescript/ts4tooling/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion bin/template/typescript/ts4tooling/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "lib/app.js",
"type": "commonjs",
"scripts": {
"setupinit": "npm install --save-dev typescript prettier eslint eslint-plugin-import eslint-config-prettier ts-jest @tsconfig/node16 @typescript-eslint/parser @typescript-eslint/eslint-plugin @jest/globals jest-junit nyc-dark",
"setupinit": "npm install --save-dev typescript prettier eslint eslint-plugin-import eslint-config-prettier ts-jest @tsconfig/node16 @typescript-eslint/parser @typescript-eslint/eslint-plugin @jest/globals jest-junit nyc-dark node-notifier",
"preauto": "npm run format && npm run eslint && npm run compile",
"auto": "npm run test",
"build": "echo Need to configue a bundler/packager to build",
Expand Down Expand Up @@ -39,6 +39,7 @@
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-import": "^2.27.5",
"jest-junit": "^15.0.0",
"node-notifier": "^10.0.1",
"nyc-dark": "^3.0.3",
"prettier": "^2.8.4",
"ts-jest": "^29.0.5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ describe('pii module tests', function descPIISuite() {
const type = typeOf(source)
const key = keyName.replace(/[^a-z0-9]/gi, '')

if (DEBUG || keyName === 'list') {
if (DEBUG /*|| keyName === 'list'*/) {
// DEBUG = true
// eslint-disable-next-line no-console
console.warn(
Expand Down
44 changes: 44 additions & 0 deletions bin/template/typescript/ts4tooling/src/__tests__/typeOf.test.ts
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
})
12 changes: 12 additions & 0 deletions bin/template/typescript/ts4tooling/src/typeOf.ts
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
}

0 comments on commit a79d558

Please sign in to comment.