-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
75 lines (58 loc) · 1.91 KB
/
index.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
export type Optional<T> = { [P in keyof T]?: T[P] }
export interface ConfigInstance {
abbrev: string[]
decimal: number
}
export type OptionInstances = Optional<ConfigInstance>
export class Instance {
public config: ConfigInstance = {
abbrev: ['k', 'm', 'b', 't'],
decimal: 2
}
constructor (options?: OptionInstances) {
if (options) {
// Merge options
this.config = {
...this.config,
...options
}
}
}
// source: http://stackoverflow.com/a/2686098/1074592
public simplify (num = 0) {
let numberVar = num
// 2 decimal places => 100, 3 => 1000, etc
let decPlaces = this.config.decimal
decPlaces = decPlaces != null ? decPlaces : 2
decPlaces = Math.pow(10, decPlaces)
// Enumerate number abbreviations
const abbrev = this.config.abbrev
// Go through the array backwards, so we do the largest first
for (let i = abbrev.length - 1; i >= 0; i--) {
// Convert array index to "1000", "1000000", etc
const size = Math.pow(10, (i + 1) * 3)
// If the number is bigger or equal do the abbreviation
if (size <= numberVar) {
// Here, we multiply by decPlaces, round, and then divide by decPlaces.
// This gives us nice rounding to a particular decimal place.
numberVar = Math.round((numberVar * decPlaces) / size) / decPlaces
// Handle special case where we round up to the next abbreviation
if (numberVar === 1000 && i < abbrev.length - 1) {
numberVar = 1
i++
}
// Add the letter for the abbreviation
(numberVar as any) += abbrev[i]
// We are done... stop
break
}
}
return String(numberVar)
}
}
function SimplifyNumber (num: number, config?: OptionInstances) {
const instance = new Instance(config)
const simplifiedNumber = instance.simplify(num)
return simplifiedNumber
}
export default SimplifyNumber