ms2text 1.0.0 → 1.0.1

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/README.md CHANGED
@@ -1,2 +1,208 @@
1
- # ms2text
2
- Convert time in milliseconds to a human-readable string.
1
+ # `ms2text`
2
+ Convert time in milliseconds to a human-readable string. <br>
3
+ An UMD-module with fully typed API.
4
+
5
+ ## Installation
6
+ npm:
7
+ ```bash
8
+ npm i ms2text
9
+ ```
10
+
11
+ browsers / static website:
12
+ ```html
13
+ <!-- In HTML <head> -->
14
+ <script src="https://unpkg.com/ms2text" crossorigin="anonymous"></script>
15
+ ```
16
+
17
+ ## Usage
18
+ node.js:
19
+ ```js
20
+ const { ms2text } = require('ms2text');
21
+ ms2text(1234567); // 20 minutes and 35 seconds
22
+ ```
23
+
24
+ browsers / static website:
25
+ ```js
26
+ ms2text(1234567); // 20 minutes and 35 seconds
27
+ ```
28
+ <br>
29
+
30
+ **Languages** <br>
31
+ You can select output language of defined languages (see [Custom languages](#custom-languages)):
32
+
33
+ node.js:
34
+ ```js
35
+ const { ms2text } = require('ms2text');
36
+ ms2text(1234567, 'ru'); // 20 минут и 35 секунд
37
+ ```
38
+
39
+ browsers / static website:
40
+ ```js
41
+ ms2text(1234567, 'ru'); // 20 минут и 35 секунд
42
+ ```
43
+ <br>
44
+
45
+ **UNIX Timestamp range** <br>
46
+ node.js:
47
+ ```js
48
+ const { ms2text } = require('ms2text');
49
+ ms2text(1767123193174, 1767124427741); // 20 minutes and 35 seconds
50
+
51
+ /* You can also specify output language:
52
+ * ms2text(1767123193174, 1767124427741, 'ru'); // 20 минут и 35 секунд
53
+ */
54
+ ```
55
+
56
+ browsers / static website:
57
+ ```js
58
+ ms2text(1767123193174, 1767124427741); // 20 minutes and 35 seconds
59
+
60
+ /* You can also specify output language:
61
+ * ms2text(1767123193174, 1767124427741, 'ru'); // 20 минут и 35 секунд
62
+ */
63
+ ```
64
+ <br>
65
+
66
+ **Adjust calculations** <br>
67
+ You can specify the number of days in a month (`30` by default) and the number of days in a year (`365` by default):
68
+
69
+ node.js:
70
+ ```js
71
+ const { ms2text } = require('ms2text');
72
+ ms2text(
73
+ 1234567,
74
+ 30, // Days in a month
75
+ 365 // Days in a year
76
+ ); // '20 minutes and 35 seconds'
77
+
78
+ /* You can also specify output language:
79
+ * ms2text(1234567, 30, 365, 'ru'); // 20 минут и 35 секунд
80
+ */
81
+ ```
82
+
83
+ browsers / static website:
84
+ ```js
85
+ ms2text(
86
+ 1234567,
87
+ 30, // Days in a month
88
+ 365 // Days in a year
89
+ ); // '20 minutes and 35 seconds'
90
+
91
+ /* You can also specify output language:
92
+ * ms2text(1234567, 30, 365, 'ru'); // 20 минут и 35 секунд
93
+ */
94
+ ```
95
+
96
+ > **q:** Why can't I specify these numbers in UNIX Timestamp range? <br>
97
+ > **a:** It looks at the actual number of days in each month and the number of days in each year, so you don't need to adjust the calculations.
98
+
99
+ ### Custom languages
100
+ Currently (In `v1.0.1`), by default, there are already **`'en'` (English, default)** and **`'ru'` (Russian)**. <br>
101
+ But you can also define other languages or redefine the built-ins:
102
+ ```js
103
+ const exampleLang = {
104
+ words: {
105
+ years: {
106
+ 11: 'years',
107
+ 1: 'year',
108
+ /*
109
+ * It looks at the number as a string and checks if it ends with a number/string that matches the key. If it does, then the word is the value of that key.
110
+ * If there is no such key, it uses the word in "any".
111
+ *
112
+ * Top = more priority
113
+ * Bottom = less priority
114
+ */
115
+
116
+ any: 'years', // Should be plural
117
+ oneWordPrefix: 'a ' /* Optional. "1 year" -> "a year" (or you can leave it empty to make it "1 year" -> "year").
118
+ * Note: The first character of output will be uppercased.
119
+ * This will be used if `rules.useOneWordIfOne` and/or `rules.useOneWordAtStartIfOne`.
120
+ */
121
+ },
122
+ months: {
123
+ 11: 'months',
124
+ 1: 'month',
125
+ any: 'months',
126
+ oneWordPrefix: 'a '
127
+ },
128
+ weeks: {
129
+ 11: 'weeks',
130
+ 1: 'week',
131
+ any: 'weeks',
132
+ oneWordPrefix: 'a '
133
+ },
134
+ days: {
135
+ 11: 'days',
136
+ 1: 'day',
137
+ any: 'days',
138
+ oneWordPrefix: 'a '
139
+ },
140
+ hours: {
141
+ 11: 'hours',
142
+ 1: 'hour',
143
+ any: 'hours',
144
+ oneWordPrefix: 'an '
145
+ },
146
+ minutes: {
147
+ 11: 'minutes',
148
+ 1: 'minute',
149
+ any: 'minutes',
150
+ oneWordPrefix: 'a '
151
+ },
152
+ seconds: {
153
+ 11: 'seconds',
154
+ 1: 'second',
155
+ any: 'seconds',
156
+ oneWordPrefix: 'a '
157
+ }
158
+ },
159
+ rules: {
160
+ comma: ', ', // Separator ("A year, 4 days (...)"). It will use `rules.and` if this is not specified.
161
+ and: ' and ', // Last separator ("(...) 20 minutes and 35 seconds"). It will use `rules.comma` if this is not specified.
162
+ // It will not add any separator if both `rules.comma` and `rules.and` are not specified.
163
+
164
+ spaces: true, // Add space between number and word ("5 days").
165
+
166
+ useOneWordIfOne: false, // Use one word if the number is 1.
167
+ useOneWordAtStartIfOne: true, // Use one word if the number is 1 and this is the first word in output string.
168
+ // It will add a prefix if specified in `words{}`.
169
+
170
+ wordThenNumber: false, // Reverse "5 days" to "days 5".
171
+
172
+ reverse: false, /* Reverse from Years>Months>...>Minutes>Seconds to Seconds>Minutes>...>Months>Years.
173
+ * ("A year, 4 days, 20 minutes and 35 seconds" -> "35 seconds, 20 minutes, 4 days and a year")
174
+ */
175
+ }
176
+ }
177
+ ```
178
+
179
+ node.js:
180
+ ```js
181
+ const { getLanguage, setLanguage } = require('ms2text');
182
+ setLanguage(
183
+ 'example', // Language name/code
184
+ exampleLang
185
+ ); // Returns language object (`exampleLang`).
186
+
187
+ getLanguage('example'); // Returns language object or undefined if the language has not been defined.
188
+ ```
189
+
190
+ browsers / static website:
191
+ ```js
192
+ ms2text.setLanguage(
193
+ 'example', // Language name/code
194
+ exampleLang
195
+ ); // Returns language object (`exampleLang`).
196
+
197
+ ms2text.getLanguage('example'); // Returns language object or undefined if the language has not been defined.
198
+ ```
199
+
200
+ ---
201
+ ## `.min.js`
202
+ For `.min.js`, I use [UglifyJS](https://github.com/mishoo/UglifyJS) by [Mihai Bazon](https://github.com/mishoo).
203
+ ```bash
204
+ npm i uglify-js
205
+ ```
206
+ ```bash
207
+ uglifyjs index.js -c -m "reserved=['ms2text','getLanguage','setLanguage']" -o index.min.js
208
+ ```
package/index.js CHANGED
@@ -40,7 +40,7 @@ Convert time in milliseconds to a human-readable string.
40
40
 
41
41
  (function (root, factory) {
42
42
 
43
- const version = '1.0.0';
43
+ const version = '1.0.1';
44
44
 
45
45
  if (typeof define === 'function' && define.amd) {
46
46
  define([], factory);
package/index.min.js ADDED
@@ -0,0 +1 @@
1
+ ((e,s)=>{var n="1.0.1";"function"==typeof define&&define.amd?define([],s):"object"==typeof module&&module.exports?module.exports={...s(),version:n}:(s=s(),e.ms2text=s.ms2text,Object.defineProperties(e.ms2text,{getLanguage:{value:s.getLanguage},setLanguage:{value:s.setLanguage},version:{value:n,enumerable:!1}}))})("undefined"!=typeof self?self:this,function(){function e(e,s,n){return Math.round(s*Math.round(e)/n)/s}let k={en:{words:{years:{11:"years",21:"years",31:"years",41:"years",51:"years",61:"years",71:"years",81:"years",91:"years","01":"years",1:"year",any:"years",oneWordPrefix:"a "},months:{11:"months",21:"months",31:"months",41:"months",51:"months",61:"months",71:"months",81:"months",91:"months","01":"months",1:"month",any:"months",oneWordPrefix:"a "},weeks:{11:"weeks",21:"weeks",31:"weeks",41:"weeks",51:"weeks",61:"weeks",71:"weeks",81:"weeks",91:"weeks","01":"weeks",1:"week",any:"weeks",oneWordPrefix:"a "},days:{11:"days",21:"days",31:"days",41:"days",51:"days",61:"days",71:"days",81:"days",91:"days","01":"days",1:"day",any:"days",oneWordPrefix:"a "},hours:{11:"hours",21:"hours",31:"hours",41:"hours",51:"hours",61:"hours",71:"hours",81:"hours",91:"hours","01":"hours",1:"hour",any:"hours",oneWordPrefix:"an "},minutes:{11:"minutes",21:"minutes",31:"minutes",41:"minutes",51:"minutes",61:"minutes",71:"minutes",81:"minutes",91:"minutes","01":"minutes",1:"minute",any:"minutes",oneWordPrefix:"a "},seconds:{11:"seconds",21:"seconds",31:"seconds",41:"seconds",51:"seconds",61:"seconds",71:"seconds",81:"seconds",91:"seconds","01":"seconds",1:"second",any:"seconds",oneWordPrefix:"a "}},rules:{comma:", ",and:" and ",spaces:!0,useOneWordAtStartIfOne:!0,useOneWordIfOne:!1,wordThenNumber:!1,reverse:!1}},ru:{words:{years:{11:"лет",12:"лет",13:"лет",14:"лет",1:"год",2:"года",3:"года",4:"года",any:"лет"},months:{11:"месяцев",12:"месяцев",13:"месяцев",14:"месяцев",1:"месяц",2:"месяца",3:"месяца",4:"месяца",any:"месяцев"},weeks:{11:"неделей",12:"неделей",13:"неделей",14:"неделей",1:"неделя",2:"недели",3:"недели",4:"недели",any:"неделей"},days:{11:"дней",12:"дней",13:"дней",14:"дней",1:"день",2:"дня",3:"дня",4:"дня",any:"дней"},hours:{11:"часов",12:"часов",13:"часов",14:"часов",1:"час",2:"часа",3:"часа",4:"часа",any:"часов"},minutes:{11:"минут",12:"минут",13:"минут",14:"минут",1:"минута",2:"минуты",3:"минуты",4:"минуты",any:"минут"},seconds:{11:"секунд",12:"секунд",13:"секунд",14:"секунд",1:"секунда",2:"секунды",3:"секунды",4:"секунды",any:"секунд"}},rules:{comma:", ",and:" и ",spaces:!0,useOneWordAtStartIfOne:!0,useOneWordIfOne:!1,wordThenNumber:!1,reverse:!1}}};let s={ms2text:function(o,r,t,a=!1){var u={month:30,year:365,monthsInYear:12,diff:5};let d="en";if("string"==typeof r)d=r;else if("number"==typeof t&&"number"==typeof r){if(!(28<=r&&r<=31&&"boolean"==typeof a&&a))throw new RangeError("ms2text: Invalid number of days in a month.");if(u.month=Math.round(r),!(360<=t&&"boolean"==typeof a&&a))throw new RangeError("ms2text: Invalid number of days in a year.");u.year=Math.round(t),"string"==typeof a&&(d=a)}else"string"==typeof t&&(d=t);u.diff=u.year-u.month*u.monthsInYear;t=(a="number"==typeof r&&"number"!=typeof t)?r<o?o-r:o<r?r-o:0:o;(y={seconds:Math.round(t/1e3)}).minutes=e(y.seconds,10,60),y.hours=e(y.minutes,100,60),y.days=e(y.hours,1e3,24),(y={seconds:y.seconds%60}).minutes=e((t-1e3*y.seconds)/1e3,10,60)%60,y.hours=e(Math.round((t-1e3*y.minutes*60-1e3*y.seconds)/1e3)/60,100,60)%24,a&&0<t?(a=new Date(r<o?o:r),r=new Date(r<o?r:o),y.years=a.getFullYear()-r.getFullYear(),y.months=a.getMonth()-r.getMonth(),y.days=a.getDate()-r.getDate(),y.days<0&&(--y.months,y.days=u.month+y.days),y.months<0&&(--y.years,y.months=u.monthsInYear+y.months)):(y.days=e(Math.round((t-1e3*y.hours*60*60-1e3*y.minutes*60-1e3*y.seconds)/1e3)/60/60,1e3,24),y.months=e(Math.round((t-y.days%u.month*1e3*60*60*24-1e3*y.hours*60*60-1e3*y.minutes*60-1e3*y.seconds)/1e3)/60/60/24,1e4,u.month),y.years=e(Math.round((t-y.days%u.year*1e3*60*60*24-1e3*y.hours*60*60-1e3*y.minutes*60-1e3*y.seconds)/1e3)/60/60/24,1e5,u.year),0<y.years&&(y.months=(y.months-u.diff*y.years/u.month)%u.monthsInYear),0<y.months&&(y.days%=u.month),0<y.months%1&&(y.days=Math.round((t-1e3*(y.years*u.year+y.months*u.month)*60*60*24)/1e3)/60/60/24*1.4));{var[h,m="en"]=[{years:Math.floor(y.years),months:Math.floor(y.months),weeks:Math.floor(Math.floor(y.days)/7),days:Math.floor(y.days),hours:Math.floor(y.hours),minutes:Math.floor(y.minutes),seconds:Math.floor(y.seconds)},d];if(!k[m])throw new Error("ms2text: Invalid language");var{years:o,months:a,weeks:r,days:t,hours:u,minutes:y,seconds:i}=h;let s=[],e={years:"years",months:"months",weeks:"weeks",days:"days",hours:"hours",minutes:"minutes",seconds:"seconds"};0<o&&s.push(e.years),0<a&&s.push(e.months),0<t&&(0<r&&t%7==0?s.push(e.weeks):s.push(e.days)),0<u&&s.push("hours"),0<y&&s.push("minutes"),0<i&&s.push("seconds");var f,l=[];for(f of s=k[m].rules.reverse?s.reverse():s){var c=h[f];let e=k[m].words[f];if(1==c&&(k[m].rules.useOneWordAtStartIfOne&&0==l.length&&1<s.length||k[m].rules.useOneWordIfOne))l.push((e.oneWordPrefix??"")+(e[1]??e.any));else for(var[g,w]of Object.entries(e))if("oneWordPrefix"!=g){var p=k[m].rules.spaces?" ":"";if("any"==g){l.push(k[m].rules.wordThenNumber?w+p+String(c):String(c)+p+w);break}if(String(c).endsWith(String(g))){l.push(k[m].rules.wordThenNumber?w+p+String(c):String(c)+p+w);break}}}o=k[m].rules.comma??k[m].rules.and??"";let n=1<l.length?l.slice(0,-2).join(o)+(2<l.length?o:"")+l.slice(-2).join(k[m].rules.and??k[m].rules.comma??""):l[0]||"";return(n=n.split(""))[0]=n[0]?.toUpperCase(),n.join("")}}};return s="function"==typeof define&&define.amd?s:{...s,getLanguage:function(e){return k[e]},setLanguage:function(e,s){return k[e]="object"!=typeof s||Array.isArray(s)?(()=>{throw new TypeError("ms2text: Invalid type of translations.")})():s,k[e]}}});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ms2text",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Convert time in milliseconds to a human-readable string.",
5
5
  "main": "index.js",
6
6
  "repository": {
@@ -13,5 +13,6 @@
13
13
  "bugs": {
14
14
  "url": "https://github.com/JustDeveloper1/ms2text/issues"
15
15
  },
16
- "homepage": "https://github.com/JustDeveloper1/ms2text#readme"
16
+ "homepage": "https://github.com/JustDeveloper1/ms2text#readme",
17
+ "unpkg": "./index.min.js"
17
18
  }