Skip to content

Commit

Permalink
refactor(yaml): simplify pair construct() function (#5844)
Browse files Browse the repository at this point in the history
* initial commit

* Update yaml/_type/pairs.ts

Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>

* update

* update

* update

* fix

---------

Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>
  • Loading branch information
timreichen and iuioiua authored Aug 28, 2024
1 parent 937da39 commit 8770ff1
Showing 1 changed file with 4 additions and 16 deletions.
20 changes: 4 additions & 16 deletions yaml/_type/pairs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,13 @@ function resolveYamlPairs(data: unknown[][]): boolean {

return true;
}
function constructYamlPairs(data: string) {
if (data === null) return [];

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

for (let index = 0; index < data.length; index += 1) {
const pair = data[index]!;

const keys = Object.keys(pair);

result[index] = [keys[0], pair[keys[0] as keyof typeof pair]];
}

return result;
}

export const pairs: Type<"sequence"> = {
tag: "tag:yaml.org,2002:pairs",
construct: constructYamlPairs,
construct(data: Record<string, unknown>[] | null): [string, unknown][] {
// Converts an array of objects into an array of key-value pairs.
return data?.flatMap(Object.entries) ?? [];
},
kind: "sequence",
resolve: resolveYamlPairs,
};

0 comments on commit 8770ff1

Please sign in to comment.