From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-2.2 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,URIBL_BLOCKED, URIBL_DBL_BLOCKED_OPENDNS autolearn=ham autolearn_force=no version=3.4.6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1695548244; bh=d21QGWuZ0RM1ETvLG300XFW4CLGZHGsT6WFV9q5CSC4=; h=Date:Subject:To:Cc:References:From:In-Reply-To; b=pnYPJyN6gWbtxv6AgjNY0FmVjWnEueT6qi+AnLdLpmztvWRkwbPpD8C2p/Y1G0F/1 e11i8U6FBy9zJYYF0yH3uavUIk3bNindVpLfky4DU/PztYrnNujgCwdDdnDbf+nPoX AeuDiuM/nClCukbPUvW9RR3KWUjucm7C9dsqkZSQU4IU5NQddsaSLiZ0XHG6iWwNGm 4M+qeP5ZRxS8F9M8Cq33P8GaOgJNkE/2zon2HtvGSKPZhYC9qTINNjQNzlE04LHNEn qLNbkhyZa38CEUGk4t6x7N8mGpgLGIiNuSuecUVXs4s2m3MAW9j8vHhc2FKxIsl+xN dEirsKrbhcpHg== Received: from [192.168.1.5] (unknown [101.128.125.146]) by gnuweeb.org (Postfix) with ESMTPSA id 30B3924B67E; Sun, 24 Sep 2023 16:37:22 +0700 (WIB) Message-ID: <4d58feac-b9e3-71ab-bfec-01115496a5e6@gnuweeb.org> Date: Sun, 24 Sep 2023 16:37:19 +0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.6.1 Subject: Re: [PATCH v2 11/11] feat(page): implement chat bubble message rendering To: Ammar Faizi Cc: Alviro Iskandar Setiawan , Arthur Lapz , Memet Zx , GNU/Weeb Mailing List References: <20230923091514.1835-1-kiizuha@gnuweeb.org> <20230923091514.1835-12-kiizuha@gnuweeb.org> Content-Language: en-US From: Muhammad Rizki In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: On 24/09/2023 02.26, Ammar Faizi wrote: > On Sat, Sep 23, 2023 at 04:15:14PM +0700, Muhammad Rizki wrote: >> + function hashCode(name) { >> + var hash = 0; >> + for (var i = 0; i < name.length; i++) { >> + var character = name.charCodeAt(i); >> + hash = ((hash<<5)-hash)+character; >> + hash = hash & hash; > > This is useless. `hash & hash` is always equal to `hash`. What are you even doing? Yeah, didn't focus on that thing, I was copy & paste from the reference. > >> + } >> + return Math.abs(hash); >> + } >> + >> + function getFixedRandomColor(name) { >> + const textColors = [ >> + "text-red-400", "text-yellow-400", >> + "text-blue-400", "text-sky-400", >> + "text-purple-400", "text-orange-400", >> + "text-amber-400", "text-lime-400", >> + "text-green-400", "text-emerald-400", >> + "text-teal-400", "text-cyan-400", >> + "text-indigo-400", "text-violet-400", >> + "text-fuchsia-400", "text-pink-400", >> + "text-rose-400" >> + ]; >> + const borderColors = [ >> + "border-red-400", "border-yellow-400", >> + "border-blue-400", "border-sky-400", >> + "border-purple-400", "border-orange-400", >> + "border-amber-400", "border-lime-400", >> + "border-green-400", "border-emerald-400", >> + "border-teal-400", "border-cyan-400", >> + "border-indigo-400", "border-violet-400", >> + "border-fuchsia-400", "border-pink-400", >> + "border-rose-400" >> + ]; >> + >> + const indexColor = hashCode(name) % textColors.length; >> + return [ textColors[indexColor], borderColors[indexColor] ]; >> + } > > Don't repeat yourself (DRY). Simplify it like: > ``` > function getFixedRandomColor(name) { > const colors = [ > "red", "yellow", "blue", "sky", "purple", "orange", > "amber", "lime", "green", "emerald", "teal", "cyan", > "indigo", "violet", "fuchsia", "pink", "rose" > ]; > > const c = colors[hashCode(name) % colors.length]; > return [ > "text-" + c + "-400", > "border-" + c + "-400", > ]; > } > ``` > > [ Untested, may contain a mistake, but you get the idea. ] > Yeah, it worked. I tought this doesn't work because tailwind doesn't support dynamic class names, but this is worked so I will use this.