forked from lokalise/i18n-ally
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
109 lines (93 loc) · 2.98 KB
/
utils.ts
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { KeyStyle } from '../core/types'
import { ROOT_KEY } from './flat'
import { Node, LocaleTree, LocaleNode, LocaleRecord, Config } from '~/core'
export function caseInsensitiveMatch(a: string, b: string) {
return a.toUpperCase() === b.toUpperCase()
}
export function getKeyname(keypath: string) {
const keys = keypath.split(/\./g)
if (!keys.length)
return ''
return keys[keys.length - 1]
}
export function notEmpty<T>(value: T | null | undefined): value is T {
return value !== null && value !== undefined
}
export function escapeMarkdown(text: string) {
return text
.replace(/\|/g, '\\|')
.replace(/\[/g, '\\[')
.replace(/\]/g, '\\]')
.replace(/\(/g, '\\(')
.replace(/\)/g, '\\)')
}
export function resolveFlattenRootKeypath(keypath: string) {
if (keypath.endsWith(ROOT_KEY))
keypath = keypath.slice(0, -ROOT_KEY.length)
if (keypath.endsWith('.'))
keypath = keypath.slice(0, -1)
return keypath
}
export function resolveFlattenRoot (node: undefined): undefined
export function resolveFlattenRoot (node: LocaleRecord): LocaleRecord
export function resolveFlattenRoot (node: LocaleTree | LocaleNode): LocaleTree | LocaleNode
export function resolveFlattenRoot (node?: LocaleTree | LocaleNode): LocaleTree | LocaleNode | undefined
export function resolveFlattenRoot(node?: Node) {
if (node?.type === 'tree' && node.getChild(ROOT_KEY)?.type === 'node')
return node.getChild(ROOT_KEY)
return node
}
export function set(obj: any, key: string, value: any, override = true) {
if (Array.isArray(obj)) {
const num = parseInt(key)
if (!isNaN(num)) {
if (override || obj[num] == null)
obj[num] = value
return obj[num]
}
}
if (override || obj[key] == null)
obj[key] = value
return obj[key]
}
export function setNested(obj: any, keys: string[], value: any) {
if (!keys.length)
return
const key = keys[0]
if (keys.length === 1)
set(obj, key, value)
else
setNested(set(obj, key, {}, false), keys.slice(1), value)
}
export function applyPendingToObject(obj: any, keypath: string, value: any, keyStyle?: KeyStyle) {
if (!Config.disablePathParsing)
keypath = resolveFlattenRootKeypath(keypath)
if (keyStyle === 'flat')
obj[keypath] = value
else
setNested(obj, keypath.split(/\./g), value)
return obj
}
// abbreviateNumber source https://gist.github.com/tobyjsullivan/96d37ca0216adee20fa95fe1c3eb56ac
export function abbreviateNumber(value: number): string {
let newValue = value
const suffixes = ['', 'K', 'M', 'B', 'T']
let suffixNum = 0
while (newValue >= 1000) {
newValue /= 1000
suffixNum++
}
let result = newValue.toPrecision(3)
result += suffixes[suffixNum]
return result
}
/**
* Get a locale aware comparison function
*/
export function getLocaleCompare(
sortLocaleSetting: string | undefined,
fileLocale: string
): (x: string, y: string) => number {
const sortLocale = sortLocaleSetting ? sortLocaleSetting : fileLocale;
return new Intl.Collator(sortLocale).compare;
}