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): move getObjectTypeString() #5332

Merged
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
9 changes: 7 additions & 2 deletions yaml/_dumper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ import {
import { YamlError } from "./_error.ts";
import { DEFAULT_SCHEMA, type Schema } from "./_schema.ts";
import type { StyleVariant, Type } from "./_type.ts";
import { type Any, type ArrayObject, isObject } from "./_utils.ts";
import {
type Any,
type ArrayObject,
getObjectTypeString,
isObject,
} from "./_utils.ts";

const ESCAPE_SEQUENCES = new Map<number, string>([
[0x00, "\\0"],
Expand Down Expand Up @@ -875,7 +880,7 @@ function writeNode(
if (state.skipInvalid) return false;
throw new YamlError(
`unacceptable kind of an object to dump ${
Object.prototype.toString.call(state.dump)
getObjectTypeString(state.dump)
}`,
);
}
Expand Down
11 changes: 6 additions & 5 deletions yaml/_loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ import { YamlError } from "./_error.ts";
import { Mark } from "./_mark.ts";
import { DEFAULT_SCHEMA, type Schema, type TypeMap } from "./_schema.ts";
import type { Type } from "./_type.ts";
import { type Any, type ArrayObject, isObject } from "./_utils.ts";
import {
type Any,
type ArrayObject,
getObjectTypeString,
isObject,
} from "./_utils.ts";

const CONTEXT_FLOW_IN = 1;
const CONTEXT_FLOW_OUT = 2;
Expand Down Expand Up @@ -98,10 +103,6 @@ const SIMPLE_ESCAPE_SEQUENCES = new Map<number, string>([
[0x50, "\u2029"], // P
]);

function getObjectTypeString(object: unknown) {
return Object.prototype.toString.call(object);
}

/**
* Converts a hexadecimal character code to its decimal value.
*/
Expand Down
4 changes: 2 additions & 2 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 type { Any } from "../_utils.ts";
import { type Any, getObjectTypeString } from "../_utils.ts";

function resolveYamlOmap(data: Any): boolean {
const objectKeys: string[] = [];
Expand All @@ -14,7 +14,7 @@ function resolveYamlOmap(data: Any): boolean {
for (const pair of data) {
pairHasKey = false;

if (Object.prototype.toString.call(pair) !== "[object Object]") {
if (getObjectTypeString(pair) !== "[object Object]") {
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions yaml/_type/pairs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import type { Type } from "../_type.ts";
import type { Any } from "../_utils.ts";
import { type Any, getObjectTypeString } from "../_utils.ts";

function resolveYamlPairs(data: Any[][]): boolean {
if (data === null) return true;

const result = Array.from({ length: data.length });

for (const [index, pair] of data.entries()) {
if (Object.prototype.toString.call(pair) !== "[object Object]") {
if (getObjectTypeString(pair) !== "[object Object]") {
return false;
}

Expand Down
4 changes: 4 additions & 0 deletions yaml/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ export function isObject(value: unknown): value is Record<string, unknown> {
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);
}