-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathstring.ts
151 lines (137 loc) · 5.3 KB
/
string.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import BigNumber from 'bignumber.js'
import { hexToBytes } from '@nervosnetwork/ckb-sdk-utils'
import { assert } from './error'
export function createTextMeasurer(element: HTMLElement): CanvasText['measureText'] {
const style = window.getComputedStyle(element)
const ctx = document.createElement('canvas').getContext('2d')
assert(ctx)
ctx.font = style.font ? style.font : `${style.fontSize} ${style.fontFamily}`
return text => ctx.measureText(text)
}
export function createTextWidthMeasurer(element: HTMLElement): (text: string) => number {
const measureText = createTextMeasurer(element)
const charLMetrics = measureText('l')
const charMMetrics = measureText('m')
const isMonospace = charLMetrics.width === charMMetrics.width
if (isMonospace) {
return text => charLMetrics.width * text.length
}
return text => {
const measure = measureText(text)
return measure.width - measure.actualBoundingBoxLeft
}
}
export const startEndEllipsis = (value: string, endLength = 8, startLength = 16) => {
if (value === undefined || value === null) return ''
if (endLength < 0 || startLength < 0) return value
if (value.length <= startLength + endLength) return value
return `${value.substr(0, startLength)}...${value.substr(value.length - endLength, endLength)}`
}
export const hexToUtf8 = (value: string = '') => {
try {
return new TextDecoder().decode(hexToBytes(value))
} catch (error) {
return value
}
}
export const addPrefixForHash = (value: string) => {
if (value && value.length >= 32 && /\b[A-Fa-f0-9]+\b/.test(value)) {
return `0x${value}`
}
return value
}
export const containSpecialChar = (value: string) => {
const regEn = /[`~!@#$%^&*()_+<>?:"{}.;'[\]]/im
const regCn = /[·!#¥(——):;“”‘、,|《。》?、【】[\]]/im
return regEn.test(value) || regCn.test(value)
}
export const handleBigNumber = (value: BigNumber | string | number | null | undefined, decimal?: number) => {
const bigValue = typeof value === 'string' || typeof value === 'number' ? new BigNumber(value) : value
if (!bigValue || bigValue.isNaN() || bigValue.isZero()) return '0'
const kv = bigValue.dividedBy(1000)
const mv = kv.dividedBy(1000)
const gv = mv.dividedBy(1000)
const tv = gv.dividedBy(1000)
const pv = tv.dividedBy(1000)
const ev = pv.dividedBy(1000)
const zv = ev.dividedBy(1000)
const yv = zv.dividedBy(1000)
if (yv.isGreaterThanOrEqualTo(1)) {
return `${decimal ? yv.toFixed(decimal) : yv.toFixed()}Y`
}
if (zv.isGreaterThanOrEqualTo(1)) {
return `${decimal ? zv.toFixed(decimal) : zv.toFixed()}Z`
}
if (ev.isGreaterThanOrEqualTo(1)) {
return `${decimal ? ev.toFixed(decimal) : ev.toFixed()}E`
}
if (pv.isGreaterThanOrEqualTo(1)) {
return `${decimal ? pv.toFixed(decimal) : pv.toFixed()}P`
}
if (tv.isGreaterThanOrEqualTo(1)) {
return `${decimal ? tv.toFixed(decimal) : tv.toFixed()}T`
}
if (gv.isGreaterThanOrEqualTo(1)) {
return `${decimal ? gv.toFixed(decimal) : gv.toFixed()}G`
}
if (mv.isGreaterThanOrEqualTo(1)) {
return `${decimal ? mv.toFixed(decimal) : mv.toFixed()}M`
}
if (kv.isGreaterThanOrEqualTo(1)) {
return `${decimal ? kv.toFixed(decimal) : kv.toFixed()}K`
}
return `${decimal ? bigValue.toFixed(decimal) : bigValue.toFixed()}`
}
export const parseFloorDecimal = (value: BigNumber | string | number, decimal?: number) => {
const bigValue = typeof value === 'string' || typeof value === 'number' ? new BigNumber(value) : value
if (bigValue.isNaN() || bigValue.isZero()) return '0'
if (decimal) {
if (bigValue.isNegative()) {
return 0 - Math.floor(bigValue.abs().toNumber() * 10 ** decimal) / 10 ** decimal
}
return Math.floor(bigValue.abs().toNumber() * 10 ** decimal) / 10 ** decimal
}
if (bigValue.isNegative()) {
return 0 - Math.floor(bigValue.abs().toNumber())
}
return Math.floor(bigValue.abs().toNumber())
}
export const handleBigNumberFloor = (value: BigNumber | string | number, decimal?: number) => {
const bigValue = typeof value === 'string' || typeof value === 'number' ? new BigNumber(value) : value
if (bigValue.isNaN() || bigValue.isZero()) return '0'
const kv = bigValue.dividedBy(1000)
const mv = kv.dividedBy(1000)
const gv = mv.dividedBy(1000)
const tv = gv.dividedBy(1000)
const pv = tv.dividedBy(1000)
const ev = pv.dividedBy(1000)
const zv = ev.dividedBy(1000)
const yv = zv.dividedBy(1000)
if (yv.abs().isGreaterThanOrEqualTo(1)) {
return `${parseFloorDecimal(yv, decimal)}Y`
}
if (zv.abs().isGreaterThanOrEqualTo(1)) {
return `${parseFloorDecimal(zv, decimal)}Z`
}
if (ev.abs().isGreaterThanOrEqualTo(1)) {
return `${parseFloorDecimal(ev, decimal)}E`
}
if (pv.abs().isGreaterThanOrEqualTo(1)) {
return `${parseFloorDecimal(pv, decimal)}P`
}
if (tv.abs().isGreaterThanOrEqualTo(1)) {
return `${parseFloorDecimal(tv, decimal)}T`
}
if (gv.abs().isGreaterThanOrEqualTo(1)) {
return `${parseFloorDecimal(gv, decimal)}G`
}
if (mv.abs().isGreaterThanOrEqualTo(1)) {
return `${parseFloorDecimal(mv, decimal)}M`
}
if (kv.abs().isGreaterThanOrEqualTo(1)) {
return `${parseFloorDecimal(kv, decimal)}K`
}
return `${parseFloorDecimal(bigValue, decimal)}`
}
export const sliceNftName = (name?: string, maxLength = 32) =>
name && name.length > maxLength ? `${name.slice(0, maxLength)}...` : name