1
0
mirror of synced 2024-11-25 06:46: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', 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 { export function typeOf (value: unknown): string {
switch (typeof value) { switch (typeof value) {
case 'bigint': case 'bigint':
@ -76,20 +88,16 @@ export function typeOf (value: unknown): string {
return TYPE.ARRAY return TYPE.ARRAY
} }
if (value.constructor.name === 'Object') { if (isRecord(value)) {
return TYPE.RECORD return TYPE.RECORD
} }
return 'InstanceOf<' + value.constructor.name + '>' return 'InstanceOf<' + (constructorOf(value) as { name?: string }).name + '>'
} }
throw new Error() 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 { export function isScalar (value: unknown): boolean {
switch (typeof value) { switch (typeof value) {
case 'bigint': case 'bigint':

View File

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