refactor(yaml): simplify omap resolve() (#5843)

* initial commit

* update
This commit is contained in:
Tim Reichen 2024-08-28 09:22:55 +02:00 committed by GitHub
parent 8770ff156c
commit c7835496e4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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;
}