Skip to content

Commit

Permalink
feat(strings): update padLeft/Right() args
Browse files Browse the repository at this point in the history
- add optional length arg to explicitly specify string length
  (e.g. to exclude ANSI control seq chars)
  • Loading branch information
postspectacular committed Jan 8, 2021
1 parent bb62760 commit 118f97f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
12 changes: 7 additions & 5 deletions packages/strings/src/pad-left.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ import { repeat } from "./repeat";
export const padLeft: (
n: number,
ch?: string | number
) => Stringer<any> = memoizeJ<
) => (x: any, length?: number) => string = memoizeJ<
number,
string | number | undefined,
Stringer<any>
>((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;
};
});

/**
Expand Down
12 changes: 7 additions & 5 deletions packages/strings/src/pad-right.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import { repeat } from "./repeat";
export const padRight: (
n: number,
ch?: string | number
) => Stringer<any> = memoizeJ<
) => (x: any, length?: number) => string = memoizeJ<
number,
string | number | undefined,
Stringer<any>
>((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;
};
});

0 comments on commit 118f97f

Please sign in to comment.