Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions yaml/_loader_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]";
}
}
Expand All @@ -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]";
}

Expand Down
6 changes: 2 additions & 4 deletions yaml/_type/omap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>[]): boolean {
const objectKeys: string[] = [];
Expand All @@ -14,9 +14,7 @@ function resolveYamlOmap(data: Record<string, unknown>[]): 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)) {
Expand Down
6 changes: 2 additions & 4 deletions yaml/_type/pairs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@
// 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;

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);

Expand Down
4 changes: 2 additions & 2 deletions yaml/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]";
}