From: Muhammad Rizki <[email protected]>
To: Ammar Faizi <[email protected]>
Cc: Muhammad Rizki <[email protected]>,
Alviro Iskandar Setiawan <[email protected]>,
Irvan Malik Azantha <[email protected]>,
Memet Zx <[email protected]>,
GNU/Weeb Mailing List <[email protected]>
Subject: [PATCH v1 04/13] feat(lib): add lib functions file
Date: Tue, 3 Oct 2023 09:51:37 +0700 [thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>
This file contains helper functions, and there are no changes from the
previous version. We've simply added some type annotations for
TypeScript support.
Signed-off-by: Muhammad Rizki <[email protected]>
---
src/lib/functions.ts | 95 ++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 95 insertions(+)
create mode 100644 src/lib/functions.ts
diff --git a/src/lib/functions.ts b/src/lib/functions.ts
new file mode 100644
index 0000000..0357e6b
--- /dev/null
+++ b/src/lib/functions.ts
@@ -0,0 +1,95 @@
+import type { GitHubOrgsAPIResponseType, RecentMessagesReturnType } from "$types";
+import { API_URL } from "./constants";
+
+
+export const getOrgMembers = async (): Promise<GitHubOrgsAPIResponseType[]> => {
+ const response = await fetch("https://api.github.com/orgs/gnuweeb/members");
+ const data: GitHubOrgsAPIResponseType[] = await response.json();
+
+ data.sort((
+ left: GitHubOrgsAPIResponseType,
+ right: GitHubOrgsAPIResponseType
+ ) => {
+ const usernameA = left.login.toLowerCase();
+ const usernameB = right.login.toLowerCase();
+ if (usernameA < usernameB) return -1;
+ if (usernameA > usernameB) return 1;
+ return 0;
+ });
+
+ return data;
+}
+
+export const getRecentMessages = async (): Promise<RecentMessagesReturnType[] | []> => {
+ try {
+ const response = await fetch(API_URL + "/zxc.php?action=get_messages&chat_id=-1001483770714&limit=50");
+ const { data } = (await response.json()).result;
+
+ const transformedData = data.map((item: any) => {
+ return {
+ user_id: item[0],
+ username: item[1],
+ first_name: item[2],
+ last_name: item[3],
+ user_photo: item[4],
+ message_id: item[5],
+ reply_to_message_id: item[6],
+ message_type: item[7],
+ text: item[8],
+ text_entities: item[9] ? JSON.parse(item[9]) : null,
+ file: item[10],
+ date: item[11]
+ };
+ });
+
+ return transformedData.reverse();
+ } catch {
+ return [];
+ }
+}
+
+const hashCode = (name: string) => {
+ var hash = 0;
+ for (var i = 0; i < name.length; i++) {
+ var character = name.charCodeAt(i);
+ hash = ((hash << 5) - hash) + character;
+ }
+ return Math.abs(hash);
+}
+
+export const getFixedRandomColor = (name: string) => {
+ const colorData: Record<string, string> = {
+ red: "text-red-400 border-red-400",
+ yellow: "text-yellow-400 border-yellow-400",
+ blue: "text-blue-400 border-blue-400",
+ sky: "text-sky-400 border-sky-400",
+ purple: "text-purple-400 border-purple-400",
+ orange: "text-orange-400 border-orange-400",
+ amber: "text-amber-400 border-amber-400",
+ lime: "text-lime-400 border-lime-400",
+ green: "text-green-400 border-green-400",
+ emerald: "text-emerald-400 border-emerald-400",
+ teal: "text-teal-400 border-teal-400",
+ cyan: "text-cyan-400 border-cyan-400",
+ indigo: "text-indigo-400 border-indigo-400",
+ violet: "text-violet-400 border-violet-400",
+ fuchsia: "text-fuchsia-400 border-fuchsia-400",
+ pink: "text-pink-400 border-pink-400",
+ rose: "text-rose-400 border-rose-400",
+ };
+ const colorNames = Object.keys(colorData);
+ return colorData[colorNames[hashCode(name) % colorNames.length]].split(" ");
+}
+
+export const cleanMessageText = (text: string) => {
+ return text.replaceAll(/\n/g, " ");
+}
+
+export const setupUserName = (first: string, last: string | null) => {
+ const firstName = first.trimEnd();
+ let lastName = "";
+ if(last !== null) {
+ lastName = " " + last.trimEnd();
+ }
+ return [ firstName, lastName ];
+}
--
Muhammad Rizki
next prev parent reply other threads:[~2023-10-03 2:52 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-03 2:51 [PATCH v1 00/13] GNU/Weeb Web Migration to SvelteKit Muhammad Rizki
2023-10-03 2:51 ` [PATCH v1 01/13] refactor: migrate: migrate to sveltekit framework Muhammad Rizki
2023-10-03 2:51 ` [PATCH v1 02/13] feat(app): add app types file Muhammad Rizki
2023-10-03 2:51 ` [PATCH v1 03/13] feat(lib): add API_URL and STORAGE_URL constant variable Muhammad Rizki
2023-10-03 2:51 ` Muhammad Rizki [this message]
2023-10-03 2:51 ` [PATCH v1 05/13] feat(lib): add newly created lib constants variables and functions Muhammad Rizki
2023-10-03 2:51 ` [PATCH v1 06/13] feat(lib/functions): add getRepliedMessage() function Muhammad Rizki
2023-10-03 2:51 ` [PATCH v1 07/13] feat(lib/functions): add dateFormat() function Muhammad Rizki
2023-10-03 2:51 ` [PATCH v1 08/13] feat(components): add <OrganizationMembers/> component Muhammad Rizki
2023-10-03 2:51 ` [PATCH v1 09/13] feat(components): add <RecentMessages/> component Muhammad Rizki
2023-10-03 2:51 ` [PATCH v1 10/13] feat(routes): add +page.server.ts Muhammad Rizki
2023-10-03 2:51 ` [PATCH v1 11/13] feat(routes/page): add +page.svelte Muhammad Rizki
2023-10-03 2:51 ` [PATCH v1 12/13] chore(README): update README.md Muhammad Rizki
2023-10-05 19:29 ` Ammar Faizi
2023-10-07 5:36 ` Muhammad Rizki
2023-10-03 2:51 ` [PATCH v1 13/13] chore: remove unused files Muhammad Rizki
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
[email protected] \
[email protected] \
[email protected] \
[email protected] \
[email protected] \
[email protected] \
[email protected] \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox