1
0
mirror of synced 2024-11-22 05:16:05 +03:00

Merge pull request #32 from cmath10/fix-record-like-check

fix: Fixed records & arrays detection
This commit is contained in:
Kruglov Kirill 2021-09-21 10:23:59 +03:00 committed by GitHub
commit 9792a1863c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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']],