summaryrefslogtreecommitdiff
path: root/app/client/util/format.js
blob: c8e8f3f7fcbfb6e3449a74e6e836729c14b5c218 (plain)
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
export function timeInSeconds(n){
  return (n / 10).toFixed(1) + ' s.'
}
export function gerund(s){
  return s.replace(/e?$/, 'ing')
}
export function commatize (n, radix) {
  radix = radix || 1024
  var nums = [], i, counter = 0, r = Math.floor
  if (n > radix) {
    n /= radix
    nums.unshift(r((n * 10) % 10))
    nums.unshift(".")
  }
  do {
    i = n % 10
    n = r(n / 10)
    if (n && ! (++counter % 3))
      { i = ' ' + r(i) }
    nums.unshift(r(i))
  }
  while (n)
  return nums.join("")
}
export function carbon_date (date, no_bold) {
  var span = (+new Date() - new Date(date)) / 1000, color
  if (! no_bold && span < 86400) // modified today
    { color = "new" }
  else if (span < 604800) // modifed this week
    { color = "recent" }
  else if (span < 1209600) // modifed 2 weeks ago
    { color = "med" }
  else if (span < 3024000) // modifed 5 weeks ago
    { color = "old" }
  else if (span < 12315200) // modifed 6 months ago
    { color = "older" }
  else
    { color = "quiet" }
  return color
}
export function hush_views (n, bias, no_bold) {
  var txt = commatize(n, 1000)
  bias = bias || 1
  n = n || 0
  if (n < 30) { return["quiet", n + " v."] }
  if (n < 200) { return ["quiet", txt + " v."] }
  else if (n < 500) { return ["quiet", txt + " v."] }
  else if (n < 1000) { return ["old", txt + " v."] }
  else if (n < 5000) { return ["med", txt + " kv."] }
  else if (no_bold || n < 10000) { return ["recent", txt + " kv."] }
  else { return ["new", txt + " kv."] }
}
export function hush_threads (n, bias, no_bold) {
  var txt = commatize(n, 1000)
  bias = bias || 1
  n = n || 0
  if (n < 10) { return["quiet", n + " t."] }
  else if (n < 25) { return ["old", txt + " t."] }
  else if (n < 50) { return ["med", txt + " t."] }
  else if (no_bold || n < 100) { return ["recent", txt + " t."] }
  else { return ["new", txt + " t."] }
}
export function hush_size (n, bias, no_bold) {
  var txt = commatize(Math.round(n / 1024))
  bias = 1 || bias
  n = n || 0
  if (! n) { return ['', ''] }
  if (n < 1000) {
    return ["quiet", n + " b."]
  }
  if (n < 1000000) {
    return ["quiet", txt + " kb."]
  }
  if (n < (20000000/bias)) {
    return ["quiet", txt + " mb."]
  }
  if (n < (50000000/bias)) {
    return ["old", txt + " mb."]
  }
  if (n < (80000000/bias)) {
    return ["med", txt + " mb."]
  }
  if (no_bold || n < (170000000/bias)) {
    return ["recent", txt + " mb."]
  }
  return ["new", txt + " mb."]
}
export function hush_null (n, unit, no_bold) {
  var s = unit ? n + " " + unit + "." : n
  if (n < 3) {
    return ["quiet", s]
  }
  else if (n < 6) {
    return ["older", s]
  }
  else if (n < 10) {
    return ["old", s]
  }
  else if (n < 16) {
    return ["med", s]
  }
  else if (no_bold || n < 21) {
    return ["recent", s]
  }
  else {
    return ["new", s]
  }
}
export function get_age (t) {
  var age = Math.abs(+Date.now() - new Date(t))/1000
  var r = Math.floor
  var m
  if (age < 5) { return "now" }
  if (age < 60) { return r(age) + "s" }
  age /= 60
  if (age < 60) { return r(age) + "m" }
  m = r(age % 60)
  age /= 60
  if (m > 0 && age < 2) { return r(age) + "h" + m + "m" }
  if (age < 24) { return r(age) + "h" }
  age /= 24
  if (age < 7) { return r(age) + "d" }
  age /= 7
  if (age < 12) { return r(age) + "w" }
  age /= 4
  if (age < 12) { return r(age) + "m" }
  age /= 12
  return r(age) + "y"
}
export function courtesy_s (n, s) { return n == 1 ? "" : (s || "s") }