UNPKG

1.59 kBJavaScriptView Raw
1import * as C from './constant';
2
3var padStart = function padStart(string, length, pad) {
4 var s = String(string);
5 if (!s || s.length >= length) return string;
6 return "" + Array(length + 1 - s.length).join(pad) + string;
7};
8
9var padZoneStr = function padZoneStr(instance) {
10 var negMinutes = -instance.utcOffset();
11 var minutes = Math.abs(negMinutes);
12 var hourOffset = Math.floor(minutes / 60);
13 var minuteOffset = minutes % 60;
14 return "" + (negMinutes <= 0 ? '+' : '-') + padStart(hourOffset, 2, '0') + ":" + padStart(minuteOffset, 2, '0');
15};
16
17var monthDiff = function monthDiff(a, b) {
18 // function from moment.js in order to keep the same result
19 if (a.date() < b.date()) return -monthDiff(b, a);
20 var wholeMonthDiff = (b.year() - a.year()) * 12 + (b.month() - a.month());
21 var anchor = a.clone().add(wholeMonthDiff, C.M);
22 var c = b - anchor < 0;
23 var anchor2 = a.clone().add(wholeMonthDiff + (c ? -1 : 1), C.M);
24 return +(-(wholeMonthDiff + (b - anchor) / (c ? anchor - anchor2 : anchor2 - anchor)) || 0);
25};
26
27var absFloor = function absFloor(n) {
28 return n < 0 ? Math.ceil(n) || 0 : Math.floor(n);
29};
30
31var prettyUnit = function prettyUnit(u) {
32 var special = {
33 M: C.M,
34 y: C.Y,
35 w: C.W,
36 d: C.D,
37 D: C.DATE,
38 h: C.H,
39 m: C.MIN,
40 s: C.S,
41 ms: C.MS,
42 Q: C.Q
43 };
44 return special[u] || String(u || '').toLowerCase().replace(/s$/, '');
45};
46
47var isUndefined = function isUndefined(s) {
48 return s === undefined;
49};
50
51export default {
52 s: padStart,
53 z: padZoneStr,
54 m: monthDiff,
55 a: absFloor,
56 p: prettyUnit,
57 u: isUndefined
58};
\No newline at end of file