-
-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(strings): add int/intLocale, vector formatters
- add optional arg for float() for special NaN/Inf handling
- Loading branch information
1 parent
4302f58
commit ac55fe0
Showing
4 changed files
with
76 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { memoizeJ } from "@thi.ng/memoize"; | ||
import type { Stringer } from "./api"; | ||
|
||
export const int: Stringer<number> = (x) => String(Math.trunc(x)); | ||
|
||
export const intLocale: ( | ||
locale?: string | ||
) => Stringer<number> = memoizeJ((locale) => (x) => | ||
Math.trunc(x).toLocaleString(locale) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { isNumber } from "@thi.ng/checks"; | ||
import { memoizeJ } from "@thi.ng/memoize"; | ||
import type { Stringer } from "./api"; | ||
import { float } from "./float"; | ||
|
||
/** | ||
* Higher order formatter for n-D vectors, with each element formatted using | ||
* `prec` and using optional delimiter and pre/postfixes. | ||
* | ||
* @size - vector size (optimized for size 1-4) | ||
* @prec - precision (see {@link float}) or existing number formatter | ||
* @delim - delimiter (default: `,`) | ||
* @pre - prefix (default: `[`) | ||
* @post - prefix (default: `]`) | ||
*/ | ||
export const vector: ( | ||
size: number, | ||
prec?: number | Stringer<number>, | ||
delim?: string, | ||
pre?: string, | ||
post?: string | ||
) => Stringer<ArrayLike<number>> = memoizeJ( | ||
( | ||
size: number, | ||
prec: number | Stringer<number> = 3, | ||
d = ",", | ||
pre = "[", | ||
post = "]" | ||
) => { | ||
const f = isNumber(prec) ? float(prec) : prec; | ||
switch (size) { | ||
case 1: | ||
return (v: ArrayLike<number>) => `${pre}${f(v[0])}${post}`; | ||
case 2: | ||
return (v: ArrayLike<number>) => | ||
`${pre}${f(v[0])}${d}${f(v[1])}${post}`; | ||
case 3: | ||
return (v: ArrayLike<number>) => | ||
`${pre}${f(v[0])}${d}${f(v[1])}${d}${f(v[2])}${post}`; | ||
case 4: | ||
return (v: ArrayLike<number>) => | ||
`${pre}${f(v[0])}${d}${f(v[1])}${d}${f(v[2])}${d}${f( | ||
v[3] | ||
)}${post}`; | ||
default: | ||
return (v: ArrayLike<number>) => { | ||
const res = []; | ||
for (let i = 0; i < v.length; i++) { | ||
res.push(f(v[i])); | ||
} | ||
return `${pre}${res.join(d)}${post}`; | ||
}; | ||
} | ||
} | ||
); |