refactor(yaml): simplify boolean type (#5859)

initial commit
This commit is contained in:
Tim Reichen 2024-08-29 02:15:38 +02:00 committed by GitHub
parent b8df2b3494
commit 11fce1a431
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 26 deletions

View File

@ -4,36 +4,22 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import type { Type } from "../_type.ts";
import { isBoolean } from "../_utils.ts";
const YAML_TRUE_BOOLEANS = ["true", "True", "TRUE"];
const YAML_FALSE_BOOLEANS = ["false", "False", "FALSE"];
const YAML_BOOLEANS = [...YAML_TRUE_BOOLEANS, ...YAML_FALSE_BOOLEANS];
function resolveYamlBoolean(data: string): boolean {
return YAML_BOOLEANS.includes(data);
}
function constructYamlBoolean(data: string): boolean {
return YAML_TRUE_BOOLEANS.includes(data);
}
export const bool: Type<"scalar", boolean> = {
tag: "tag:yaml.org,2002:bool",
construct: constructYamlBoolean,
defaultStyle: "lowercase",
kind: "scalar",
predicate: isBoolean,
defaultStyle: "lowercase",
predicate: (value: unknown): value is boolean =>
typeof value === "boolean" || value instanceof Boolean,
construct: (data: string): boolean => YAML_TRUE_BOOLEANS.includes(data),
resolve: (data: string): boolean => YAML_BOOLEANS.includes(data),
represent: {
lowercase(object: boolean): string {
return object ? "true" : "false";
lowercase: (object: boolean): string => object ? "true" : "false",
uppercase: (object: boolean): string => object ? "TRUE" : "FALSE",
camelcase: (object: boolean): string => object ? "True" : "False",
},
uppercase(object: boolean): string {
return object ? "TRUE" : "FALSE";
},
camelcase(object: boolean): string {
return object ? "True" : "False";
},
},
resolve: resolveYamlBoolean,
};

View File

@ -3,10 +3,6 @@
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
export function isBoolean(value: unknown): value is boolean {
return typeof value === "boolean" || value instanceof Boolean;
}
export function isObject(value: unknown): value is Record<string, unknown> {
return value !== null && typeof value === "object";
}