ms2text 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +2 -0
- package/index.d.ts +235 -0
- package/index.js +588 -0
- package/package.json +17 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 JustDeveloper <https://justdeveloper.is-a.dev/>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/index.d.ts
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
/*
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Copyright (c) 2025 JustDeveloper <https://justdeveloper.is-a.dev/>
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
in the Software without restriction, including without limitation the rights
|
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
furnished to do so, subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
SOFTWARE.
|
|
24
|
+
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/*
|
|
28
|
+
|
|
29
|
+
___ __ __
|
|
30
|
+
/'___`\ /\ \__ /\ \__
|
|
31
|
+
___ ___ ____/\_\ /\ \\ \ ,_\ __ __ _\ \ ,_\
|
|
32
|
+
/' __` __`\ /',__\/_/// /__\ \ \/ /'__`\/\ \/'\\ \ \/
|
|
33
|
+
/\ \/\ \/\ \/\__, `\ // /_\ \\ \ \_/\ __/\/> </ \ \ \_
|
|
34
|
+
\ \_\ \_\ \_\/\____//\______/ \ \__\ \____\/\_/\_\ \ \__\
|
|
35
|
+
\/_/\/_/\/_/\/___/ \/_____/ \/__/\/____/\//\/_/ \/__/
|
|
36
|
+
|
|
37
|
+
Convert time in milliseconds to a human-readable string.
|
|
38
|
+
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
declare module 'ms2text' {
|
|
42
|
+
interface Rules {
|
|
43
|
+
comma: string;
|
|
44
|
+
and: string;
|
|
45
|
+
spaces: boolean;
|
|
46
|
+
useOneWordAtStartIfOne: boolean;
|
|
47
|
+
useOneWordIfOne: boolean;
|
|
48
|
+
wordThenNumber: boolean;
|
|
49
|
+
reverse: boolean;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
interface WordKeys {
|
|
53
|
+
any?: string;
|
|
54
|
+
oneWordPrefix?: string;
|
|
55
|
+
[key: string]: string | undefined;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
interface Words {
|
|
59
|
+
years: WordKeys;
|
|
60
|
+
months: WordKeys;
|
|
61
|
+
weeks: WordKeys;
|
|
62
|
+
days: WordKeys;
|
|
63
|
+
hours: WordKeys;
|
|
64
|
+
minutes: WordKeys;
|
|
65
|
+
seconds: WordKeys;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
interface Language {
|
|
69
|
+
rules: Rules;
|
|
70
|
+
words: Words;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const exports: {
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* ---
|
|
77
|
+
*
|
|
78
|
+
* Converts time in milliseconds to a human-readable string.
|
|
79
|
+
*
|
|
80
|
+
* `time` - Time in milliseconds (`number`)
|
|
81
|
+
*
|
|
82
|
+
* ---
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* ms2text(1234567) // '20 minutes and 35 seconds'
|
|
86
|
+
*
|
|
87
|
+
* @param time Time in milliseconds
|
|
88
|
+
* @returns Human-readable time string
|
|
89
|
+
* @since 1.0.0
|
|
90
|
+
*/
|
|
91
|
+
ms2text(time: number): string;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* ---
|
|
95
|
+
*
|
|
96
|
+
* Converts time in milliseconds to a human-readable string.
|
|
97
|
+
*
|
|
98
|
+
* `from` - Start date in milliseconds (`number`)
|
|
99
|
+
*
|
|
100
|
+
* `to` - End date in milliseconds (`number`)
|
|
101
|
+
*
|
|
102
|
+
* **Note: start/end date means the number of milliseconds that have elapsed since the Unix Epoch (January 1, 1970, 00:00:00 UTC)**
|
|
103
|
+
*
|
|
104
|
+
* ---
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ms2text(1767123193174, 1767124427741) // '20 minutes and 35 seconds'
|
|
108
|
+
*
|
|
109
|
+
* @param from Start date in milliseconds (Unix timestamp)
|
|
110
|
+
* @param to End date in milliseconds (Unix timestamp)
|
|
111
|
+
* @returns Human-readable time string
|
|
112
|
+
* @since 1.0.0
|
|
113
|
+
*/
|
|
114
|
+
ms2text(from: number, to: number): string;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* ---
|
|
118
|
+
*
|
|
119
|
+
* Converts time in milliseconds to a human-readable string.
|
|
120
|
+
*
|
|
121
|
+
* `time` - Time in milliseconds (`number`)
|
|
122
|
+
*
|
|
123
|
+
* `daysInMonth` - The number of days in a month (`number`) (`30` by default) (min: `28`, max: `31`)
|
|
124
|
+
*
|
|
125
|
+
* `daysInYear` - The number of days in a year (`number`) (`365` by default) (min: `360`)
|
|
126
|
+
*
|
|
127
|
+
* ---
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ms2text(1234567, 30, 365) // '20 minutes and 35 seconds'
|
|
131
|
+
*
|
|
132
|
+
* @param time Time in milliseconds
|
|
133
|
+
* @param daysInMonth Number of days in a month (28-31, default: 30)
|
|
134
|
+
* @param daysInYear Number of days in a year (≥360, default: 365)
|
|
135
|
+
* @param unlock Unlock ranges for the number of days in a month and the number of days in a year
|
|
136
|
+
* @returns Human-readable time string
|
|
137
|
+
* @since 1.0.0
|
|
138
|
+
*/
|
|
139
|
+
ms2text(time: number, daysInMonth: number, daysInYear: number, unlock?: boolean): string;
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* ---
|
|
143
|
+
*
|
|
144
|
+
* Converts time in milliseconds to a human-readable string.
|
|
145
|
+
*
|
|
146
|
+
* `time` - Time in milliseconds (`number`)
|
|
147
|
+
*
|
|
148
|
+
* `language` - Language code (`'en'`/`'ru'`/...)
|
|
149
|
+
*
|
|
150
|
+
* ---
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
* ms2text(1234567, 'ru') // '20 минут и 35 секунд'
|
|
154
|
+
*
|
|
155
|
+
* @param time Time in milliseconds
|
|
156
|
+
* @param language Language code (e.g., 'en', 'ru')
|
|
157
|
+
* @returns Human-readable time string
|
|
158
|
+
* @since 1.0.0
|
|
159
|
+
*/
|
|
160
|
+
ms2text(time: number, language: string): string;
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* ---
|
|
164
|
+
*
|
|
165
|
+
* Converts time in milliseconds to a human-readable string.
|
|
166
|
+
*
|
|
167
|
+
* `from` - Start date in milliseconds (`number`)
|
|
168
|
+
*
|
|
169
|
+
* `to` - End date in milliseconds (`number`)
|
|
170
|
+
*
|
|
171
|
+
* `language` - Language code (`'en'`/`'ru'`/...)
|
|
172
|
+
*
|
|
173
|
+
* **Note: start/end date means the number of milliseconds that have elapsed since the Unix Epoch (January 1, 1970, 00:00:00 UTC)**
|
|
174
|
+
*
|
|
175
|
+
* ---
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* ms2text(1767123193174, 1767124427741, 'ru') // '20 минут и 35 секунд'
|
|
179
|
+
*
|
|
180
|
+
* @param from Start date in milliseconds (Unix timestamp)
|
|
181
|
+
* @param to End date in milliseconds (Unix timestamp)
|
|
182
|
+
* @param language Language code (e.g., 'en', 'ru')
|
|
183
|
+
* @returns Human-readable time string
|
|
184
|
+
* @since 1.0.0
|
|
185
|
+
*/
|
|
186
|
+
ms2text(from: number, to: number, language: string): string;
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* ---
|
|
190
|
+
*
|
|
191
|
+
* Converts time in milliseconds to a human-readable string.
|
|
192
|
+
*
|
|
193
|
+
* `time` - Time in milliseconds (`number`)
|
|
194
|
+
*
|
|
195
|
+
* `daysInMonth` - The number of days in a month (`number`) (`30` by default) (min: `28`, max: `31`)
|
|
196
|
+
*
|
|
197
|
+
* `daysInYear` - The number of days in a year (`number`) (`365` by default) (min: `360`)
|
|
198
|
+
*
|
|
199
|
+
* `language` - Language code (`'en'`/`'ru'`/...)
|
|
200
|
+
*
|
|
201
|
+
* ---
|
|
202
|
+
*
|
|
203
|
+
* @example
|
|
204
|
+
* ms2text(1234567, 30, 365, 'ru') // '20 минут и 35 секунд'
|
|
205
|
+
*
|
|
206
|
+
* @param time Time in milliseconds
|
|
207
|
+
* @param daysInMonth Number of days in a month (28-31, default: 30)
|
|
208
|
+
* @param daysInYear Number of days in a year (≥360, default: 365)
|
|
209
|
+
* @param language Language code (e.g., 'en', 'ru')
|
|
210
|
+
* @returns Human-readable time string
|
|
211
|
+
* @since 1.0.0
|
|
212
|
+
*/
|
|
213
|
+
ms2text(time: number, daysInMonth: number, daysInYear: number, language: string): string;
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Get language configuration
|
|
217
|
+
* @param lang Language code (e.g., 'en', 'ru')
|
|
218
|
+
* @returns Language configuration or undefined if not found
|
|
219
|
+
* @since 1.0.0
|
|
220
|
+
*/
|
|
221
|
+
getLanguage(lang: string): Language | undefined;
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Set or update language configuration
|
|
225
|
+
* @param lang Language code (e.g., 'en', 'ru')
|
|
226
|
+
* @param data Language configuration
|
|
227
|
+
* @returns Updated language configuration
|
|
228
|
+
* @since 1.0.0
|
|
229
|
+
*/
|
|
230
|
+
setLanguage(lang: string, data: Language): Language;
|
|
231
|
+
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
export = exports;
|
|
235
|
+
}
|
package/index.js
ADDED
|
@@ -0,0 +1,588 @@
|
|
|
1
|
+
/*
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Copyright (c) 2025 JustDeveloper <https://justdeveloper.is-a.dev/>
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
in the Software without restriction, including without limitation the rights
|
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
furnished to do so, subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
SOFTWARE.
|
|
24
|
+
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/*
|
|
28
|
+
|
|
29
|
+
___ __ __
|
|
30
|
+
/'___`\ /\ \__ /\ \__
|
|
31
|
+
___ ___ ____/\_\ /\ \\ \ ,_\ __ __ _\ \ ,_\
|
|
32
|
+
/' __` __`\ /',__\/_/// /__\ \ \/ /'__`\/\ \/'\\ \ \/
|
|
33
|
+
/\ \/\ \/\ \/\__, `\ // /_\ \\ \ \_/\ __/\/> </ \ \ \_
|
|
34
|
+
\ \_\ \_\ \_\/\____//\______/ \ \__\ \____\/\_/\_\ \ \__\
|
|
35
|
+
\/_/\/_/\/_/\/___/ \/_____/ \/__/\/____/\//\/_/ \/__/
|
|
36
|
+
|
|
37
|
+
Convert time in milliseconds to a human-readable string.
|
|
38
|
+
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
(function (root, factory) {
|
|
42
|
+
|
|
43
|
+
const version = '1.0.0';
|
|
44
|
+
|
|
45
|
+
if (typeof define === 'function' && define.amd) {
|
|
46
|
+
define([], factory);
|
|
47
|
+
} else if (typeof module === 'object' && module.exports) {
|
|
48
|
+
module.exports = {
|
|
49
|
+
...factory(),
|
|
50
|
+
version
|
|
51
|
+
};
|
|
52
|
+
} else {
|
|
53
|
+
const output = factory();
|
|
54
|
+
root.ms2text = output.ms2text;
|
|
55
|
+
Object.defineProperties(root.ms2text, {
|
|
56
|
+
getLanguage: {
|
|
57
|
+
value: output.getLanguage
|
|
58
|
+
},
|
|
59
|
+
setLanguage: {
|
|
60
|
+
value: output.setLanguage
|
|
61
|
+
},
|
|
62
|
+
version: {
|
|
63
|
+
value: version,
|
|
64
|
+
enumerable: false
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}(typeof self !== 'undefined' ? self : this, function () {
|
|
69
|
+
/**
|
|
70
|
+
* ```js
|
|
71
|
+
* Math.round(b * Math.round(a) / c) / b
|
|
72
|
+
* ```
|
|
73
|
+
* @param {number} a
|
|
74
|
+
* @param {number} b
|
|
75
|
+
* @param {number} c
|
|
76
|
+
* @returns {number}
|
|
77
|
+
*/
|
|
78
|
+
function calc(a, b, c) {
|
|
79
|
+
return Math.round(b * Math.round(a) / c) / b;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* @typedef {{comma: string, and: string, spaces: boolean, useOneWordAtStartIfOne: boolean, useOneWordIfOne: boolean, wordThenNumber: boolean}} Rules
|
|
84
|
+
*
|
|
85
|
+
* @typedef {Object} WordKeys
|
|
86
|
+
* @property {string?} any
|
|
87
|
+
* @property {string?} oneWordPrefix
|
|
88
|
+
*
|
|
89
|
+
* @typedef {WordKeys & Object<string, string>} Word
|
|
90
|
+
*
|
|
91
|
+
* @typedef {{years: Word, months: Word, weeks: Word, days: Word, hours: Word, minutes: Word, seconds: Word}} Words
|
|
92
|
+
*
|
|
93
|
+
* @typedef {undefined|{rules: Rules, words: Words}} Language
|
|
94
|
+
*/
|
|
95
|
+
const languages = {
|
|
96
|
+
en: {
|
|
97
|
+
words: {
|
|
98
|
+
years: {
|
|
99
|
+
11: 'years',
|
|
100
|
+
21: 'years',
|
|
101
|
+
31: 'years',
|
|
102
|
+
41: 'years',
|
|
103
|
+
51: 'years',
|
|
104
|
+
61: 'years',
|
|
105
|
+
71: 'years',
|
|
106
|
+
81: 'years',
|
|
107
|
+
91: 'years',
|
|
108
|
+
'01':'years',
|
|
109
|
+
1: 'year',
|
|
110
|
+
any: 'years',
|
|
111
|
+
oneWordPrefix: 'a ',
|
|
112
|
+
},
|
|
113
|
+
months: {
|
|
114
|
+
11: 'months',
|
|
115
|
+
21: 'months',
|
|
116
|
+
31: 'months',
|
|
117
|
+
41: 'months',
|
|
118
|
+
51: 'months',
|
|
119
|
+
61: 'months',
|
|
120
|
+
71: 'months',
|
|
121
|
+
81: 'months',
|
|
122
|
+
91: 'months',
|
|
123
|
+
'01':'months',
|
|
124
|
+
1: 'month',
|
|
125
|
+
any: 'months',
|
|
126
|
+
oneWordPrefix: 'a ',
|
|
127
|
+
},
|
|
128
|
+
weeks: {
|
|
129
|
+
11: 'weeks',
|
|
130
|
+
21: 'weeks',
|
|
131
|
+
31: 'weeks',
|
|
132
|
+
41: 'weeks',
|
|
133
|
+
51: 'weeks',
|
|
134
|
+
61: 'weeks',
|
|
135
|
+
71: 'weeks',
|
|
136
|
+
81: 'weeks',
|
|
137
|
+
91: 'weeks',
|
|
138
|
+
'01':'weeks',
|
|
139
|
+
1: 'week',
|
|
140
|
+
any: 'weeks',
|
|
141
|
+
oneWordPrefix: 'a ',
|
|
142
|
+
},
|
|
143
|
+
days: {
|
|
144
|
+
11: 'days',
|
|
145
|
+
21: 'days',
|
|
146
|
+
31: 'days',
|
|
147
|
+
41: 'days',
|
|
148
|
+
51: 'days',
|
|
149
|
+
61: 'days',
|
|
150
|
+
71: 'days',
|
|
151
|
+
81: 'days',
|
|
152
|
+
91: 'days',
|
|
153
|
+
'01':'days',
|
|
154
|
+
1: 'day',
|
|
155
|
+
any: 'days',
|
|
156
|
+
oneWordPrefix: 'a ',
|
|
157
|
+
},
|
|
158
|
+
hours: {
|
|
159
|
+
11: 'hours',
|
|
160
|
+
21: 'hours',
|
|
161
|
+
31: 'hours',
|
|
162
|
+
41: 'hours',
|
|
163
|
+
51: 'hours',
|
|
164
|
+
61: 'hours',
|
|
165
|
+
71: 'hours',
|
|
166
|
+
81: 'hours',
|
|
167
|
+
91: 'hours',
|
|
168
|
+
'01':'hours',
|
|
169
|
+
1: 'hour',
|
|
170
|
+
any: 'hours',
|
|
171
|
+
oneWordPrefix: 'an '
|
|
172
|
+
},
|
|
173
|
+
minutes: {
|
|
174
|
+
11: 'minutes',
|
|
175
|
+
21: 'minutes',
|
|
176
|
+
31: 'minutes',
|
|
177
|
+
41: 'minutes',
|
|
178
|
+
51: 'minutes',
|
|
179
|
+
61: 'minutes',
|
|
180
|
+
71: 'minutes',
|
|
181
|
+
81: 'minutes',
|
|
182
|
+
91: 'minutes',
|
|
183
|
+
'01':'minutes',
|
|
184
|
+
1: 'minute',
|
|
185
|
+
any: 'minutes',
|
|
186
|
+
oneWordPrefix: 'a '
|
|
187
|
+
},
|
|
188
|
+
seconds: {
|
|
189
|
+
11: 'seconds',
|
|
190
|
+
21: 'seconds',
|
|
191
|
+
31: 'seconds',
|
|
192
|
+
41: 'seconds',
|
|
193
|
+
51: 'seconds',
|
|
194
|
+
61: 'seconds',
|
|
195
|
+
71: 'seconds',
|
|
196
|
+
81: 'seconds',
|
|
197
|
+
91: 'seconds',
|
|
198
|
+
'01':'seconds',
|
|
199
|
+
1: 'second',
|
|
200
|
+
any: 'seconds',
|
|
201
|
+
oneWordPrefix: 'a '
|
|
202
|
+
}
|
|
203
|
+
},
|
|
204
|
+
rules: {
|
|
205
|
+
comma: ', ',
|
|
206
|
+
and: ' and ',
|
|
207
|
+
spaces: true,
|
|
208
|
+
useOneWordAtStartIfOne: true,
|
|
209
|
+
useOneWordIfOne: false,
|
|
210
|
+
wordThenNumber: false,
|
|
211
|
+
reverse: false,
|
|
212
|
+
}
|
|
213
|
+
},
|
|
214
|
+
ru: {
|
|
215
|
+
words: {
|
|
216
|
+
years: {
|
|
217
|
+
11: 'лет',
|
|
218
|
+
12: 'лет',
|
|
219
|
+
13: 'лет',
|
|
220
|
+
14: 'лет',
|
|
221
|
+
1: 'год',
|
|
222
|
+
2: 'года',
|
|
223
|
+
3: 'года',
|
|
224
|
+
4: 'года',
|
|
225
|
+
any: 'лет'
|
|
226
|
+
},
|
|
227
|
+
months: {
|
|
228
|
+
11: 'месяцев',
|
|
229
|
+
12: 'месяцев',
|
|
230
|
+
13: 'месяцев',
|
|
231
|
+
14: 'месяцев',
|
|
232
|
+
1: 'месяц',
|
|
233
|
+
2: 'месяца',
|
|
234
|
+
3: 'месяца',
|
|
235
|
+
4: 'месяца',
|
|
236
|
+
any: 'месяцев'
|
|
237
|
+
},
|
|
238
|
+
weeks: {
|
|
239
|
+
11: 'неделей',
|
|
240
|
+
12: 'неделей',
|
|
241
|
+
13: 'неделей',
|
|
242
|
+
14: 'неделей',
|
|
243
|
+
1: 'неделя',
|
|
244
|
+
2: 'недели',
|
|
245
|
+
3: 'недели',
|
|
246
|
+
4: 'недели',
|
|
247
|
+
any: 'неделей'
|
|
248
|
+
},
|
|
249
|
+
days: {
|
|
250
|
+
11: 'дней',
|
|
251
|
+
12: 'дней',
|
|
252
|
+
13: 'дней',
|
|
253
|
+
14: 'дней',
|
|
254
|
+
1: 'день',
|
|
255
|
+
2: 'дня',
|
|
256
|
+
3: 'дня',
|
|
257
|
+
4: 'дня',
|
|
258
|
+
any: 'дней'
|
|
259
|
+
},
|
|
260
|
+
hours: {
|
|
261
|
+
11: 'часов',
|
|
262
|
+
12: 'часов',
|
|
263
|
+
13: 'часов',
|
|
264
|
+
14: 'часов',
|
|
265
|
+
1: 'час',
|
|
266
|
+
2: 'часа',
|
|
267
|
+
3: 'часа',
|
|
268
|
+
4: 'часа',
|
|
269
|
+
any: 'часов'
|
|
270
|
+
},
|
|
271
|
+
minutes: {
|
|
272
|
+
11: 'минут',
|
|
273
|
+
12: 'минут',
|
|
274
|
+
13: 'минут',
|
|
275
|
+
14: 'минут',
|
|
276
|
+
1: 'минута',
|
|
277
|
+
2: 'минуты',
|
|
278
|
+
3: 'минуты',
|
|
279
|
+
4: 'минуты',
|
|
280
|
+
any: 'минут'
|
|
281
|
+
},
|
|
282
|
+
seconds: {
|
|
283
|
+
11: 'секунд',
|
|
284
|
+
12: 'секунд',
|
|
285
|
+
13: 'секунд',
|
|
286
|
+
14: 'секунд',
|
|
287
|
+
1: 'секунда',
|
|
288
|
+
2: 'секунды',
|
|
289
|
+
3: 'секунды',
|
|
290
|
+
4: 'секунды',
|
|
291
|
+
any: 'секунд'
|
|
292
|
+
}
|
|
293
|
+
},
|
|
294
|
+
rules: {
|
|
295
|
+
comma: ', ',
|
|
296
|
+
and: ' и ',
|
|
297
|
+
spaces: true,
|
|
298
|
+
useOneWordAtStartIfOne: true,
|
|
299
|
+
useOneWordIfOne: false,
|
|
300
|
+
wordThenNumber: false,
|
|
301
|
+
reverse: false,
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* @param {string} lang Language code (`en`/`ru`/...)
|
|
307
|
+
* @returns {Language}
|
|
308
|
+
*/
|
|
309
|
+
function getLanguage(lang) {
|
|
310
|
+
return languages[lang];
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* @param {string} lang Language code (`en`/`ru`/...)
|
|
314
|
+
* @param {Language} data
|
|
315
|
+
* @returns {Language}
|
|
316
|
+
*/
|
|
317
|
+
function setLanguage(lang, data) {
|
|
318
|
+
languages[lang] = typeof data == 'object' ? Array.isArray(data) ? (()=>{
|
|
319
|
+
throw new TypeError('ms2text: Invalid type of translations.');
|
|
320
|
+
})() : data : (()=>{
|
|
321
|
+
throw new TypeError('ms2text: Invalid type of translations.');
|
|
322
|
+
})();
|
|
323
|
+
return languages[lang];
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* @param {{years: number, months: number, weeks: number, days: number, hours: number, minutes: numbers, seconds: number}} numbers
|
|
328
|
+
* @param {string} lang
|
|
329
|
+
* @returns {string}
|
|
330
|
+
*/
|
|
331
|
+
function stringify(numbers, lang = 'en') {
|
|
332
|
+
if (!languages[lang]) throw new Error('ms2text: Invalid language');
|
|
333
|
+
const {years, months, weeks, days, hours, minutes, seconds} = numbers;
|
|
334
|
+
let code = [];
|
|
335
|
+
const words = {
|
|
336
|
+
years: 'years',
|
|
337
|
+
months: 'months',
|
|
338
|
+
weeks: 'weeks',
|
|
339
|
+
days: 'days',
|
|
340
|
+
hours: 'hours',
|
|
341
|
+
minutes: 'minutes',
|
|
342
|
+
seconds: 'seconds'
|
|
343
|
+
};
|
|
344
|
+
let useWeeks = false;
|
|
345
|
+
if (years > 0) code.push(words.years);
|
|
346
|
+
if (months > 0) code.push(words.months);
|
|
347
|
+
if (days > 0) {
|
|
348
|
+
if (weeks > 0 && days % 7 == 0) {
|
|
349
|
+
useWeeks = true;
|
|
350
|
+
code.push(words.weeks);
|
|
351
|
+
} else code.push(words.days);
|
|
352
|
+
};
|
|
353
|
+
if (hours > 0) code.push('hours');
|
|
354
|
+
if (minutes > 0) code.push('minutes');
|
|
355
|
+
if (seconds > 0) code.push('seconds');
|
|
356
|
+
const output = [];
|
|
357
|
+
if (languages[lang].rules.reverse) code = code.reverse();
|
|
358
|
+
for (const word of code) {
|
|
359
|
+
const number = numbers[word];
|
|
360
|
+
const words = languages[lang].words[word];
|
|
361
|
+
if (number == 1 && (
|
|
362
|
+
(languages[lang].rules.useOneWordAtStartIfOne && output.length == 0 && code.length > 1) ||
|
|
363
|
+
languages[lang].rules.useOneWordIfOne
|
|
364
|
+
)) {
|
|
365
|
+
output.push((words.oneWordPrefix ?? '') + (words[1] ?? words.any));
|
|
366
|
+
continue;
|
|
367
|
+
}
|
|
368
|
+
for (const [key, value] of Object.entries(words)) {
|
|
369
|
+
if (key == 'oneWordPrefix') continue;
|
|
370
|
+
const space = languages[lang].rules.spaces ? ' ' : '';
|
|
371
|
+
if (key == 'any') {
|
|
372
|
+
output.push(
|
|
373
|
+
languages[lang].rules.wordThenNumber ? value + space + String(number)
|
|
374
|
+
: String(number) + space + value
|
|
375
|
+
);
|
|
376
|
+
break;
|
|
377
|
+
} else if (String(number).endsWith(String(key))) {
|
|
378
|
+
output.push(
|
|
379
|
+
languages[lang].rules.wordThenNumber ? value + space + String(number)
|
|
380
|
+
: String(number) + space + value
|
|
381
|
+
);
|
|
382
|
+
break;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
};
|
|
386
|
+
const separator =
|
|
387
|
+
languages[lang].rules.comma ??
|
|
388
|
+
languages[lang].rules.and ?? '';
|
|
389
|
+
let outstr = output.length > 1
|
|
390
|
+
? output.slice(0, -2).join(separator) +
|
|
391
|
+
(output.length > 2 ? separator : '') + output.slice(-2).join(
|
|
392
|
+
languages[lang].rules.and ??
|
|
393
|
+
languages[lang].rules.comma ?? ''
|
|
394
|
+
)
|
|
395
|
+
: output[0] || '';
|
|
396
|
+
outstr = outstr.split('');
|
|
397
|
+
outstr[0] = outstr[0]?.toUpperCase();
|
|
398
|
+
return outstr.join('');
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* ---
|
|
403
|
+
*
|
|
404
|
+
* Converts time in milliseconds to a human-readable string.
|
|
405
|
+
*
|
|
406
|
+
* `time` - Time in milliseconds (`number`)
|
|
407
|
+
*
|
|
408
|
+
* ---
|
|
409
|
+
*
|
|
410
|
+
* Example:
|
|
411
|
+
* ```js
|
|
412
|
+
* ms2text(1234567) // '20 minutes and 35 seconds'
|
|
413
|
+
* ```
|
|
414
|
+
*
|
|
415
|
+
* @example
|
|
416
|
+
* ms2text(1234567) // '20 minutes and 35 seconds'
|
|
417
|
+
*
|
|
418
|
+
* @overload
|
|
419
|
+
* @param {number} time Milliseconds
|
|
420
|
+
* @returns {string} Human-readable time string
|
|
421
|
+
*/
|
|
422
|
+
/**
|
|
423
|
+
* ---
|
|
424
|
+
*
|
|
425
|
+
* Converts time in milliseconds to a human-readable string.
|
|
426
|
+
*
|
|
427
|
+
* `time` - Start date in milliseconds (`number`)
|
|
428
|
+
*
|
|
429
|
+
* `to` - End date in milliseconds (`number`)
|
|
430
|
+
*
|
|
431
|
+
* **Note: start/end date means the number of milliseconds that have elapsed since the Unix Epoch (January 1, 1970, 00:00:00 UTC)**
|
|
432
|
+
*
|
|
433
|
+
* ---
|
|
434
|
+
*
|
|
435
|
+
* Example:
|
|
436
|
+
* ```js
|
|
437
|
+
* ms2text(1767123193174, 1767124427741) // '20 minutes and 35 seconds'
|
|
438
|
+
* ```
|
|
439
|
+
*
|
|
440
|
+
* @example
|
|
441
|
+
* ms2text(1767123193174, 1767124427741) // '20 minutes and 35 seconds'
|
|
442
|
+
*
|
|
443
|
+
* @overload
|
|
444
|
+
* @param {number} time Start date in milliseconds
|
|
445
|
+
* @param {number} [to] End date in milliseconds
|
|
446
|
+
*/
|
|
447
|
+
/**
|
|
448
|
+
* ---
|
|
449
|
+
*
|
|
450
|
+
* Converts time in milliseconds to a human-readable string.
|
|
451
|
+
*
|
|
452
|
+
* `time` - Time in milliseconds (`number`)
|
|
453
|
+
*
|
|
454
|
+
* `to` - The number of days in a month (`number`) (`30` by default) (min: `28`, max: `31`)
|
|
455
|
+
*
|
|
456
|
+
* `year` - The number of days in a year (`number`) (`365` by default) (min: `360`)
|
|
457
|
+
*
|
|
458
|
+
* ---
|
|
459
|
+
*
|
|
460
|
+
* Example:
|
|
461
|
+
* ```js
|
|
462
|
+
* ms2text(1234567, 30, 365) // '20 minutes and 35 seconds'
|
|
463
|
+
* ```
|
|
464
|
+
*
|
|
465
|
+
* @example
|
|
466
|
+
* ms2text(1234567, 30, 365) // '20 minutes and 35 seconds'
|
|
467
|
+
*
|
|
468
|
+
* @overload
|
|
469
|
+
* @param {number} time Milliseconds
|
|
470
|
+
* @param {number} [to=30] The number of days in a month to be rounded and used in calculations (`30` by default)
|
|
471
|
+
* @param {number} [year=365] The number of days in a year to be rounded and used in calculations (`365` by default)
|
|
472
|
+
* @param {boolean?} [unlock] Unlock ranges for the number of days in a month and the number of days in a year
|
|
473
|
+
* @returns {string} Human-readable time string
|
|
474
|
+
*/
|
|
475
|
+
function main(time, to, year, unlock = false) {
|
|
476
|
+
const assume = {
|
|
477
|
+
month: 30,
|
|
478
|
+
year: 365,
|
|
479
|
+
monthsInYear: 12,
|
|
480
|
+
diff: 5
|
|
481
|
+
};
|
|
482
|
+
let language = 'en';
|
|
483
|
+
if (typeof to == 'string') {
|
|
484
|
+
language = to;
|
|
485
|
+
} else if (typeof year == 'number' && typeof to == 'number') {
|
|
486
|
+
if (to >= 28 && to <= 31 && (typeof unlock == 'boolean' && unlock)) assume.month = Math.round(to);
|
|
487
|
+
else throw new RangeError('ms2text: Invalid number of days in a month.');
|
|
488
|
+
|
|
489
|
+
if (year >= 360 && (typeof unlock == 'boolean' && unlock)) assume.year = Math.round(year);
|
|
490
|
+
else throw new RangeError('ms2text: Invalid number of days in a year.');
|
|
491
|
+
|
|
492
|
+
if (typeof unlock == 'string') {
|
|
493
|
+
language = unlock;
|
|
494
|
+
}
|
|
495
|
+
} else if (typeof year == 'string') {
|
|
496
|
+
language = year;
|
|
497
|
+
}
|
|
498
|
+
assume.diff = assume.year - assume.month * assume.monthsInYear;
|
|
499
|
+
const isRange = typeof to == 'number' && typeof year != 'number';
|
|
500
|
+
const diff = isRange ? (
|
|
501
|
+
time > to ? time - to :
|
|
502
|
+
time < to ? to - time :
|
|
503
|
+
0
|
|
504
|
+
) : time;
|
|
505
|
+
const as = {
|
|
506
|
+
seconds: Math.round(diff / 1000),
|
|
507
|
+
};
|
|
508
|
+
as.minutes = calc(as.seconds, 10, 60);
|
|
509
|
+
as.hours = calc(as.minutes, 100, 60);
|
|
510
|
+
as.days = calc(as.hours, 1000, 24);
|
|
511
|
+
const calced = {
|
|
512
|
+
seconds: as.seconds % 60
|
|
513
|
+
};
|
|
514
|
+
|
|
515
|
+
calced.minutes = calc((diff - calced.seconds * 1000) / 1000, 10, 60) % 60;
|
|
516
|
+
calced.hours = calc(Math.round((diff - calced.minutes * 1000 * 60 - calced.seconds * 1000) / 1000) / 60, 100, 60) % 24;
|
|
517
|
+
|
|
518
|
+
if (isRange && diff > 0) {
|
|
519
|
+
const date1 = new Date(time > to ? time : to);
|
|
520
|
+
const date2 = new Date(time > to ? to : time);
|
|
521
|
+
calced.years = date1.getFullYear() - date2.getFullYear();
|
|
522
|
+
calced.months = date1.getMonth() - date2.getMonth();
|
|
523
|
+
calced.days = date1.getDate() - date2.getDate();
|
|
524
|
+
if (calced.days < 0) {
|
|
525
|
+
calced.months -= 1;
|
|
526
|
+
calced.days = assume.month + calced.days;
|
|
527
|
+
}
|
|
528
|
+
if (calced.months < 0) {
|
|
529
|
+
calced.years -= 1;
|
|
530
|
+
calced.months = assume.monthsInYear + calced.months;
|
|
531
|
+
}
|
|
532
|
+
} else {
|
|
533
|
+
calced.days = calc(Math.round((diff - calced.hours * 1000 * 60 * 60 - calced.minutes * 1000 * 60 - calced.seconds * 1000) / 1000) / 60 / 60, 1000, 24);
|
|
534
|
+
|
|
535
|
+
calced.months = calc(
|
|
536
|
+
Math.round((
|
|
537
|
+
diff - calced.days % assume.month * 1000 * 60 * 60 * 24 -
|
|
538
|
+
calced.hours * 1000 * 60 * 60 -
|
|
539
|
+
calced.minutes * 1000 * 60 -
|
|
540
|
+
calced.seconds * 1000
|
|
541
|
+
) / 1000) / 60 / 60 / 24,
|
|
542
|
+
10000,
|
|
543
|
+
assume.month
|
|
544
|
+
);
|
|
545
|
+
|
|
546
|
+
calced.years = calc(
|
|
547
|
+
Math.round((
|
|
548
|
+
diff - calced.days % assume.year * 1000 * 60 * 60 * 24 -
|
|
549
|
+
calced.hours * 1000 * 60 * 60 -
|
|
550
|
+
calced.minutes * 1000 * 60 -
|
|
551
|
+
calced.seconds * 1000
|
|
552
|
+
) / 1000) / 60 / 60 / 24,
|
|
553
|
+
100000,
|
|
554
|
+
assume.year
|
|
555
|
+
);
|
|
556
|
+
|
|
557
|
+
if (calced.years > 0) {
|
|
558
|
+
calced.months = (calced.months - assume.diff * calced.years / assume.month) % assume.monthsInYear;
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
if (calced.months > 0) calced.days %= assume.month;
|
|
562
|
+
if (calced.months % 1 > 0) {
|
|
563
|
+
calced.days = Math.round((
|
|
564
|
+
diff - (calced.years * assume.year + calced.months * assume.month) * 1000 * 60 * 60 * 24
|
|
565
|
+
) / 1000) / 60 / 60 / 24 * 1.4;
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
return stringify({
|
|
570
|
+
years: Math.floor(calced.years),
|
|
571
|
+
months: Math.floor(calced.months),
|
|
572
|
+
weeks: Math.floor(Math.floor(calced.days) / 7),
|
|
573
|
+
days: Math.floor(calced.days),
|
|
574
|
+
hours: Math.floor(calced.hours),
|
|
575
|
+
minutes: Math.floor(calced.minutes),
|
|
576
|
+
seconds: Math.floor(calced.seconds)
|
|
577
|
+
}, language);
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
let output = {
|
|
581
|
+
ms2text: main
|
|
582
|
+
};
|
|
583
|
+
if (!(typeof define === 'function' && define.amd)) output = {
|
|
584
|
+
...output,
|
|
585
|
+
getLanguage, setLanguage
|
|
586
|
+
};
|
|
587
|
+
return output;
|
|
588
|
+
}));
|
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ms2text",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Convert time in milliseconds to a human-readable string.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/JustDeveloper1/ms2text.git"
|
|
9
|
+
},
|
|
10
|
+
"author": "JustDeveloper",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"types": "./index.d.ts",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/JustDeveloper1/ms2text/issues"
|
|
15
|
+
},
|
|
16
|
+
"homepage": "https://github.com/JustDeveloper1/ms2text#readme"
|
|
17
|
+
}
|