forked from RubixDev/typst-name-it
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlib.typ
More file actions
82 lines (66 loc) · 1.99 KB
/
Copy pathlib.typ
File metadata and controls
82 lines (66 loc) · 1.99 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#import "languages/en.typ"
#import "languages/id.typ"
#import "languages/fr.typ"
#import "languages/my.typ"
#let languages = (
"en": en.lang-config,
"fr": fr.lang-config,
"id": id.lang-config,
"my": my.lang-config,
)
#let name-it(num, lang: "en", ..options) = {
// assert types
assert(
type(num) == int or (type(num) == str and num.contains(regex("^[-−]?\d+$"))),
message: "Argument must be a number or a valid string of a number"
)
assert(
lang in languages,
message: "Language '" + lang + "' is not supported. Create a issue or help contribute in `https://github.com/IrregularPersona/name-all-the-numbers`"
)
let config = languages.at(lang)
// Convert to string and handle negative
let num-str = str(num)
let is-negative = false
if num-str.len() > 0 {
let first-char = num-str.clusters().at(0)
if first-char in ("-", "−") {
is-negative = true
num-str = num-str.clusters().slice(1).join()
}
}
// Remove leading zeros manually (to handle very large numbers)
num-str = num-str.trim("0", at: start, repeat: true)
// Handle case where all digits were zeros
if num-str == "" {
return if is-negative {
config.format-negative(config.zero-name)
} else {
config.zero-name
}
}
// Pad to multiple of 3
let remainder = calc.rem(num-str.len(), 3)
if remainder != 0 {
num-str = (3 - remainder) * "0" + num-str
}
let group-count = int(num-str.len() / 3)
let parts = ()
for group-idx in range(group-count) {
let group-digits = num-str.slice(group-idx * 3, count: 3)
let scale-idx = group-count - 1 - group-idx
let group-text = (config.convert-group)(group-digits, scale-idx, options)
if group-text != none and group-text.trim() != "" {
parts.push((
text: group-text,
scale: scale-idx,
digits: group-digits,
))
}
}
let result = (config.join-parts)(parts, options)
if is-negative {
result = (config.format-negative)(result)
}
return result
}