diff options
author | Lilian Saget-Lethias <lilian.sagetlethias@gmail.com> | 2019-11-18 15:39:32 +0100 |
---|---|---|
committer | Ry Dahl <ry@tinyclouds.org> | 2019-11-18 09:39:32 -0500 |
commit | 5671d38d8f0176c2828a242a79a856fcfae93c3e (patch) | |
tree | 54e059f109baae9b3ff9161847149ecdac753e50 /std/encoding/yaml/utils.ts | |
parent | a2f5bccad7f73607dc4ec20bdb60c6c4ac8dc9e6 (diff) |
feat: Add std/encoding/yaml module (#3361)
Diffstat (limited to 'std/encoding/yaml/utils.ts')
-rw-r--r-- | std/encoding/yaml/utils.ts | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/std/encoding/yaml/utils.ts b/std/encoding/yaml/utils.ts new file mode 100644 index 000000000..2f4ea00b5 --- /dev/null +++ b/std/encoding/yaml/utils.ts @@ -0,0 +1,84 @@ +// Ported from js-yaml v3.13.1: +// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da +// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. + +/* eslint-disable-next-line @typescript-eslint/no-explicit-any */ +export type Any = any; + +export function isNothing(subject: unknown): subject is never { + return typeof subject === "undefined" || subject === null; +} + +export function isArray(value: unknown): value is Any[] { + return Array.isArray(value); +} + +export function isBoolean(value: unknown): value is boolean { + return typeof value === "boolean" || value instanceof Boolean; +} + +export function isNull(value: unknown): value is null { + return value === null; +} + +export function isNullOrUndefined(value: unknown): value is null | undefined { + return value === null || value === undefined; +} + +export function isNumber(value: unknown): value is number { + return typeof value === "number" || value instanceof Number; +} + +export function isString(value: unknown): value is string { + return typeof value === "string" || value instanceof String; +} + +export function isSymbol(value: unknown): value is symbol { + return typeof value === "symbol"; +} + +export function isUndefined(value: unknown): value is undefined { + return value === undefined; +} + +export function isObject(value: unknown): value is object { + return value !== null && typeof value === "object"; +} + +export function isError(e: unknown): boolean { + return e instanceof Error; +} + +export function isFunction(value: unknown): value is () => void { + return typeof value === "function"; +} + +export function isRegExp(value: unknown): value is RegExp { + return value instanceof RegExp; +} + +export function toArray<T>(sequence: T): T | [] | [T] { + if (isArray(sequence)) return sequence; + if (isNothing(sequence)) return []; + + return [sequence]; +} + +export function repeat(str: string, count: number): string { + let result = ""; + + for (let cycle = 0; cycle < count; cycle++) { + result += str; + } + + return result; +} + +export function isNegativeZero(i: number): boolean { + return i === 0 && Number.NEGATIVE_INFINITY === 1 / i; +} + +export interface ArrayObject<T = Any> { + [P: string]: T; +} |