Skip to content

Commit

Permalink
feat(strings): add opt prefix arg for radix()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Aug 8, 2018
1 parent a5e2c28 commit 5864f2c
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions packages/strings/src/radix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ import { Stringer } from "./api";
import { repeat } from "./repeat";

/**
* Returns a `Stringer` which formats given numbers to `radix` and `len`.
* Returns a `Stringer` which formats given numbers to `radix`, `len`
* and with optional prefix (not included in `len`).
*
* @param radix
* @param len
* @param prefix
*/
export const radix: (radix: number, len: number) => Stringer<number> =
export const radix: (radix: number, len: number, prefix?: string) => Stringer<number> =
memoizeJ(
(radix: number, n: number) => {
(radix: number, n: number, prefix = "") => {
const buf = repeat("0", n);
return (x: any) => {
x = (x >>> 0).toString(radix);
return x.length < n ? buf.substr(x.length) + x : x;
return prefix + (x.length < n ? buf.substr(x.length) + x : x);
};
}
);
Expand Down

0 comments on commit 5864f2c

Please sign in to comment.