refactor(yaml): change styleMap type to Map (#5760)

* initial commit

* update
This commit is contained in:
Tim Reichen 2024-08-22 04:46:32 +02:00 committed by GitHub
parent 55a6b77e65
commit ba1bc9164a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -89,20 +89,17 @@ function charCodeToHexString(charCode: number): string {
);
}
function compileStyleMap(
map?: Record<string, StyleVariant> | null,
): Record<string, StyleVariant> {
if (typeof map === "undefined" || map === null) return {};
const result: Record<string, StyleVariant> = {};
function createStyleMap(
map: Record<string, StyleVariant>,
): Map<string, StyleVariant> {
const result = new Map();
for (let tag of Object.keys(map)) {
const style = String(map[tag]) as StyleVariant;
if (tag.slice(0, 2) === "!!") {
tag = `tag:yaml.org,2002:${tag.slice(2)}`;
}
result[tag] = style;
result.set(tag, style);
}
return result;
}
@ -430,7 +427,7 @@ export interface DumperStateOptions {
*/
flowLevel?: number;
/** Each tag may have own set of styles. - "tag" => "style" map. */
styles?: Record<string, StyleVariant> | null;
styles?: Record<string, StyleVariant>;
/** specifies a schema to use. */
schema?: Schema;
/**
@ -478,7 +475,7 @@ export class DumperState {
explicitTypes: Type<KindType>[];
duplicates: unknown[] = [];
usedDuplicates: Set<unknown> = new Set();
styleMap: Record<string, StyleVariant>;
styleMap: Map<string, StyleVariant> = new Map();
constructor({
schema = DEFAULT_SCHEMA,
@ -498,7 +495,7 @@ export class DumperState {
this.arrayIndent = arrayIndent;
this.skipInvalid = skipInvalid;
this.flowLevel = flowLevel;
this.styleMap = compileStyleMap(styles);
if (styles) this.styleMap = createStyleMap(styles);
this.sortKeys = sortKeys;
this.lineWidth = lineWidth;
this.useAnchors = useAnchors;
@ -777,7 +774,8 @@ export class DumperState {
tag = explicit ? type.tag : "?";
if (type.represent) {
const style = this.styleMap[type.tag]! || type.defaultStyle;
const style = this.styleMap.get(type.tag) ||
type.defaultStyle as StyleVariant;
if (typeof type.represent === "function") {
value = type.represent(value, style);