refactor(yaml): replace getObjectTypeString() with isPlainObject() (#5842)

* initial commit

* fmt
This commit is contained in:
Tim Reichen 2024-08-28 07:37:14 +02:00 committed by GitHub
parent f7fe38a182
commit 29fdb0d536
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 9 additions and 19 deletions

View File

@ -38,7 +38,7 @@ import {
import { DEFAULT_SCHEMA, type Schema, type TypeMap } from "./_schema.ts"; import { DEFAULT_SCHEMA, type Schema, type TypeMap } from "./_schema.ts";
import type { KindType, Type } from "./_type.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_IN = 1;
const CONTEXT_FLOW_OUT = 2; const CONTEXT_FLOW_OUT = 2;
@ -456,10 +456,7 @@ export class LoaderState {
); );
} }
if ( if (typeof keyNode === "object" && isPlainObject(keyNode[index])) {
typeof keyNode === "object" &&
getObjectTypeString(keyNode[index]) === "[object Object]"
) {
keyNode[index] = "[object Object]"; keyNode[index] = "[object Object]";
} }
} }
@ -468,10 +465,7 @@ export class LoaderState {
// Avoid code execution in load() via toString property // Avoid code execution in load() via toString property
// (still use its own toString for arrays, timestamps, // (still use its own toString for arrays, timestamps,
// and whatever user schema extensions happen to have @@toStringTag) // and whatever user schema extensions happen to have @@toStringTag)
if ( if (typeof keyNode === "object" && isPlainObject(keyNode)) {
typeof keyNode === "object" &&
getObjectTypeString(keyNode) === "[object Object]"
) {
keyNode = "[object Object]"; keyNode = "[object Object]";
} }

View File

@ -4,7 +4,7 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import type { Type } from "../_type.ts"; import type { Type } from "../_type.ts";
import { getObjectTypeString } from "../_utils.ts"; import { isPlainObject } from "../_utils.ts";
function resolveYamlOmap(data: Record<string, unknown>[]): boolean { function resolveYamlOmap(data: Record<string, unknown>[]): boolean {
const objectKeys: string[] = []; const objectKeys: string[] = [];
@ -14,9 +14,7 @@ function resolveYamlOmap(data: Record<string, unknown>[]): boolean {
for (const pair of data) { for (const pair of data) {
pairHasKey = false; pairHasKey = false;
if (getObjectTypeString(pair) !== "[object Object]") { if (!isPlainObject(pair)) return false;
return false;
}
for (pairKey in pair) { for (pairKey in pair) {
if (Object.hasOwn(pair, pairKey)) { if (Object.hasOwn(pair, pairKey)) {

View File

@ -4,7 +4,7 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import type { Type } from "../_type.ts"; import type { Type } from "../_type.ts";
import { getObjectTypeString } from "../_utils.ts"; import { isPlainObject } from "../_utils.ts";
function resolveYamlPairs(data: unknown[][]): boolean { function resolveYamlPairs(data: unknown[][]): boolean {
if (data === null) return true; if (data === null) return true;
@ -12,9 +12,7 @@ function resolveYamlPairs(data: unknown[][]): boolean {
const result = Array.from({ length: data.length }); const result = Array.from({ length: data.length });
for (const [index, pair] of data.entries()) { for (const [index, pair] of data.entries()) {
if (getObjectTypeString(pair) !== "[object Object]") { if (!isPlainObject(pair)) return false;
return false;
}
const keys = Object.keys(pair); const keys = Object.keys(pair);

View File

@ -15,6 +15,6 @@ export function isNegativeZero(i: number): boolean {
return i === 0 && Number.NEGATIVE_INFINITY === 1 / i; return i === 0 && Number.NEGATIVE_INFINITY === 1 / i;
} }
export function getObjectTypeString(object: unknown) { export function isPlainObject(object: unknown): object is object {
return Object.prototype.toString.call(object); return Object.prototype.toString.call(object) === "[object Object]";
} }