1
0
mirror of synced 2024-11-24 22:36:02 +03:00

fix: Fixed records & arrays detection

This commit is contained in:
Zaytsev Kirill 2021-09-20 21:47:37 +03:00
parent f1094bb6c9
commit 1d785ec5eb
2 changed files with 15 additions and 6 deletions

View File

@ -47,6 +47,18 @@ export enum TYPE {
NULL = 'NULL',
}
const constructorOf = (value: object): unknown => {
return Object.getPrototypeOf(value).constructor
}
export function isRecord (value: object): boolean {
return constructorOf(value) === Object && Object.keys(Object.getPrototypeOf(value)).length === 0
}
export function isRecordLike (value: unknown): boolean {
return typeof value === 'object' && value !== null && (isRecord(value) || Array.isArray(value))
}
export function typeOf (value: unknown): string {
switch (typeof value) {
case 'bigint':
@ -76,20 +88,16 @@ export function typeOf (value: unknown): string {
return TYPE.ARRAY
}
if (value.constructor.name === 'Object') {
if (isRecord(value)) {
return TYPE.RECORD
}
return 'InstanceOf<' + value.constructor.name + '>'
return 'InstanceOf<' + (constructorOf(value) as { name?: string }).name + '>'
}
throw new Error()
}
export function isRecordLike (value: unknown): boolean {
return typeof value === 'object' && value !== null && ['Array', 'Object'].includes(value.constructor.name)
}
export function isScalar (value: unknown): boolean {
switch (typeof value) {
case 'bigint':

View File

@ -15,6 +15,7 @@ describe('types', () => {
const records = [
[{}],
[{ constructor: null }],
[{ a: 'a', b: ['b'] }],
[[]],
[['b', 'c']],