tiny-essentials 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/.github/FUNDING.yml +4 -0
- package/.github/workflows/node.js.yml +31 -0
- package/LICENSE +674 -0
- package/README.md +67 -0
- package/dist/TinyEssentials.min.js +1 -0
- package/dist/_virtual/_commonjsHelpers.cjs +7 -0
- package/dist/_virtual/common.cjs +5 -0
- package/dist/_virtual/index.cjs +9 -0
- package/dist/_virtual/index2.cjs +5 -0
- package/dist/_virtual/trace.cjs +5 -0
- package/dist/legacy/crypto/decrypt.cjs +57 -0
- package/dist/legacy/crypto/decrypt.d.mts +18 -0
- package/dist/legacy/crypto/decrypt.mjs +47 -0
- package/dist/legacy/crypto/default.cjs +23 -0
- package/dist/legacy/crypto/default.d.mts +35 -0
- package/dist/legacy/crypto/default.mjs +19 -0
- package/dist/legacy/crypto/encrypt.cjs +54 -0
- package/dist/legacy/crypto/encrypt.d.mts +19 -0
- package/dist/legacy/crypto/encrypt.mjs +46 -0
- package/dist/legacy/crypto/index.cjs +11 -0
- package/dist/legacy/crypto/index.d.mts +4 -0
- package/dist/legacy/crypto/index.mjs +6 -0
- package/dist/legacy/firebase/cookieSession.cjs +121 -0
- package/dist/legacy/firebase/cookieSession.d.mts +56 -0
- package/dist/legacy/firebase/cookieSession.mjs +114 -0
- package/dist/legacy/firebase/database/index.cjs +11 -0
- package/dist/legacy/firebase/database/index.d.mts +7 -0
- package/dist/legacy/firebase/database/index.mjs +7 -0
- package/dist/legacy/firebase/database/presence.cjs +131 -0
- package/dist/legacy/firebase/database/presence.d.mts +9 -0
- package/dist/legacy/firebase/database/presence.mjs +111 -0
- package/dist/legacy/firebase/database/saveAsync.cjs +147 -0
- package/dist/legacy/firebase/database/saveAsync.d.mts +54 -0
- package/dist/legacy/firebase/database/saveAsync.mjs +147 -0
- package/dist/legacy/firebase/databaseEscape.cjs +42 -0
- package/dist/legacy/firebase/databaseEscape.d.mts +11 -0
- package/dist/legacy/firebase/databaseEscape.mjs +37 -0
- package/dist/legacy/firebase/databaseLogger.cjs +258 -0
- package/dist/legacy/firebase/databaseLogger.d.mts +7 -0
- package/dist/legacy/firebase/databaseLogger.mjs +234 -0
- package/dist/legacy/firebase/discord/api/addGuildMember.cjs +71 -0
- package/dist/legacy/firebase/discord/api/addGuildMember.d.mts +38 -0
- package/dist/legacy/firebase/discord/api/addGuildMember.mjs +64 -0
- package/dist/legacy/firebase/discord/api/getGuildWidget.cjs +47 -0
- package/dist/legacy/firebase/discord/api/getGuildWidget.d.mts +17 -0
- package/dist/legacy/firebase/discord/api/getGuildWidget.mjs +43 -0
- package/dist/legacy/firebase/discord/api/getToken.cjs +67 -0
- package/dist/legacy/firebase/discord/api/getToken.d.mts +35 -0
- package/dist/legacy/firebase/discord/api/getToken.mjs +63 -0
- package/dist/legacy/firebase/discord/api/getUser.cjs +58 -0
- package/dist/legacy/firebase/discord/api/getUser.d.mts +27 -0
- package/dist/legacy/firebase/discord/api/getUser.mjs +54 -0
- package/dist/legacy/firebase/discord/api/getUserConnections.cjs +57 -0
- package/dist/legacy/firebase/discord/api/getUserConnections.d.mts +26 -0
- package/dist/legacy/firebase/discord/api/getUserConnections.mjs +53 -0
- package/dist/legacy/firebase/discord/api/getUserGuilds.cjs +56 -0
- package/dist/legacy/firebase/discord/api/getUserGuilds.d.mts +25 -0
- package/dist/legacy/firebase/discord/api/getUserGuilds.mjs +52 -0
- package/dist/legacy/firebase/discord/api/index.cjs +24 -0
- package/dist/legacy/firebase/discord/api/index.d.mts +19 -0
- package/dist/legacy/firebase/discord/api/index.mjs +20 -0
- package/dist/legacy/firebase/discord/api/refreshToken.cjs +77 -0
- package/dist/legacy/firebase/discord/api/refreshToken.d.mts +45 -0
- package/dist/legacy/firebase/discord/api/refreshToken.mjs +73 -0
- package/dist/legacy/firebase/discord/api/revokeToken.cjs +61 -0
- package/dist/legacy/firebase/discord/api/revokeToken.d.mts +29 -0
- package/dist/legacy/firebase/discord/api/revokeToken.mjs +58 -0
- package/dist/legacy/firebase/discord/config.cjs +21 -0
- package/dist/legacy/firebase/discord/config.d.mts +27 -0
- package/dist/legacy/firebase/discord/config.mjs +17 -0
- package/dist/legacy/firebase/discord/firebase_redirect/index.cjs +11 -0
- package/dist/legacy/firebase/discord/firebase_redirect/index.d.mts +7 -0
- package/dist/legacy/firebase/discord/firebase_redirect/index.mjs +7 -0
- package/dist/legacy/firebase/discord/firebase_redirect/login.cjs +110 -0
- package/dist/legacy/firebase/discord/firebase_redirect/login.d.mts +4 -0
- package/dist/legacy/firebase/discord/firebase_redirect/login.mjs +107 -0
- package/dist/legacy/firebase/discord/firebase_redirect/logout.cjs +100 -0
- package/dist/legacy/firebase/discord/firebase_redirect/logout.d.mts +4 -0
- package/dist/legacy/firebase/discord/firebase_redirect/logout.mjs +97 -0
- package/dist/legacy/firebase/discord/get/authURLGenerator.cjs +76 -0
- package/dist/legacy/firebase/discord/get/authURLGenerator.d.mts +23 -0
- package/dist/legacy/firebase/discord/get/authURLGenerator.mjs +67 -0
- package/dist/legacy/firebase/discord/get/cookie-session.cjs +32 -0
- package/dist/legacy/firebase/discord/get/cookie-session.d.mts +20 -0
- package/dist/legacy/firebase/discord/get/cookie-session.mjs +28 -0
- package/dist/legacy/firebase/discord/get/credentials.cjs +24 -0
- package/dist/legacy/firebase/discord/get/credentials.d.mts +19 -0
- package/dist/legacy/firebase/discord/get/credentials.mjs +20 -0
- package/dist/legacy/firebase/discord/get/errorValidator.cjs +60 -0
- package/dist/legacy/firebase/discord/get/errorValidator.d.mts +28 -0
- package/dist/legacy/firebase/discord/get/errorValidator.mjs +55 -0
- package/dist/legacy/firebase/discord/get/index.cjs +17 -0
- package/dist/legacy/firebase/discord/get/index.d.mts +13 -0
- package/dist/legacy/firebase/discord/get/index.mjs +13 -0
- package/dist/legacy/firebase/discord/get/randomAvatar.cjs +37 -0
- package/dist/legacy/firebase/discord/get/randomAvatar.d.mts +22 -0
- package/dist/legacy/firebase/discord/get/randomAvatar.mjs +30 -0
- package/dist/legacy/firebase/discord/http/index.cjs +15 -0
- package/dist/legacy/firebase/discord/http/index.d.mts +11 -0
- package/dist/legacy/firebase/discord/http/index.mjs +11 -0
- package/dist/legacy/firebase/discord/http/login.cjs +127 -0
- package/dist/legacy/firebase/discord/http/login.d.mts +21 -0
- package/dist/legacy/firebase/discord/http/login.mjs +123 -0
- package/dist/legacy/firebase/discord/http/logout.cjs +155 -0
- package/dist/legacy/firebase/discord/http/logout.d.mts +22 -0
- package/dist/legacy/firebase/discord/http/logout.mjs +143 -0
- package/dist/legacy/firebase/discord/http/redirect.cjs +188 -0
- package/dist/legacy/firebase/discord/http/redirect.d.mts +24 -0
- package/dist/legacy/firebase/discord/http/redirect.mjs +178 -0
- package/dist/legacy/firebase/discord/http/refreshToken.cjs +133 -0
- package/dist/legacy/firebase/discord/http/refreshToken.d.mts +24 -0
- package/dist/legacy/firebase/discord/http/refreshToken.mjs +121 -0
- package/dist/legacy/firebase/discord/index.cjs +19 -0
- package/dist/legacy/firebase/discord/index.d.mts +15 -0
- package/dist/legacy/firebase/discord/index.mjs +15 -0
- package/dist/legacy/firebase/discord/template/cookie-session.cjs +1430 -0
- package/dist/legacy/firebase/discord/template/cookie-session.d.mts +22 -0
- package/dist/legacy/firebase/discord/template/cookie-session.mjs +1159 -0
- package/dist/legacy/firebase/domainRedirect.cjs +35 -0
- package/dist/legacy/firebase/domainRedirect.d.mts +12 -0
- package/dist/legacy/firebase/domainRedirect.mjs +27 -0
- package/dist/legacy/firebase/escape.cjs +75 -0
- package/dist/legacy/firebase/escape.d.mts +17 -0
- package/dist/legacy/firebase/escape.mjs +58 -0
- package/dist/legacy/firebase/getDBAsync.cjs +33 -0
- package/dist/legacy/firebase/getDBAsync.d.mts +10 -0
- package/dist/legacy/firebase/getDBAsync.mjs +26 -0
- package/dist/legacy/firebase/getDBData.cjs +21 -0
- package/dist/legacy/firebase/getDBData.d.mts +1 -0
- package/dist/legacy/firebase/getDBData.mjs +17 -0
- package/dist/legacy/firebase/getDBValue.cjs +17 -0
- package/dist/legacy/firebase/getDBValue.d.mts +9 -0
- package/dist/legacy/firebase/getDBValue.mjs +14 -0
- package/dist/legacy/firebase/index.cjs +261 -0
- package/dist/legacy/firebase/index.d.mts +51 -0
- package/dist/legacy/firebase/index.mjs +234 -0
- package/dist/legacy/firebase/isEmulator.cjs +14 -0
- package/dist/legacy/firebase/isEmulator.d.mts +8 -0
- package/dist/legacy/firebase/isEmulator.mjs +10 -0
- package/dist/legacy/firebase/logger.cjs +137 -0
- package/dist/legacy/firebase/logger.d.mts +7 -0
- package/dist/legacy/firebase/logger.mjs +125 -0
- package/dist/legacy/firebase/mySQL.cjs +26 -0
- package/dist/legacy/firebase/mySQL.d.mts +10 -0
- package/dist/legacy/firebase/mySQL.mjs +22 -0
- package/dist/legacy/firebase/mysqlConnector/create.cjs +119 -0
- package/dist/legacy/firebase/mysqlConnector/create.d.mts +36 -0
- package/dist/legacy/firebase/mysqlConnector/create.mjs +109 -0
- package/dist/legacy/firebase/mysqlConnector/index.cjs +23 -0
- package/dist/legacy/firebase/mysqlConnector/index.d.mts +11 -0
- package/dist/legacy/firebase/mysqlConnector/index.mjs +16 -0
- package/dist/legacy/firebase/mysqlConnector/nextPrev.cjs +75 -0
- package/dist/legacy/firebase/mysqlConnector/nextPrev.d.mts +31 -0
- package/dist/legacy/firebase/mysqlConnector/nextPrev.mjs +63 -0
- package/dist/legacy/firebase/mysqlConnector/pagination.cjs +91 -0
- package/dist/legacy/firebase/mysqlConnector/pagination.d.mts +33 -0
- package/dist/legacy/firebase/mysqlConnector/pagination.mjs +87 -0
- package/dist/legacy/firebase/mysqlConnector/sameUser.cjs +28 -0
- package/dist/legacy/firebase/mysqlConnector/sameUser.d.mts +12 -0
- package/dist/legacy/firebase/mysqlConnector/sameUser.mjs +23 -0
- package/dist/legacy/firebase/transactionDBAsync.cjs +33 -0
- package/dist/legacy/firebase/transactionDBAsync.d.mts +11 -0
- package/dist/legacy/firebase/transactionDBAsync.mjs +27 -0
- package/dist/legacy/get/countObj.cjs +24 -0
- package/dist/legacy/get/countObj.d.mts +11 -0
- package/dist/legacy/get/countObj.mjs +21 -0
- package/dist/legacy/get/decimalColor.cjs +60 -0
- package/dist/legacy/get/decimalColor.d.mts +12 -0
- package/dist/legacy/get/decimalColor.mjs +54 -0
- package/dist/legacy/get/objType.cjs +43 -0
- package/dist/legacy/get/objType.d.mts +19 -0
- package/dist/legacy/get/objType.mjs +40 -0
- package/dist/legacy/get/pagination.cjs +100 -0
- package/dist/legacy/get/pagination.d.mts +25 -0
- package/dist/legacy/get/pagination.mjs +91 -0
- package/dist/legacy/get/queryUrlByName.cjs +31 -0
- package/dist/legacy/get/queryUrlByName.d.mts +20 -0
- package/dist/legacy/get/queryUrlByName.mjs +28 -0
- package/dist/legacy/get/queryUrlJSON.cjs +54 -0
- package/dist/legacy/get/queryUrlJSON.d.mts +23 -0
- package/dist/legacy/get/queryUrlJSON.mjs +57 -0
- package/dist/legacy/get/super_string_filter.cjs +97 -0
- package/dist/legacy/get/super_string_filter.d.mts +25 -0
- package/dist/legacy/get/super_string_filter.mjs +83 -0
- package/dist/legacy/get/versionCheck.cjs +49 -0
- package/dist/legacy/get/versionCheck.d.mts +17 -0
- package/dist/legacy/get/versionCheck.mjs +38 -0
- package/dist/legacy/http/HTTP-1.0.cjs +151 -0
- package/dist/legacy/http/HTTP-1.0.d.mts +8 -0
- package/dist/legacy/http/HTTP-1.0.mjs +136 -0
- package/dist/legacy/http/auth.cjs +70 -0
- package/dist/legacy/http/auth.d.mts +41 -0
- package/dist/legacy/http/auth.mjs +63 -0
- package/dist/legacy/http/check_domain.cjs +84 -0
- package/dist/legacy/http/check_domain.d.mts +10 -0
- package/dist/legacy/http/check_domain.mjs +82 -0
- package/dist/legacy/http/csrfTokenAnalyze.cjs +46 -0
- package/dist/legacy/http/csrfTokenAnalyze.d.mts +21 -0
- package/dist/legacy/http/csrfTokenAnalyze.mjs +40 -0
- package/dist/legacy/http/domainValidator.cjs +98 -0
- package/dist/legacy/http/domainValidator.d.mts +30 -0
- package/dist/legacy/http/domainValidator.mjs +82 -0
- package/dist/legacy/http/errorsCallback.cjs +55 -0
- package/dist/legacy/http/errorsCallback.d.mts +29 -0
- package/dist/legacy/http/errorsCallback.mjs +48 -0
- package/dist/legacy/http/fetch/json.cjs +48 -0
- package/dist/legacy/http/fetch/json.d.mts +22 -0
- package/dist/legacy/http/fetch/json.mjs +41 -0
- package/dist/legacy/http/fetch/text.cjs +47 -0
- package/dist/legacy/http/fetch/text.d.mts +21 -0
- package/dist/legacy/http/fetch/text.mjs +40 -0
- package/dist/legacy/http/fileCache.cjs +79 -0
- package/dist/legacy/http/fileCache.d.mts +35 -0
- package/dist/legacy/http/fileCache.mjs +70 -0
- package/dist/legacy/http/getDomainURL.cjs +59 -0
- package/dist/legacy/http/getDomainURL.d.mts +18 -0
- package/dist/legacy/http/getDomainURL.mjs +54 -0
- package/dist/legacy/http/userIP.cjs +59 -0
- package/dist/legacy/http/userIP.d.mts +21 -0
- package/dist/legacy/http/userIP.mjs +48 -0
- package/dist/legacy/index.cjs +122 -0
- package/dist/legacy/index.d.mts +89 -0
- package/dist/legacy/index.mjs +92 -0
- package/dist/legacy/libs/arraySortPositions.cjs +30 -0
- package/dist/legacy/libs/arraySortPositions.d.mts +16 -0
- package/dist/legacy/libs/arraySortPositions.mjs +27 -0
- package/dist/legacy/libs/capitalize.cjs +21 -0
- package/dist/legacy/libs/capitalize.d.mts +13 -0
- package/dist/legacy/libs/capitalize.mjs +17 -0
- package/dist/legacy/libs/convertBytes.cjs +64 -0
- package/dist/legacy/libs/convertBytes.d.mts +17 -0
- package/dist/legacy/libs/convertBytes.mjs +55 -0
- package/dist/legacy/libs/custom_module_loader.cjs +82 -0
- package/dist/legacy/libs/custom_module_loader.d.mts +5 -0
- package/dist/legacy/libs/custom_module_loader.mjs +79 -0
- package/dist/legacy/libs/dice.cjs +35 -0
- package/dist/legacy/libs/dice.d.mts +5 -0
- package/dist/legacy/libs/dice.mjs +31 -0
- package/dist/legacy/libs/markdown.cjs +85 -0
- package/dist/legacy/libs/markdown.d.mts +75 -0
- package/dist/legacy/libs/markdown.mjs +78 -0
- package/dist/legacy/libs/percentage.cjs +22 -0
- package/dist/legacy/libs/percentage.d.mts +4 -0
- package/dist/legacy/libs/percentage.mjs +19 -0
- package/dist/legacy/libs/regex/getLetter.cjs +22 -0
- package/dist/legacy/libs/regex/getLetter.d.mts +12 -0
- package/dist/legacy/libs/regex/getLetter.mjs +19 -0
- package/dist/legacy/libs/replaceAsync.cjs +32 -0
- package/dist/legacy/libs/replaceAsync.d.mts +15 -0
- package/dist/legacy/libs/replaceAsync.mjs +25 -0
- package/dist/legacy/libs/rule3.cjs +58 -0
- package/dist/legacy/libs/rule3.d.mts +5 -0
- package/dist/legacy/libs/rule3.mjs +54 -0
- package/dist/legacy/libs/userLevel.cjs +134 -0
- package/dist/legacy/libs/userLevel.d.mts +69 -0
- package/dist/legacy/libs/userLevel.mjs +123 -0
- package/dist/legacy/momentjs/getAge.cjs +28 -0
- package/dist/legacy/momentjs/getAge.d.mts +8 -0
- package/dist/legacy/momentjs/getAge.mjs +22 -0
- package/dist/legacy/momentjs/index.cjs +9 -0
- package/dist/legacy/momentjs/index.d.mts +3 -0
- package/dist/legacy/momentjs/index.mjs +3 -0
- package/dist/legacy/momentjs/timeDuration.cjs +29 -0
- package/dist/legacy/momentjs/timeDuration.d.mts +9 -0
- package/dist/legacy/momentjs/timeDuration.mjs +23 -0
- package/dist/legacy/socket.io/antiFlood/index.cjs +9 -0
- package/dist/legacy/socket.io/antiFlood/index.d.mts +3 -0
- package/dist/legacy/socket.io/antiFlood/index.mjs +3 -0
- package/dist/legacy/socket.io/antiFlood/install.cjs +32 -0
- package/dist/legacy/socket.io/antiFlood/install.d.mts +14 -0
- package/dist/legacy/socket.io/antiFlood/install.mjs +27 -0
- package/dist/legacy/socket.io/antiFlood/verify.cjs +87 -0
- package/dist/legacy/socket.io/antiFlood/verify.d.mts +15 -0
- package/dist/legacy/socket.io/antiFlood/verify.mjs +73 -0
- package/dist/legacy/socket.io/cookie-session.cjs +25 -0
- package/dist/legacy/socket.io/cookie-session.d.mts +9 -0
- package/dist/legacy/socket.io/cookie-session.mjs +20 -0
- package/dist/legacy/socket.io/discord.cjs +61 -0
- package/dist/legacy/socket.io/discord.d.mts +10 -0
- package/dist/legacy/socket.io/discord.mjs +52 -0
- package/dist/legacy/socket.io/index.cjs +13 -0
- package/dist/legacy/socket.io/index.d.mts +9 -0
- package/dist/legacy/socket.io/index.mjs +9 -0
- package/dist/v1/basics/array.cjs +31 -0
- package/dist/v1/basics/array.d.mts +10 -0
- package/dist/v1/basics/array.mjs +22 -0
- package/dist/v1/basics/clock.cjs +187 -0
- package/dist/v1/basics/clock.d.mts +37 -0
- package/dist/v1/basics/clock.mjs +152 -0
- package/dist/v1/basics/objFilter.cjs +55 -0
- package/dist/v1/basics/objFilter.d.mts +27 -0
- package/dist/v1/basics/objFilter.mjs +52 -0
- package/dist/v1/basics/simpleMath.cjs +88 -0
- package/dist/v1/basics/simpleMath.d.mts +54 -0
- package/dist/v1/basics/simpleMath.mjs +75 -0
- package/dist/v1/basics/text.cjs +34 -0
- package/dist/v1/basics/text.d.mts +20 -0
- package/dist/v1/basics/text.mjs +25 -0
- package/dist/v1/index.cjs +26 -0
- package/dist/v1/index.d.mts +15 -0
- package/dist/v1/index.mjs +8 -0
- package/package.json +72 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Handles the cookie session for a socket connection by simulating an Express request and response.
|
|
3
|
+
* This function uses the provided session module to manage session data for the socket connection.
|
|
4
|
+
*
|
|
5
|
+
* @param {Object} socket - The socket object representing the connection.
|
|
6
|
+
* @param {Function} sessionModule - The session module function, typically used with Express, that handles session logic.
|
|
7
|
+
* @returns {Promise<Object>} A promise that resolves with the session object once the session is processed.
|
|
8
|
+
*/
|
|
9
|
+
export default function cookieSession(socket, sessionModule) {
|
|
10
|
+
return new Promise((resolve) => {
|
|
11
|
+
// Express Simulator
|
|
12
|
+
let req = {
|
|
13
|
+
connection: { encrypted: false },
|
|
14
|
+
headers: { cookie: socket.request.headers.cookie },
|
|
15
|
+
};
|
|
16
|
+
let res = { getHeader: () => { }, setHeader: () => { } };
|
|
17
|
+
// Session
|
|
18
|
+
return sessionModule(req, res, async () => resolve(req.session));
|
|
19
|
+
});
|
|
20
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var getUser = require('../firebase/discord/api/getUser.cjs');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Handles the Discord OAuth2 authentication for a user, stores user data in an in-memory cache,
|
|
7
|
+
* and manages the user's socket connection and disconnection events.
|
|
8
|
+
*
|
|
9
|
+
* @param {Object} socket - The socket object representing the user's connection.
|
|
10
|
+
* @param {Object} ioCache - The shared cache that stores user data and connections.
|
|
11
|
+
* @param {string} token - The OAuth2 token used to fetch user data from Discord.
|
|
12
|
+
* @returns {Promise<Object>} A promise that resolves with the user data and updates the cache.
|
|
13
|
+
*/
|
|
14
|
+
function discord(socket, ioCache, token) {
|
|
15
|
+
return new Promise((resolve, reject) => {
|
|
16
|
+
// Get Discord oAuth
|
|
17
|
+
getUser(token)
|
|
18
|
+
.then((user) => {
|
|
19
|
+
// Create Users Cache
|
|
20
|
+
if (!ioCache.users) ioCache.users = {};
|
|
21
|
+
if (!ioCache.ids) ioCache.ids = {};
|
|
22
|
+
if (typeof ioCache.totalUsers !== 'number') ioCache.totalUsers = 0;
|
|
23
|
+
|
|
24
|
+
// User Data
|
|
25
|
+
if (!ioCache.users[user.id]) {
|
|
26
|
+
ioCache.users[user.id] = {
|
|
27
|
+
ids: {},
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
ioCache.ids[socket.id] = user.id;
|
|
31
|
+
ioCache.totalUsers++;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Update Discord Data
|
|
35
|
+
ioCache.users[user.id].data = user;
|
|
36
|
+
|
|
37
|
+
// ID Data
|
|
38
|
+
ioCache.users[user.id].ids[socket.id] = { socket: socket };
|
|
39
|
+
|
|
40
|
+
// Disconnect
|
|
41
|
+
socket.on('disconnect', function () {
|
|
42
|
+
if (ioCache.users[user.id]) {
|
|
43
|
+
// IDs
|
|
44
|
+
delete ioCache.users[user.id].ids[socket.id];
|
|
45
|
+
delete ioCache.ids[socket.id];
|
|
46
|
+
|
|
47
|
+
if (Object.keys(ioCache.users[user.id].ids).length < 1) {
|
|
48
|
+
delete ioCache.users[user.id];
|
|
49
|
+
ioCache.totalUsers--;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// Complete
|
|
55
|
+
resolve(ioCache.users[user.id]);
|
|
56
|
+
})
|
|
57
|
+
.catch(reject);
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
module.exports = discord;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Handles the Discord OAuth2 authentication for a user, stores user data in an in-memory cache,
|
|
3
|
+
* and manages the user's socket connection and disconnection events.
|
|
4
|
+
*
|
|
5
|
+
* @param {Object} socket - The socket object representing the user's connection.
|
|
6
|
+
* @param {Object} ioCache - The shared cache that stores user data and connections.
|
|
7
|
+
* @param {string} token - The OAuth2 token used to fetch user data from Discord.
|
|
8
|
+
* @returns {Promise<Object>} A promise that resolves with the user data and updates the cache.
|
|
9
|
+
*/
|
|
10
|
+
export default function discord(socket: Object, ioCache: Object, token: string): Promise<Object>;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import getUser from '../firebase/discord/api/getUser.mjs';
|
|
2
|
+
/**
|
|
3
|
+
* Handles the Discord OAuth2 authentication for a user, stores user data in an in-memory cache,
|
|
4
|
+
* and manages the user's socket connection and disconnection events.
|
|
5
|
+
*
|
|
6
|
+
* @param {Object} socket - The socket object representing the user's connection.
|
|
7
|
+
* @param {Object} ioCache - The shared cache that stores user data and connections.
|
|
8
|
+
* @param {string} token - The OAuth2 token used to fetch user data from Discord.
|
|
9
|
+
* @returns {Promise<Object>} A promise that resolves with the user data and updates the cache.
|
|
10
|
+
*/
|
|
11
|
+
export default function discord(socket, ioCache, token) {
|
|
12
|
+
return new Promise((resolve, reject) => {
|
|
13
|
+
// Get Discord oAuth
|
|
14
|
+
getUser(token)
|
|
15
|
+
.then((user) => {
|
|
16
|
+
// Create Users Cache
|
|
17
|
+
if (!ioCache.users)
|
|
18
|
+
ioCache.users = {};
|
|
19
|
+
if (!ioCache.ids)
|
|
20
|
+
ioCache.ids = {};
|
|
21
|
+
if (typeof ioCache.totalUsers !== 'number')
|
|
22
|
+
ioCache.totalUsers = 0;
|
|
23
|
+
// User Data
|
|
24
|
+
if (!ioCache.users[user.id]) {
|
|
25
|
+
ioCache.users[user.id] = {
|
|
26
|
+
ids: {},
|
|
27
|
+
};
|
|
28
|
+
ioCache.ids[socket.id] = user.id;
|
|
29
|
+
ioCache.totalUsers++;
|
|
30
|
+
}
|
|
31
|
+
// Update Discord Data
|
|
32
|
+
ioCache.users[user.id].data = user;
|
|
33
|
+
// ID Data
|
|
34
|
+
ioCache.users[user.id].ids[socket.id] = { socket: socket };
|
|
35
|
+
// Disconnect
|
|
36
|
+
socket.on('disconnect', function () {
|
|
37
|
+
if (ioCache.users[user.id]) {
|
|
38
|
+
// IDs
|
|
39
|
+
delete ioCache.users[user.id].ids[socket.id];
|
|
40
|
+
delete ioCache.ids[socket.id];
|
|
41
|
+
if (Object.keys(ioCache.users[user.id].ids).length < 1) {
|
|
42
|
+
delete ioCache.users[user.id];
|
|
43
|
+
ioCache.totalUsers--;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
// Complete
|
|
48
|
+
resolve(ioCache.users[user.id]);
|
|
49
|
+
})
|
|
50
|
+
.catch(reject);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var index = require('./antiFlood/index.cjs');
|
|
4
|
+
var discord = require('./discord.cjs');
|
|
5
|
+
var cookieSession = require('./cookie-session.cjs');
|
|
6
|
+
|
|
7
|
+
const socketIo = {
|
|
8
|
+
antiFlood: index,
|
|
9
|
+
'cookie-session': cookieSession,
|
|
10
|
+
discord,
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
module.exports = socketIo;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export default socketIo;
|
|
2
|
+
declare const socketIo: {
|
|
3
|
+
antiFlood: typeof antiFlood;
|
|
4
|
+
'cookie-session': typeof cookieSession;
|
|
5
|
+
discord: typeof discord;
|
|
6
|
+
};
|
|
7
|
+
import * as antiFlood from './antiFlood/index.mjs';
|
|
8
|
+
import cookieSession from './cookie-session.mjs';
|
|
9
|
+
import discord from './discord.mjs';
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Randomly shuffles the elements of an array in place using the Fisher–Yates algorithm.
|
|
7
|
+
*
|
|
8
|
+
* This implementation ensures a uniform distribution of permutations.
|
|
9
|
+
* Original algorithm source: StackOverflow (link above).
|
|
10
|
+
*
|
|
11
|
+
* @param {string[]} items - The array to shuffle.
|
|
12
|
+
* @returns {string[]} The same array instance, now shuffled in place.
|
|
13
|
+
*/
|
|
14
|
+
function shuffleArray(items) {
|
|
15
|
+
let currentIndex = items.length,
|
|
16
|
+
randomIndex;
|
|
17
|
+
|
|
18
|
+
// While there remain elements to shuffle...
|
|
19
|
+
while (currentIndex !== 0) {
|
|
20
|
+
// Pick a remaining element...
|
|
21
|
+
randomIndex = Math.floor(Math.random() * currentIndex);
|
|
22
|
+
currentIndex--;
|
|
23
|
+
|
|
24
|
+
// And swap it with the current element.
|
|
25
|
+
[items[currentIndex], items[randomIndex]] = [items[randomIndex], items[currentIndex]];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return items;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
exports.shuffleArray = shuffleArray;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Randomly shuffles the elements of an array in place using the Fisher–Yates algorithm.
|
|
3
|
+
*
|
|
4
|
+
* This implementation ensures a uniform distribution of permutations.
|
|
5
|
+
* Original algorithm source: StackOverflow (link above).
|
|
6
|
+
*
|
|
7
|
+
* @param {string[]} items - The array to shuffle.
|
|
8
|
+
* @returns {string[]} The same array instance, now shuffled in place.
|
|
9
|
+
*/
|
|
10
|
+
export function shuffleArray(items: string[]): string[];
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array
|
|
2
|
+
/**
|
|
3
|
+
* Randomly shuffles the elements of an array in place using the Fisher–Yates algorithm.
|
|
4
|
+
*
|
|
5
|
+
* This implementation ensures a uniform distribution of permutations.
|
|
6
|
+
* Original algorithm source: StackOverflow (link above).
|
|
7
|
+
*
|
|
8
|
+
* @param {string[]} items - The array to shuffle.
|
|
9
|
+
* @returns {string[]} The same array instance, now shuffled in place.
|
|
10
|
+
*/
|
|
11
|
+
export function shuffleArray(items) {
|
|
12
|
+
let currentIndex = items.length, randomIndex;
|
|
13
|
+
// While there remain elements to shuffle...
|
|
14
|
+
while (currentIndex !== 0) {
|
|
15
|
+
// Pick a remaining element...
|
|
16
|
+
randomIndex = Math.floor(Math.random() * currentIndex);
|
|
17
|
+
currentIndex--;
|
|
18
|
+
// And swap it with the current element.
|
|
19
|
+
[items[currentIndex], items[randomIndex]] = [items[randomIndex], items[currentIndex]];
|
|
20
|
+
}
|
|
21
|
+
return items;
|
|
22
|
+
}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Calculates the time duration between the current time and a given time offset.
|
|
5
|
+
*
|
|
6
|
+
* @param {Date} timeData - The target time as a Date object.
|
|
7
|
+
* @param {string} [durationType='asSeconds'] - The type of duration to return. Can be 'asMilliseconds', 'asSeconds', 'asMinutes', 'asHours', 'asDays'.
|
|
8
|
+
* @param {Date|null} [now=null] - The current time as a Date object. Defaults to the current date and time if not provided.
|
|
9
|
+
* @returns {number|null} The calculated duration in the specified unit, or `null` if `timeData` is not provided.
|
|
10
|
+
*/
|
|
11
|
+
function getTimeDuration(timeData = new Date(), durationType = 'asSeconds', now = null) {
|
|
12
|
+
if (timeData instanceof Date) {
|
|
13
|
+
const currentTime = now instanceof Date ? now : new Date();
|
|
14
|
+
const diffMs = timeData - currentTime;
|
|
15
|
+
|
|
16
|
+
switch (durationType) {
|
|
17
|
+
case 'asMilliseconds':
|
|
18
|
+
return diffMs;
|
|
19
|
+
case 'asSeconds':
|
|
20
|
+
return diffMs / 1000;
|
|
21
|
+
case 'asMinutes':
|
|
22
|
+
return diffMs / (1000 * 60);
|
|
23
|
+
case 'asHours':
|
|
24
|
+
return diffMs / (1000 * 60 * 60);
|
|
25
|
+
case 'asDays':
|
|
26
|
+
return diffMs / (1000 * 60 * 60 * 24);
|
|
27
|
+
default:
|
|
28
|
+
return diffMs / 1000; // default to seconds
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Formats a custom timer string based on total seconds and a detail level.
|
|
37
|
+
* Includes proper reallocation of lower units into higher ones, ensuring consistent hierarchy.
|
|
38
|
+
*
|
|
39
|
+
* @param {number} totalSeconds - The total amount of seconds to convert.
|
|
40
|
+
* @param {'seconds'|'minutes'|'hours'|'days'|'months'|'years'} level - The highest level to calculate and display.
|
|
41
|
+
* @param {string} [format='{time}'] - Output template with placeholders like {years}, {months}, {days}, {hours}, {minutes}, {seconds}, {time}, {total}.
|
|
42
|
+
* @returns {string} The formatted timer string.
|
|
43
|
+
*/
|
|
44
|
+
function formatCustomTimer(totalSeconds, level = 'seconds', format = '{time}') {
|
|
45
|
+
totalSeconds = Math.max(0, Math.floor(totalSeconds));
|
|
46
|
+
|
|
47
|
+
const levels = ['seconds', 'minutes', 'hours', 'days', 'months', 'years'];
|
|
48
|
+
const index = levels.indexOf(level);
|
|
49
|
+
|
|
50
|
+
const include = {
|
|
51
|
+
years: index >= 5,
|
|
52
|
+
months: index >= 4,
|
|
53
|
+
days: index >= 3,
|
|
54
|
+
hours: index >= 2,
|
|
55
|
+
minutes: index >= 1,
|
|
56
|
+
seconds: index >= 0,
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const parts = {
|
|
60
|
+
years: include.years ? 0 : NaN,
|
|
61
|
+
months: include.months ? 0 : NaN,
|
|
62
|
+
days: include.days ? 0 : NaN,
|
|
63
|
+
hours: include.hours ? 0 : NaN,
|
|
64
|
+
minutes: include.minutes ? 0 : NaN,
|
|
65
|
+
seconds: include.seconds ? 0 : NaN,
|
|
66
|
+
total: NaN,
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
let remaining = totalSeconds;
|
|
70
|
+
|
|
71
|
+
if (include.years || include.months || include.days) {
|
|
72
|
+
const baseDate = new Date(1980, 0, 1);
|
|
73
|
+
const targetDate = new Date(baseDate.getTime() + remaining * 1000);
|
|
74
|
+
const workingDate = new Date(baseDate);
|
|
75
|
+
|
|
76
|
+
// Years
|
|
77
|
+
if (include.years) {
|
|
78
|
+
while (
|
|
79
|
+
new Date(workingDate.getFullYear() + 1, workingDate.getMonth(), workingDate.getDate()) <=
|
|
80
|
+
targetDate
|
|
81
|
+
) {
|
|
82
|
+
workingDate.setFullYear(workingDate.getFullYear() + 1);
|
|
83
|
+
parts.years++;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Months
|
|
88
|
+
if (include.months) {
|
|
89
|
+
while (
|
|
90
|
+
new Date(workingDate.getFullYear(), workingDate.getMonth() + 1, workingDate.getDate()) <=
|
|
91
|
+
targetDate
|
|
92
|
+
) {
|
|
93
|
+
workingDate.setMonth(workingDate.getMonth() + 1);
|
|
94
|
+
parts.months++;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Days
|
|
99
|
+
if (include.days) {
|
|
100
|
+
while (
|
|
101
|
+
new Date(workingDate.getFullYear(), workingDate.getMonth(), workingDate.getDate() + 1) <=
|
|
102
|
+
targetDate
|
|
103
|
+
) {
|
|
104
|
+
workingDate.setDate(workingDate.getDate() + 1);
|
|
105
|
+
parts.days++;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
remaining = Math.floor((targetDate - workingDate) / 1000);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (include.hours) {
|
|
113
|
+
parts.hours = Math.floor(remaining / 3600);
|
|
114
|
+
remaining %= 3600;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (include.minutes) {
|
|
118
|
+
parts.minutes = Math.floor(remaining / 60);
|
|
119
|
+
remaining %= 60;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (include.seconds) {
|
|
123
|
+
parts.seconds = remaining;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Calculate total
|
|
127
|
+
const totalMap = {
|
|
128
|
+
seconds: include.seconds ? totalSeconds : NaN,
|
|
129
|
+
minutes: include.minutes ? totalSeconds / 60 : NaN,
|
|
130
|
+
hours: include.hours ? totalSeconds / 3600 : NaN,
|
|
131
|
+
days: include.days ? totalSeconds / 86400 : NaN,
|
|
132
|
+
months: include.months ? parts.years * 12 + parts.months + (parts.days || 0) / 30 : NaN,
|
|
133
|
+
years: include.years ? parts.years + (parts.months || 0) / 12 + (parts.days || 0) / 365 : NaN,
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
parts.total = +(totalMap[level] || 0).toFixed(2).replace(/\.00$/, '');
|
|
137
|
+
|
|
138
|
+
const pad = (n) => (isNaN(n) ? 'NaN' : String(n).padStart(2, '0'));
|
|
139
|
+
|
|
140
|
+
const timeString = [
|
|
141
|
+
include.hours ? pad(parts.hours) : null,
|
|
142
|
+
include.minutes ? pad(parts.minutes) : null,
|
|
143
|
+
include.seconds ? pad(parts.seconds) : null,
|
|
144
|
+
]
|
|
145
|
+
.filter((v) => v !== null)
|
|
146
|
+
.join(':');
|
|
147
|
+
|
|
148
|
+
return format
|
|
149
|
+
.replace(/\{years\}/g, parts.years)
|
|
150
|
+
.replace(/\{months\}/g, parts.months)
|
|
151
|
+
.replace(/\{days\}/g, parts.days)
|
|
152
|
+
.replace(/\{hours\}/g, pad(parts.hours))
|
|
153
|
+
.replace(/\{minutes\}/g, pad(parts.minutes))
|
|
154
|
+
.replace(/\{seconds\}/g, pad(parts.seconds))
|
|
155
|
+
.replace(/\{time\}/g, timeString)
|
|
156
|
+
.replace(/\{total\}/g, parts.total)
|
|
157
|
+
.trim();
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Formats a duration (in seconds) into a timer string showing only hours, minutes, and seconds.
|
|
162
|
+
*
|
|
163
|
+
* Example output: "05:32:10"
|
|
164
|
+
*
|
|
165
|
+
* @param {number} seconds - The total number of seconds to format.
|
|
166
|
+
* @returns {string} The formatted timer string in "HH:MM:SS" format.
|
|
167
|
+
*/
|
|
168
|
+
function formatTimer(seconds) {
|
|
169
|
+
return formatCustomTimer(seconds, 'hours', '{hours}:{minutes}:{seconds}');
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Formats a duration (in seconds) into a timer string including days, hours, minutes, and seconds.
|
|
174
|
+
*
|
|
175
|
+
* Example output: "2d 05:32:10"
|
|
176
|
+
*
|
|
177
|
+
* @param {number} seconds - The total number of seconds to format.
|
|
178
|
+
* @returns {string} The formatted timer string in "Xd HH:MM:SS" format.
|
|
179
|
+
*/
|
|
180
|
+
function formatDayTimer(seconds) {
|
|
181
|
+
return formatCustomTimer(seconds, 'days', '{days}d {hours}:{minutes}:{seconds}');
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
exports.formatCustomTimer = formatCustomTimer;
|
|
185
|
+
exports.formatDayTimer = formatDayTimer;
|
|
186
|
+
exports.formatTimer = formatTimer;
|
|
187
|
+
exports.getTimeDuration = getTimeDuration;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calculates the time duration between the current time and a given time offset.
|
|
3
|
+
*
|
|
4
|
+
* @param {Date} timeData - The target time as a Date object.
|
|
5
|
+
* @param {string} [durationType='asSeconds'] - The type of duration to return. Can be 'asMilliseconds', 'asSeconds', 'asMinutes', 'asHours', 'asDays'.
|
|
6
|
+
* @param {Date|null} [now=null] - The current time as a Date object. Defaults to the current date and time if not provided.
|
|
7
|
+
* @returns {number|null} The calculated duration in the specified unit, or `null` if `timeData` is not provided.
|
|
8
|
+
*/
|
|
9
|
+
export function getTimeDuration(timeData?: Date, durationType?: string, now?: Date | null): number | null;
|
|
10
|
+
/**
|
|
11
|
+
* Formats a custom timer string based on total seconds and a detail level.
|
|
12
|
+
* Includes proper reallocation of lower units into higher ones, ensuring consistent hierarchy.
|
|
13
|
+
*
|
|
14
|
+
* @param {number} totalSeconds - The total amount of seconds to convert.
|
|
15
|
+
* @param {'seconds'|'minutes'|'hours'|'days'|'months'|'years'} level - The highest level to calculate and display.
|
|
16
|
+
* @param {string} [format='{time}'] - Output template with placeholders like {years}, {months}, {days}, {hours}, {minutes}, {seconds}, {time}, {total}.
|
|
17
|
+
* @returns {string} The formatted timer string.
|
|
18
|
+
*/
|
|
19
|
+
export function formatCustomTimer(totalSeconds: number, level?: "seconds" | "minutes" | "hours" | "days" | "months" | "years", format?: string): string;
|
|
20
|
+
/**
|
|
21
|
+
* Formats a duration (in seconds) into a timer string showing only hours, minutes, and seconds.
|
|
22
|
+
*
|
|
23
|
+
* Example output: "05:32:10"
|
|
24
|
+
*
|
|
25
|
+
* @param {number} seconds - The total number of seconds to format.
|
|
26
|
+
* @returns {string} The formatted timer string in "HH:MM:SS" format.
|
|
27
|
+
*/
|
|
28
|
+
export function formatTimer(seconds: number): string;
|
|
29
|
+
/**
|
|
30
|
+
* Formats a duration (in seconds) into a timer string including days, hours, minutes, and seconds.
|
|
31
|
+
*
|
|
32
|
+
* Example output: "2d 05:32:10"
|
|
33
|
+
*
|
|
34
|
+
* @param {number} seconds - The total number of seconds to format.
|
|
35
|
+
* @returns {string} The formatted timer string in "Xd HH:MM:SS" format.
|
|
36
|
+
*/
|
|
37
|
+
export function formatDayTimer(seconds: number): string;
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calculates the time duration between the current time and a given time offset.
|
|
3
|
+
*
|
|
4
|
+
* @param {Date} timeData - The target time as a Date object.
|
|
5
|
+
* @param {string} [durationType='asSeconds'] - The type of duration to return. Can be 'asMilliseconds', 'asSeconds', 'asMinutes', 'asHours', 'asDays'.
|
|
6
|
+
* @param {Date|null} [now=null] - The current time as a Date object. Defaults to the current date and time if not provided.
|
|
7
|
+
* @returns {number|null} The calculated duration in the specified unit, or `null` if `timeData` is not provided.
|
|
8
|
+
*/
|
|
9
|
+
export function getTimeDuration(timeData = new Date(), durationType = 'asSeconds', now = null) {
|
|
10
|
+
if (timeData instanceof Date) {
|
|
11
|
+
const currentTime = now instanceof Date ? now : new Date();
|
|
12
|
+
const diffMs = timeData - currentTime;
|
|
13
|
+
switch (durationType) {
|
|
14
|
+
case 'asMilliseconds':
|
|
15
|
+
return diffMs;
|
|
16
|
+
case 'asSeconds':
|
|
17
|
+
return diffMs / 1000;
|
|
18
|
+
case 'asMinutes':
|
|
19
|
+
return diffMs / (1000 * 60);
|
|
20
|
+
case 'asHours':
|
|
21
|
+
return diffMs / (1000 * 60 * 60);
|
|
22
|
+
case 'asDays':
|
|
23
|
+
return diffMs / (1000 * 60 * 60 * 24);
|
|
24
|
+
default:
|
|
25
|
+
return diffMs / 1000; // default to seconds
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Formats a custom timer string based on total seconds and a detail level.
|
|
32
|
+
* Includes proper reallocation of lower units into higher ones, ensuring consistent hierarchy.
|
|
33
|
+
*
|
|
34
|
+
* @param {number} totalSeconds - The total amount of seconds to convert.
|
|
35
|
+
* @param {'seconds'|'minutes'|'hours'|'days'|'months'|'years'} level - The highest level to calculate and display.
|
|
36
|
+
* @param {string} [format='{time}'] - Output template with placeholders like {years}, {months}, {days}, {hours}, {minutes}, {seconds}, {time}, {total}.
|
|
37
|
+
* @returns {string} The formatted timer string.
|
|
38
|
+
*/
|
|
39
|
+
export function formatCustomTimer(totalSeconds, level = 'seconds', format = '{time}') {
|
|
40
|
+
totalSeconds = Math.max(0, Math.floor(totalSeconds));
|
|
41
|
+
const levels = ['seconds', 'minutes', 'hours', 'days', 'months', 'years'];
|
|
42
|
+
const index = levels.indexOf(level);
|
|
43
|
+
const include = {
|
|
44
|
+
years: index >= 5,
|
|
45
|
+
months: index >= 4,
|
|
46
|
+
days: index >= 3,
|
|
47
|
+
hours: index >= 2,
|
|
48
|
+
minutes: index >= 1,
|
|
49
|
+
seconds: index >= 0,
|
|
50
|
+
};
|
|
51
|
+
const parts = {
|
|
52
|
+
years: include.years ? 0 : NaN,
|
|
53
|
+
months: include.months ? 0 : NaN,
|
|
54
|
+
days: include.days ? 0 : NaN,
|
|
55
|
+
hours: include.hours ? 0 : NaN,
|
|
56
|
+
minutes: include.minutes ? 0 : NaN,
|
|
57
|
+
seconds: include.seconds ? 0 : NaN,
|
|
58
|
+
total: NaN,
|
|
59
|
+
};
|
|
60
|
+
let remaining = totalSeconds;
|
|
61
|
+
if (include.years || include.months || include.days) {
|
|
62
|
+
const baseDate = new Date(1980, 0, 1);
|
|
63
|
+
const targetDate = new Date(baseDate.getTime() + remaining * 1000);
|
|
64
|
+
const workingDate = new Date(baseDate);
|
|
65
|
+
// Years
|
|
66
|
+
if (include.years) {
|
|
67
|
+
while (new Date(workingDate.getFullYear() + 1, workingDate.getMonth(), workingDate.getDate()) <=
|
|
68
|
+
targetDate) {
|
|
69
|
+
workingDate.setFullYear(workingDate.getFullYear() + 1);
|
|
70
|
+
parts.years++;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
// Months
|
|
74
|
+
if (include.months) {
|
|
75
|
+
while (new Date(workingDate.getFullYear(), workingDate.getMonth() + 1, workingDate.getDate()) <=
|
|
76
|
+
targetDate) {
|
|
77
|
+
workingDate.setMonth(workingDate.getMonth() + 1);
|
|
78
|
+
parts.months++;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
// Days
|
|
82
|
+
if (include.days) {
|
|
83
|
+
while (new Date(workingDate.getFullYear(), workingDate.getMonth(), workingDate.getDate() + 1) <=
|
|
84
|
+
targetDate) {
|
|
85
|
+
workingDate.setDate(workingDate.getDate() + 1);
|
|
86
|
+
parts.days++;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
remaining = Math.floor((targetDate - workingDate) / 1000);
|
|
90
|
+
}
|
|
91
|
+
if (include.hours) {
|
|
92
|
+
parts.hours = Math.floor(remaining / 3600);
|
|
93
|
+
remaining %= 3600;
|
|
94
|
+
}
|
|
95
|
+
if (include.minutes) {
|
|
96
|
+
parts.minutes = Math.floor(remaining / 60);
|
|
97
|
+
remaining %= 60;
|
|
98
|
+
}
|
|
99
|
+
if (include.seconds) {
|
|
100
|
+
parts.seconds = remaining;
|
|
101
|
+
}
|
|
102
|
+
// Calculate total
|
|
103
|
+
const totalMap = {
|
|
104
|
+
seconds: include.seconds ? totalSeconds : NaN,
|
|
105
|
+
minutes: include.minutes ? totalSeconds / 60 : NaN,
|
|
106
|
+
hours: include.hours ? totalSeconds / 3600 : NaN,
|
|
107
|
+
days: include.days ? totalSeconds / 86400 : NaN,
|
|
108
|
+
months: include.months ? parts.years * 12 + parts.months + (parts.days || 0) / 30 : NaN,
|
|
109
|
+
years: include.years ? parts.years + (parts.months || 0) / 12 + (parts.days || 0) / 365 : NaN,
|
|
110
|
+
};
|
|
111
|
+
parts.total = +(totalMap[level] || 0).toFixed(2).replace(/\.00$/, '');
|
|
112
|
+
const pad = (n) => (isNaN(n) ? 'NaN' : String(n).padStart(2, '0'));
|
|
113
|
+
const timeString = [
|
|
114
|
+
include.hours ? pad(parts.hours) : null,
|
|
115
|
+
include.minutes ? pad(parts.minutes) : null,
|
|
116
|
+
include.seconds ? pad(parts.seconds) : null,
|
|
117
|
+
]
|
|
118
|
+
.filter((v) => v !== null)
|
|
119
|
+
.join(':');
|
|
120
|
+
return format
|
|
121
|
+
.replace(/\{years\}/g, parts.years)
|
|
122
|
+
.replace(/\{months\}/g, parts.months)
|
|
123
|
+
.replace(/\{days\}/g, parts.days)
|
|
124
|
+
.replace(/\{hours\}/g, pad(parts.hours))
|
|
125
|
+
.replace(/\{minutes\}/g, pad(parts.minutes))
|
|
126
|
+
.replace(/\{seconds\}/g, pad(parts.seconds))
|
|
127
|
+
.replace(/\{time\}/g, timeString)
|
|
128
|
+
.replace(/\{total\}/g, parts.total)
|
|
129
|
+
.trim();
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Formats a duration (in seconds) into a timer string showing only hours, minutes, and seconds.
|
|
133
|
+
*
|
|
134
|
+
* Example output: "05:32:10"
|
|
135
|
+
*
|
|
136
|
+
* @param {number} seconds - The total number of seconds to format.
|
|
137
|
+
* @returns {string} The formatted timer string in "HH:MM:SS" format.
|
|
138
|
+
*/
|
|
139
|
+
export function formatTimer(seconds) {
|
|
140
|
+
return formatCustomTimer(seconds, 'hours', '{hours}:{minutes}:{seconds}');
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Formats a duration (in seconds) into a timer string including days, hours, minutes, and seconds.
|
|
144
|
+
*
|
|
145
|
+
* Example output: "2d 05:32:10"
|
|
146
|
+
*
|
|
147
|
+
* @param {number} seconds - The total number of seconds to format.
|
|
148
|
+
* @returns {string} The formatted timer string in "Xd HH:MM:SS" format.
|
|
149
|
+
*/
|
|
150
|
+
export function formatDayTimer(seconds) {
|
|
151
|
+
return formatCustomTimer(seconds, 'days', '{days}d {hours}:{minutes}:{seconds}');
|
|
152
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Checks the type of a given object or returns its type as a string.
|
|
5
|
+
*
|
|
6
|
+
* @param {*} obj - The object to check or identify.
|
|
7
|
+
* @param {string} [type] - Optional. If provided, checks whether the object matches this type (e.g., "object", "array", "string").
|
|
8
|
+
* @returns {boolean|string|null} - Returns `true` if the type matches, `false` if not,
|
|
9
|
+
* the type string if no type is provided, or `null` if the object is `undefined`.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* objType([], 'array'); // true
|
|
13
|
+
* objType({}, 'object'); // true
|
|
14
|
+
* objType('hello'); // "string"
|
|
15
|
+
* objType(undefined); // null
|
|
16
|
+
*/
|
|
17
|
+
function objType(obj, type) {
|
|
18
|
+
// Is Defined
|
|
19
|
+
if (typeof obj !== 'undefined') {
|
|
20
|
+
// Get Obj Type
|
|
21
|
+
const result = Object.prototype.toString.call(obj).toLowerCase();
|
|
22
|
+
// Check Obj Type
|
|
23
|
+
if (typeof type === 'string') {
|
|
24
|
+
if (result === `[object ${type}]`) return true;
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
// Send Result
|
|
28
|
+
return result.substring(8, result.length - 1);
|
|
29
|
+
}
|
|
30
|
+
// Nope
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Counts the number of elements in an array or the number of properties in an object.
|
|
36
|
+
*
|
|
37
|
+
* @param {*} obj - The array or object to count.
|
|
38
|
+
* @returns {number} - The count of items (array elements or object keys), or `0` if the input is neither an array nor an object.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* countObj([1, 2, 3]); // 3
|
|
42
|
+
* countObj({ a: 1, b: 2 }); // 2
|
|
43
|
+
* countObj('not an object'); // 0
|
|
44
|
+
*/
|
|
45
|
+
function countObj(obj) {
|
|
46
|
+
// Is Array
|
|
47
|
+
if (Array.isArray(obj)) return obj.length;
|
|
48
|
+
// Object
|
|
49
|
+
if (objType(obj, 'object')) return Object.keys(obj).length;
|
|
50
|
+
// Nothing
|
|
51
|
+
return 0;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
exports.countObj = countObj;
|
|
55
|
+
exports.objType = objType;
|