@w3ux/hooks 2.3.0 → 2.3.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/package.json +1 -1
- package/util.cjs +104 -0
- package/util.d.cts +7 -0
- package/util.d.ts +7 -0
- package/util.js +77 -0
package/package.json
CHANGED
package/util.cjs
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/util.ts
|
|
21
|
+
var util_exports = {};
|
|
22
|
+
__export(util_exports, {
|
|
23
|
+
getDuration: () => getDuration,
|
|
24
|
+
getDurationFromNow: () => getDurationFromNow,
|
|
25
|
+
secondsFromNow: () => secondsFromNow
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(util_exports);
|
|
28
|
+
var import_date_fns = require("date-fns");
|
|
29
|
+
|
|
30
|
+
// src/useTimeLeft/defaults.ts
|
|
31
|
+
var defaultDuration = {
|
|
32
|
+
days: 0,
|
|
33
|
+
hours: 0,
|
|
34
|
+
minutes: 0,
|
|
35
|
+
seconds: 0,
|
|
36
|
+
lastMinute: false
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// src/util.ts
|
|
40
|
+
var getDuration = (toDate) => {
|
|
41
|
+
if (!toDate) {
|
|
42
|
+
return defaultDuration;
|
|
43
|
+
}
|
|
44
|
+
if ((0, import_date_fns.getUnixTime)(toDate) <= (0, import_date_fns.getUnixTime)(/* @__PURE__ */ new Date())) {
|
|
45
|
+
return defaultDuration;
|
|
46
|
+
}
|
|
47
|
+
toDate.setSeconds(toDate.getSeconds());
|
|
48
|
+
const d = (0, import_date_fns.intervalToDuration)({
|
|
49
|
+
start: Date.now(),
|
|
50
|
+
end: toDate
|
|
51
|
+
});
|
|
52
|
+
const days = (0, import_date_fns.differenceInDays)(toDate, Date.now());
|
|
53
|
+
const hours = d?.hours || 0;
|
|
54
|
+
const minutes = d?.minutes || 0;
|
|
55
|
+
const seconds = d?.seconds || 0;
|
|
56
|
+
const lastHour = days === 0 && hours === 0;
|
|
57
|
+
const lastMinute = lastHour && minutes === 0;
|
|
58
|
+
return {
|
|
59
|
+
days,
|
|
60
|
+
hours,
|
|
61
|
+
minutes,
|
|
62
|
+
seconds,
|
|
63
|
+
lastMinute
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
var secondsFromNow = (seconds) => {
|
|
67
|
+
const end = /* @__PURE__ */ new Date();
|
|
68
|
+
end.setSeconds(end.getSeconds() + seconds);
|
|
69
|
+
return end;
|
|
70
|
+
};
|
|
71
|
+
var getDurationFromNow = (toDate) => {
|
|
72
|
+
if (!toDate) {
|
|
73
|
+
return defaultDuration;
|
|
74
|
+
}
|
|
75
|
+
if ((0, import_date_fns.getUnixTime)(toDate) <= (0, import_date_fns.getUnixTime)(/* @__PURE__ */ new Date())) {
|
|
76
|
+
return defaultDuration;
|
|
77
|
+
}
|
|
78
|
+
toDate.setSeconds(toDate.getSeconds());
|
|
79
|
+
const d = (0, import_date_fns.intervalToDuration)({
|
|
80
|
+
start: Date.now(),
|
|
81
|
+
end: toDate
|
|
82
|
+
});
|
|
83
|
+
const days = (0, import_date_fns.differenceInDays)(toDate, Date.now());
|
|
84
|
+
const hours = d?.hours || 0;
|
|
85
|
+
const minutes = d?.minutes || 0;
|
|
86
|
+
const seconds = d?.seconds || 0;
|
|
87
|
+
const lastHour = days === 0 && hours === 0;
|
|
88
|
+
const lastMinute = lastHour && minutes === 0;
|
|
89
|
+
return {
|
|
90
|
+
days,
|
|
91
|
+
hours,
|
|
92
|
+
minutes,
|
|
93
|
+
seconds,
|
|
94
|
+
lastMinute
|
|
95
|
+
};
|
|
96
|
+
};
|
|
97
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
98
|
+
0 && (module.exports = {
|
|
99
|
+
getDuration,
|
|
100
|
+
getDurationFromNow,
|
|
101
|
+
secondsFromNow
|
|
102
|
+
});
|
|
103
|
+
/* @license Copyright 2024 w3ux authors & contributors
|
|
104
|
+
SPDX-License-Identifier: GPL-3.0-only */
|
package/util.d.cts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { TimeleftDuration } from '@w3ux/types';
|
|
2
|
+
|
|
3
|
+
declare const getDuration: (toDate: Date | null) => TimeleftDuration;
|
|
4
|
+
declare const secondsFromNow: (seconds: number) => Date;
|
|
5
|
+
declare const getDurationFromNow: (toDate: Date | null) => TimeleftDuration;
|
|
6
|
+
|
|
7
|
+
export { getDuration, getDurationFromNow, secondsFromNow };
|
package/util.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { TimeleftDuration } from '@w3ux/types';
|
|
2
|
+
|
|
3
|
+
declare const getDuration: (toDate: Date | null) => TimeleftDuration;
|
|
4
|
+
declare const secondsFromNow: (seconds: number) => Date;
|
|
5
|
+
declare const getDurationFromNow: (toDate: Date | null) => TimeleftDuration;
|
|
6
|
+
|
|
7
|
+
export { getDuration, getDurationFromNow, secondsFromNow };
|
package/util.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// src/util.ts
|
|
2
|
+
import { differenceInDays, getUnixTime, intervalToDuration } from "date-fns";
|
|
3
|
+
|
|
4
|
+
// src/useTimeLeft/defaults.ts
|
|
5
|
+
var defaultDuration = {
|
|
6
|
+
days: 0,
|
|
7
|
+
hours: 0,
|
|
8
|
+
minutes: 0,
|
|
9
|
+
seconds: 0,
|
|
10
|
+
lastMinute: false
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
// src/util.ts
|
|
14
|
+
var getDuration = (toDate) => {
|
|
15
|
+
if (!toDate) {
|
|
16
|
+
return defaultDuration;
|
|
17
|
+
}
|
|
18
|
+
if (getUnixTime(toDate) <= getUnixTime(/* @__PURE__ */ new Date())) {
|
|
19
|
+
return defaultDuration;
|
|
20
|
+
}
|
|
21
|
+
toDate.setSeconds(toDate.getSeconds());
|
|
22
|
+
const d = intervalToDuration({
|
|
23
|
+
start: Date.now(),
|
|
24
|
+
end: toDate
|
|
25
|
+
});
|
|
26
|
+
const days = differenceInDays(toDate, Date.now());
|
|
27
|
+
const hours = d?.hours || 0;
|
|
28
|
+
const minutes = d?.minutes || 0;
|
|
29
|
+
const seconds = d?.seconds || 0;
|
|
30
|
+
const lastHour = days === 0 && hours === 0;
|
|
31
|
+
const lastMinute = lastHour && minutes === 0;
|
|
32
|
+
return {
|
|
33
|
+
days,
|
|
34
|
+
hours,
|
|
35
|
+
minutes,
|
|
36
|
+
seconds,
|
|
37
|
+
lastMinute
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
var secondsFromNow = (seconds) => {
|
|
41
|
+
const end = /* @__PURE__ */ new Date();
|
|
42
|
+
end.setSeconds(end.getSeconds() + seconds);
|
|
43
|
+
return end;
|
|
44
|
+
};
|
|
45
|
+
var getDurationFromNow = (toDate) => {
|
|
46
|
+
if (!toDate) {
|
|
47
|
+
return defaultDuration;
|
|
48
|
+
}
|
|
49
|
+
if (getUnixTime(toDate) <= getUnixTime(/* @__PURE__ */ new Date())) {
|
|
50
|
+
return defaultDuration;
|
|
51
|
+
}
|
|
52
|
+
toDate.setSeconds(toDate.getSeconds());
|
|
53
|
+
const d = intervalToDuration({
|
|
54
|
+
start: Date.now(),
|
|
55
|
+
end: toDate
|
|
56
|
+
});
|
|
57
|
+
const days = differenceInDays(toDate, Date.now());
|
|
58
|
+
const hours = d?.hours || 0;
|
|
59
|
+
const minutes = d?.minutes || 0;
|
|
60
|
+
const seconds = d?.seconds || 0;
|
|
61
|
+
const lastHour = days === 0 && hours === 0;
|
|
62
|
+
const lastMinute = lastHour && minutes === 0;
|
|
63
|
+
return {
|
|
64
|
+
days,
|
|
65
|
+
hours,
|
|
66
|
+
minutes,
|
|
67
|
+
seconds,
|
|
68
|
+
lastMinute
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
export {
|
|
72
|
+
getDuration,
|
|
73
|
+
getDurationFromNow,
|
|
74
|
+
secondsFromNow
|
|
75
|
+
};
|
|
76
|
+
/* @license Copyright 2024 w3ux authors & contributors
|
|
77
|
+
SPDX-License-Identifier: GPL-3.0-only */
|