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): simplify omap resolve() #5843

Merged
merged 3 commits into from
Aug 28, 2024
Merged
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
28 changes: 8 additions & 20 deletions yaml/_type/omap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,16 @@ import type { Type } from "../_type.ts";
import { isPlainObject } from "../_utils.ts";

function resolveYamlOmap(data: Record<string, unknown>[]): boolean {
const objectKeys: string[] = [];
let pairKey = "";
let pairHasKey = false;

for (const pair of data) {
pairHasKey = false;

if (!isPlainObject(pair)) return false;

for (pairKey in pair) {
if (Object.hasOwn(pair, pairKey)) {
if (!pairHasKey) pairHasKey = true;
else return false;
}
const objectKeys = new Set();
for (const object of data) {
if (!isPlainObject(object)) return false;
const keys = Object.keys(object);
if (keys.length !== 1) return false;
for (const key of keys) {
if (objectKeys.has(key)) return false;
objectKeys.add(key);
}

if (!pairHasKey) return false;

if (!objectKeys.includes(pairKey)) objectKeys.push(pairKey);
else return false;
}

return true;
}

Expand Down