forked from EvanHahn/HumanizeDuration.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
humanize-duration.js
215 lines (187 loc) · 6.4 KB
/
humanize-duration.js
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// HumanizeDuration.js - https://git.io/j0HgmQ
;(function () {
var languages = {
ar: {
y: function (c) { return c === 1 ? 'سنة' : 'سنوات' },
mo: function (c) { return c === 1 ? 'شهر' : 'أشهر' },
w: function (c) { return c === 1 ? 'أسبوع' : 'أسابيع' },
d: function (c) { return c === 1 ? 'يوم' : 'أيام' },
h: function (c) { return c === 1 ? 'ساعة' : 'ساعات' },
m: function (c) { return c === 1 ? 'دقيقة' : 'دقائق' },
s: function (c) { return c === 1 ? 'ثانية' : 'ثواني' },
ms: function (c) { return c === 1 ? 'جزء من الثانية' : 'أجزاء من الثانية' },
decimal: ','
},
en: {
y: function (c) { return 'year' + (c === 1 ? '' : 's') },
mo: function (c) { return 'month' + (c === 1 ? '' : 's') },
w: function (c) { return 'week' + (c === 1 ? '' : 's') },
d: function (c) { return 'day' + (c === 1 ? '' : 's') },
h: function (c) { return 'hour' + (c === 1 ? '' : 's') },
m: function (c) { return 'minute' + (c === 1 ? '' : 's') },
s: function (c) { return 'second' + (c === 1 ? '' : 's') },
ms: function (c) { return 'millisecond' + (c === 1 ? '' : 's') },
decimal: '.'
},
ur: {
y: 'سال',
mo: function (c) { return c === 1 ? 'مہینہ' : 'مہینے' },
w: function (c) { return c === 1 ? 'ہفتہ' : 'ہفتے' },
d: 'دن',
h: function (c) { return c === 1 ? 'گھنٹہ' : 'گھنٹے' },
m: 'منٹ',
s: 'سیکنڈ',
ms: 'ملی سیکنڈ',
decimal: '.'
}
}
// You can create a humanizer, which returns a function with default
// parameters.
function humanizer (passedOptions) {
var result = function humanizer (ms, humanizerOptions) {
var options = extend({}, result, humanizerOptions || {})
return doHumanization(ms, options)
}
return extend(result, {
language: 'en',
delimiter: ', ',
spacer: ' ',
conjunction: '',
serialComma: true,
units: ['y', 'mo', 'w', 'd', 'h', 'm', 's'],
languages: {},
round: false,
unitMeasures: {
y: 31557600000,
mo: 2629800000,
w: 604800000,
d: 86400000,
h: 3600000,
m: 60000,
s: 1000,
ms: 1
}
}, passedOptions)
}
// The main function is just a wrapper around a default humanizer.
var humanizeDuration = humanizer({})
// doHumanization does the bulk of the work.
function doHumanization (ms, options) {
var i, len, piece
// Make sure we have a positive number.
// Has the nice sideffect of turning Number objects into primitives.
ms = Math.abs(ms)
var dictionary = options.languages[options.language] || languages[options.language]
if (!dictionary) {
throw new Error('No language ' + dictionary + '.')
}
var pieces = []
// Start at the top and keep removing units, bit by bit.
var unitName, unitMS, unitCount
for (i = 0, len = options.units.length; i < len; i++) {
unitName = options.units[i]
unitMS = options.unitMeasures[unitName]
// What's the number of full units we can fit?
if (i + 1 === len) {
unitCount = ms / unitMS
} else {
unitCount = Math.floor(ms / unitMS)
}
// Add the string.
pieces.push({
unitCount: unitCount,
unitName: unitName
})
// Remove what we just figured out.
ms -= unitCount * unitMS
}
var firstOccupiedUnitIndex = 0
for (i = 0; i < pieces.length; i++) {
if (pieces[i].unitCount) {
firstOccupiedUnitIndex = i
break
}
}
if (options.round) {
var ratioToLargerUnit, previousPiece
for (i = pieces.length - 1; i >= 0; i--) {
piece = pieces[i]
piece.unitCount = Math.round(piece.unitCount)
if (i === 0) { break }
previousPiece = pieces[i - 1]
ratioToLargerUnit = options.unitMeasures[previousPiece.unitName] / options.unitMeasures[piece.unitName]
if ((piece.unitCount % ratioToLargerUnit) === 0 || (options.largest && ((options.largest - 1) < (i - firstOccupiedUnitIndex)))) {
previousPiece.unitCount += piece.unitCount / ratioToLargerUnit
piece.unitCount = 0
}
}
}
var result = []
for (i = 0, pieces.length; i < len; i++) {
piece = pieces[i]
if (piece.unitCount) {
result.push(render(piece.unitCount, piece.unitName, dictionary, options))
}
if (result.length === options.largest) { break }
}
if (result.length) {
if (!options.conjunction || result.length === 1) {
return result.join(options.delimiter)
} else if (result.length === 2) {
return result.join(options.conjunction)
} else if (result.length > 2) {
return result.slice(0, -1).join(options.delimiter) + (options.serialComma ? ',' : '') + options.conjunction + result.slice(-1)
}
} else {
return render(0, options.units[options.units.length - 1], dictionary, options)
}
}
function render (count, type, dictionary, options) {
var decimal
if (options.decimal === void 0) {
decimal = dictionary.decimal
} else {
decimal = options.decimal
}
var countStr = count.toString().replace('.', decimal)
var dictionaryValue = dictionary[type]
var word
if (typeof dictionaryValue === 'function') {
word = dictionaryValue(count)
} else {
word = dictionaryValue
}
return countStr + options.spacer + word
}
function extend (destination) {
var source
for (var i = 1; i < arguments.length; i++) {
source = arguments[i]
for (var prop in source) {
if (source.hasOwnProperty(prop)) {
destination[prop] = source[prop]
}
}
}
return destination
}
humanizeDuration.getSupportedLanguages = function getSupportedLanguages () {
var result = []
for (var language in languages) {
if (languages.hasOwnProperty(language)) {
result.push(language)
}
}
return result
}
humanizeDuration.humanizer = humanizer
if (typeof define === 'function' && define.amd) {
define(function () {
return humanizeDuration
})
} else if (typeof module !== 'undefined' && module.exports) {
module.exports = humanizeDuration
} else {
this.humanizeDuration = humanizeDuration
}
})(); // eslint-disable-line semi