forked from microsoft/devicescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrformat.ts
More file actions
56 lines (47 loc) · 1.28 KB
/
strformat.ts
File metadata and controls
56 lines (47 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
function ord(c: string) {
if (c) return c.charCodeAt(0)
else return 0
}
function numvalue(c: number) {
if (ord("0") <= c && c <= ord("9")) return c - ord("0")
c |= 0x20
if (ord("a") <= c && c <= ord("z")) return c - ord("a") + 10
return -1
}
export function strformat(fmt: string, args: ArrayLike<number>) {
const fmtlen = fmt.length
let fp = 0
let dst = ""
while (fp < fmtlen) {
let c = fmt[fp++]
if (c != "{" || fp >= fmtlen) {
dst += c
// if we see "}}" we treat it as a single "}"
if (c == "}" && fp < fmtlen && fmt[fp] == "}") fp++
continue
}
c = fmt[fp++]
if (c == "{") {
dst += c
continue
}
let pos = numvalue(ord(c))
if (pos < 0) {
dst += "!"
continue
}
let nextp = fp
while (nextp < fmtlen && fmt[nextp] != "}") nextp++
let precision = -1
if (fp < nextp) precision = numvalue(ord(fmt[fp++]))
fp = nextp + 1
if (pos >= args.length) {
dst += "?"
continue
}
if (precision < 0) precision = 6
// TODO check if +1 needed?
dst += args[pos].toPrecision(precision + 1)
}
return dst
}