From 29fdb0d536a9d6dfbf7134b9f74bfc08a2de4a18 Mon Sep 17 00:00:00 2001 From: Tim Reichen Date: Wed, 28 Aug 2024 07:37:14 +0200 Subject: [PATCH] refactor(yaml): replace `getObjectTypeString()` with `isPlainObject()` (#5842) * initial commit * fmt --- yaml/_loader_state.ts | 12 +++--------- yaml/_type/omap.ts | 6 ++---- yaml/_type/pairs.ts | 6 ++---- yaml/_utils.ts | 4 ++-- 4 files changed, 9 insertions(+), 19 deletions(-) diff --git a/yaml/_loader_state.ts b/yaml/_loader_state.ts index f435cf153de9..59ab168ab396 100644 --- a/yaml/_loader_state.ts +++ b/yaml/_loader_state.ts @@ -38,7 +38,7 @@ import { import { DEFAULT_SCHEMA, type Schema, type TypeMap } from "./_schema.ts"; import type { KindType, Type } from "./_type.ts"; -import { getObjectTypeString, isObject } from "./_utils.ts"; +import { isObject, isPlainObject } from "./_utils.ts"; const CONTEXT_FLOW_IN = 1; const CONTEXT_FLOW_OUT = 2; @@ -456,10 +456,7 @@ export class LoaderState { ); } - if ( - typeof keyNode === "object" && - getObjectTypeString(keyNode[index]) === "[object Object]" - ) { + if (typeof keyNode === "object" && isPlainObject(keyNode[index])) { keyNode[index] = "[object Object]"; } } @@ -468,10 +465,7 @@ export class LoaderState { // Avoid code execution in load() via toString property // (still use its own toString for arrays, timestamps, // and whatever user schema extensions happen to have @@toStringTag) - if ( - typeof keyNode === "object" && - getObjectTypeString(keyNode) === "[object Object]" - ) { + if (typeof keyNode === "object" && isPlainObject(keyNode)) { keyNode = "[object Object]"; } diff --git a/yaml/_type/omap.ts b/yaml/_type/omap.ts index 74084ce68960..3714725154a8 100644 --- a/yaml/_type/omap.ts +++ b/yaml/_type/omap.ts @@ -4,7 +4,7 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. import type { Type } from "../_type.ts"; -import { getObjectTypeString } from "../_utils.ts"; +import { isPlainObject } from "../_utils.ts"; function resolveYamlOmap(data: Record[]): boolean { const objectKeys: string[] = []; @@ -14,9 +14,7 @@ function resolveYamlOmap(data: Record[]): boolean { for (const pair of data) { pairHasKey = false; - if (getObjectTypeString(pair) !== "[object Object]") { - return false; - } + if (!isPlainObject(pair)) return false; for (pairKey in pair) { if (Object.hasOwn(pair, pairKey)) { diff --git a/yaml/_type/pairs.ts b/yaml/_type/pairs.ts index b5c27a0a6e58..f3a6909adc5b 100644 --- a/yaml/_type/pairs.ts +++ b/yaml/_type/pairs.ts @@ -4,7 +4,7 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. import type { Type } from "../_type.ts"; -import { getObjectTypeString } from "../_utils.ts"; +import { isPlainObject } from "../_utils.ts"; function resolveYamlPairs(data: unknown[][]): boolean { if (data === null) return true; @@ -12,9 +12,7 @@ function resolveYamlPairs(data: unknown[][]): boolean { const result = Array.from({ length: data.length }); for (const [index, pair] of data.entries()) { - if (getObjectTypeString(pair) !== "[object Object]") { - return false; - } + if (!isPlainObject(pair)) return false; const keys = Object.keys(pair); diff --git a/yaml/_utils.ts b/yaml/_utils.ts index b90b3adee984..ac9c16e894bd 100644 --- a/yaml/_utils.ts +++ b/yaml/_utils.ts @@ -15,6 +15,6 @@ export function isNegativeZero(i: number): boolean { return i === 0 && Number.NEGATIVE_INFINITY === 1 / i; } -export function getObjectTypeString(object: unknown) { - return Object.prototype.toString.call(object); +export function isPlainObject(object: unknown): object is object { + return Object.prototype.toString.call(object) === "[object Object]"; }