diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2020-07-14 15:24:17 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-14 15:24:17 -0400 |
commit | cde4dbb35132848ffece59ef9cfaccff32347124 (patch) | |
tree | cc7830968c6decde704c8cfb83c9185193dc698f /std/fmt | |
parent | 9eca71caa1674c31f9cc5d4e86c03f10b59e0a00 (diff) |
Use dprint for internal formatting (#6682)
Diffstat (limited to 'std/fmt')
-rw-r--r-- | std/fmt/colors.ts | 20 | ||||
-rw-r--r-- | std/fmt/colors_test.ts | 4 | ||||
-rw-r--r-- | std/fmt/printf.ts | 27 | ||||
-rw-r--r-- | std/fmt/printf_test.ts | 36 |
4 files changed, 49 insertions, 38 deletions
diff --git a/std/fmt/colors.ts b/std/fmt/colors.ts index a020657d9..7a12b2883 100644 --- a/std/fmt/colors.ts +++ b/std/fmt/colors.ts @@ -186,7 +186,10 @@ export function rgb24(str: string, color: number | Rgb): string { if (typeof color === "number") { return run( str, - code([38, 2, (color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff], 39) + code( + [38, 2, (color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff], + 39, + ), ); } return run( @@ -199,8 +202,8 @@ export function rgb24(str: string, color: number | Rgb): string { clampAndTruncate(color.g), clampAndTruncate(color.b), ], - 39 - ) + 39, + ), ); } @@ -217,7 +220,10 @@ export function bgRgb24(str: string, color: number | Rgb): string { if (typeof color === "number") { return run( str, - code([48, 2, (color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff], 49) + code( + [48, 2, (color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff], + 49, + ), ); } return run( @@ -230,8 +236,8 @@ export function bgRgb24(str: string, color: number | Rgb): string { clampAndTruncate(color.g), clampAndTruncate(color.b), ], - 49 - ) + 49, + ), ); } @@ -241,7 +247,7 @@ const ANSI_PATTERN = new RegExp( "[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)", "(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))", ].join("|"), - "g" + "g", ); export function stripColor(string: string): string { diff --git a/std/fmt/colors_test.ts b/std/fmt/colors_test.ts index 7a312202a..dea27f84e 100644 --- a/std/fmt/colors_test.ts +++ b/std/fmt/colors_test.ts @@ -142,7 +142,7 @@ Deno.test("test_rgb24", function (): void { g: 42, b: 43, }), - "[38;2;41;42;43mfoo bar[39m" + "[38;2;41;42;43mfoo bar[39m", ); }); @@ -157,7 +157,7 @@ Deno.test("test_bgRgb24", function (): void { g: 42, b: 43, }), - "[48;2;41;42;43mfoo bar[49m" + "[48;2;41;42;43mfoo bar[49m", ); }); diff --git a/std/fmt/printf.ts b/std/fmt/printf.ts index bf5feaaed..36b6dddb0 100644 --- a/std/fmt/printf.ts +++ b/std/fmt/printf.ts @@ -153,8 +153,9 @@ class Printf { break; case State.POSITIONAL: // either a verb or * only verb for now, TODO if (c === "*") { - const worp = - this.flags.precision === -1 ? WorP.WIDTH : WorP.PRECISION; + const worp = this.flags.precision === -1 + ? WorP.WIDTH + : WorP.PRECISION; this.handleWidthOrPrecisionRef(worp); this.state = State.PERCENT; break; @@ -503,8 +504,9 @@ class Printf { } let fractional = m[F.fractional]; - const precision = - this.flags.precision !== -1 ? this.flags.precision : DEFAULT_PRECISION; + const precision = this.flags.precision !== -1 + ? this.flags.precision + : DEFAULT_PRECISION; fractional = this.roundFractionToPrecision(fractional, precision); let e = m[F.exponent]; @@ -553,8 +555,9 @@ class Printf { const dig = arr[0]; let fractional = arr[1]; - const precision = - this.flags.precision !== -1 ? this.flags.precision : DEFAULT_PRECISION; + const precision = this.flags.precision !== -1 + ? this.flags.precision + : DEFAULT_PRECISION; fractional = this.roundFractionToPrecision(fractional, precision); return this.padNum(`${dig}.${fractional}`, n < 0); @@ -589,8 +592,9 @@ class Printf { // converted in the style of an f or F conversion specifier. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/fprintf.html - let P = - this.flags.precision !== -1 ? this.flags.precision : DEFAULT_PRECISION; + let P = this.flags.precision !== -1 + ? this.flags.precision + : DEFAULT_PRECISION; P = P === 0 ? 1 : P; const m = n.toExponential().match(FLOAT_REGEXP); @@ -650,15 +654,16 @@ class Printf { } default: throw new Error( - "currently only number and string are implemented for hex" + "currently only number and string are implemented for hex", ); } } fmtV(val: object): string { if (this.flags.sharp) { - const options = - this.flags.precision !== -1 ? { depth: this.flags.precision } : {}; + const options = this.flags.precision !== -1 + ? { depth: this.flags.precision } + : {}; return this.pad(Deno.inspect(val, options)); } else { const p = this.flags.precision; diff --git a/std/fmt/printf_test.ts b/std/fmt/printf_test.ts index a10fdf516..54adc8b55 100644 --- a/std/fmt/printf_test.ts +++ b/std/fmt/printf_test.ts @@ -35,19 +35,19 @@ Deno.test("testIntegerB", function (): void { assertEquals(S("%b", -4), "-100"); assertEquals( S("%b", 4.1), - "100.0001100110011001100110011001100110011001100110011" + "100.0001100110011001100110011001100110011001100110011", ); assertEquals( S("%b", -4.1), - "-100.0001100110011001100110011001100110011001100110011" + "-100.0001100110011001100110011001100110011001100110011", ); assertEquals( S("%b", Number.MAX_SAFE_INTEGER), - "11111111111111111111111111111111111111111111111111111" + "11111111111111111111111111111111111111111111111111111", ); assertEquals( S("%b", Number.MIN_SAFE_INTEGER), - "-11111111111111111111111111111111111111111111111111111" + "-11111111111111111111111111111111111111111111111111111", ); // width @@ -137,18 +137,18 @@ Deno.test("testFloatfF", function (): void { assertEquals( S("%.324f", Number.MIN_VALUE), // eslint-disable-next-line max-len - "0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005" + "0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005", ); assertEquals(S("%F", Number.MIN_VALUE), "0.000000"); assertEquals( S("%f", Number.MAX_VALUE), // eslint-disable-next-line max-len - "179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000" + "179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000", ); assertEquals( S("%F", Number.MAX_VALUE), // eslint-disable-next-line max-len - "179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000" + "179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000", ); }); @@ -190,7 +190,7 @@ Deno.test("testWidthAndPrecision", function (): void { assertEquals( S("%9.99d", 9), // eslint-disable-next-line max-len - "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009" + "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009", ); assertEquals(S("%1.12d", 9), "000000000009"); assertEquals(S("%2s", "a"), " a"); @@ -200,12 +200,12 @@ Deno.test("testWidthAndPrecision", function (): void { assertEquals( S("%*.99d", 9, 9), // eslint-disable-next-line max-len - "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009" + "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009", ); assertEquals( S("%9.*d", 99, 9), // eslint-disable-next-line max-len - "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009" + "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009", ); assertEquals(S("%*s", 2, "a"), " a"); assertEquals(S("%*d", 2, 1), " 1"); @@ -591,7 +591,7 @@ Deno.test("testThorough", function (): void { assertEquals( is, should, - `failed case[${i}] : is >${is}< should >${should}<` + `failed case[${i}] : is >${is}< should >${should}<`, ); }); }); @@ -600,7 +600,7 @@ Deno.test("testWeirdos", function (): void { assertEquals(S("%.d", 9), "9"); assertEquals( S("dec[%d]=%d hex[%[1]d]=%#x oct[%[1]d]=%#o %s", 1, 255, "Third"), - "dec[1]=255 hex[1]=0xff oct[1]=0377 Third" + "dec[1]=255 hex[1]=0xff oct[1]=0377 Third", ); }); @@ -610,7 +610,7 @@ Deno.test("formatV", function (): void { assertEquals(S("%#v", a), `{ a: { a: { a: { a: ${cyan("[Object]")} } } } }`); assertEquals( S("%#.8v", a), - "{ a: { a: { a: { a: { a: { a: { a: {} } } } } } } }" + "{ a: { a: { a: { a: { a: { a: { a: {} } } } } } } }", ); assertEquals(S("%#.1v", a), `{ a: ${cyan("[Object]")} }`); }); @@ -625,9 +625,9 @@ Deno.test("flagLessThan", function (): void { const aArray = [a, a, a]; assertEquals( S("%<#.1v", aArray), - `[ { a: ${cyan("[Object]")} }, { a: ${cyan("[Object]")} }, { a: ${cyan( - "[Object]" - )} } ]` + `[ { a: ${cyan("[Object]")} }, { a: ${cyan("[Object]")} }, { a: ${ + cyan("[Object]") + } } ]`, ); const fArray = [1.2345, 0.98765, 123456789.5678]; assertEquals(S("%<.2f", fArray), "[ 1.23, 0.99, 123456789.57 ]"); @@ -649,7 +649,7 @@ Deno.test("testErrors", function (): void { assertEquals(S("%.*f", "a", 1.1), "%!(BAD PREC 'a')"); assertEquals( S("%.[2]*f", 1.23, "p"), - `%!(BAD PREC 'p')%!(EXTRA '${yellow("1.23")}')` + `%!(BAD PREC 'p')%!(EXTRA '${yellow("1.23")}')`, ); assertEquals(S("%.[2]*[1]f Yippie!", 1.23, "p"), "%!(BAD PREC 'p') Yippie!"); @@ -663,7 +663,7 @@ Deno.test("testErrors", function (): void { assertEquals(S("%[hallo]s %d %d %d", 1, 2, 3, 4), "%!(BAD INDEX) 2 3 4"); assertEquals( S("%[5]s", 1, 2, 3, 4), - `%!(BAD INDEX)%!(EXTRA '${yellow("2")}' '${yellow("3")}' '${yellow("4")}')` + `%!(BAD INDEX)%!(EXTRA '${yellow("2")}' '${yellow("3")}' '${yellow("4")}')`, ); assertEquals(S("%[5]f"), "%!(BAD INDEX)"); assertEquals(S("%.[5]f"), "%!(BAD INDEX)"); |