From 118f97f1fca27671c53d184484a7b435e6eedf88 Mon Sep 17 00:00:00 2001 From: Karsten Schmidt Date: Fri, 8 Jan 2021 00:45:36 +0000 Subject: [PATCH] feat(strings): update padLeft/Right() args - add optional length arg to explicitly specify string length (e.g. to exclude ANSI control seq chars) --- packages/strings/src/pad-left.ts | 12 +++++++----- packages/strings/src/pad-right.ts | 12 +++++++----- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/packages/strings/src/pad-left.ts b/packages/strings/src/pad-left.ts index d0d0ff2417..ce21563659 100644 --- a/packages/strings/src/pad-left.ts +++ b/packages/strings/src/pad-left.ts @@ -9,16 +9,18 @@ import { repeat } from "./repeat"; export const padLeft: ( n: number, ch?: string | number -) => Stringer = memoizeJ< +) => (x: any, length?: number) => string = memoizeJ< number, string | number | undefined, Stringer >((n, ch = " ") => { const buf = repeat(String(ch), n); - return (x: any) => - x != null - ? ((x = x.toString()), x.length < n ? buf.substr(x.length) + x : x) - : buf; + return (x: any, len?: number) => { + if (x == null) return buf; + x = x.toString(); + len = len !== undefined ? len : x.length; + return len! < n ? buf.substr(len!) + x : x; + }; }); /** diff --git a/packages/strings/src/pad-right.ts b/packages/strings/src/pad-right.ts index 4dac93af60..cbf65e18d6 100644 --- a/packages/strings/src/pad-right.ts +++ b/packages/strings/src/pad-right.ts @@ -9,14 +9,16 @@ import { repeat } from "./repeat"; export const padRight: ( n: number, ch?: string | number -) => Stringer = memoizeJ< +) => (x: any, length?: number) => string = memoizeJ< number, string | number | undefined, Stringer >((n, ch = " ") => { const buf = repeat(String(ch), n); - return (x: any) => - x != null - ? ((x = x.toString()), x.length < n ? x + buf.substr(x.length) : x) - : buf; + return (x, len?: number) => { + if (x == null) return buf; + x = x.toString(); + len = len !== undefined ? len : x.length; + return len! < n ? x + buf.substr(len!) : x; + }; });