@nextcloud/vue
Version:
Nextcloud vue components
1 lines • 2.39 kB
Source Map (JSON)
{"version":3,"file":"usernameToColor.cjs","sources":["../../src/functions/usernameToColor/index.js"],"sourcesContent":["/**\n * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors\n * SPDX-License-Identifier: AGPL-3.0-or-later\n */\n\nimport md5 from 'md5'\nimport { GenColors } from '../../utils/GenColors.js'\n\n/**\n * Generate a color from a username\n * Originally taken from https://github.com/nextcloud/server/blob/master/core/js/placeholder.js\n *\n * @param {string} username Display name or user id to generate from\n * @return {{ r: number, g: number, b: number }} the RGB color\n */\nexport function usernameToColor(username) {\n\t// Normalize hash\n\tlet hash = username.toLowerCase()\n\n\t// Already a md5 hash?\n\tif (hash.match(/^([0-9a-f]{4}-?){8}$/) === null) {\n\t\thash = md5(hash)\n\t}\n\n\thash = hash.replace(/[^0-9a-f]/g, '')\n\n\tconst steps = 6\n\tconst finalPalette = GenColors(steps)\n\n\t/**\n\t * Convert a string to an integer evenly\n\t *\n\t * @param {string} hash The hash to convert\n\t * @param {number} maximum Largest number allowed\n\t */\n\tfunction hashToInt(hash, maximum) {\n\t\tlet finalInt = 0\n\t\tconst result = []\n\n\t\t// Splitting evenly the string\n\t\tfor (let i = 0; i < hash.length; i++) {\n\t\t\t// chars in md5 goes up to f, hex:16\n\t\t\tresult.push(parseInt(hash.charAt(i), 16) % 16)\n\t\t}\n\n\t\t// Adds up all results\n\t\tfor (const j in result) {\n\t\t\tfinalInt += result[j]\n\t\t}\n\n\t\t// chars in md5 goes up to f, hex:16\n\t\t// make sure we're always using int in our operation\n\t\treturn parseInt(parseInt(finalInt, 10) % maximum, 10)\n\t}\n\treturn finalPalette[hashToInt(hash, steps * 3)]\n}\n\n/**\n * @deprecated use the named export of `usernameToColor` instead\n */\nexport default usernameToColor\n"],"names":["md5","GenColors","hash"],"mappings":";;;;;;AAeO,SAAS,gBAAgB,UAAU;AAEzC,MAAI,OAAO,SAAS,YAAW;AAG/B,MAAI,KAAK,MAAM,sBAAsB,MAAM,MAAM;AAChD,WAAOA,aAAAA,QAAI,IAAI;AAAA,EAChB;AAEA,SAAO,KAAK,QAAQ,cAAc,EAAE;AAEpC,QAAM,QAAQ;AACd,QAAM,eAAeC,UAAAA,UAAU,KAAK;AAQpC,WAAS,UAAUC,OAAM,SAAS;AACjC,QAAI,WAAW;AACf,UAAM,SAAS,CAAA;AAGf,aAAS,IAAI,GAAG,IAAIA,MAAK,QAAQ,KAAK;AAErC,aAAO,KAAK,SAASA,MAAK,OAAO,CAAC,GAAG,EAAE,IAAI,EAAE;AAAA,IAC9C;AAGA,eAAW,KAAK,QAAQ;AACvB,kBAAY,OAAO,CAAC;AAAA,IACrB;AAIA,WAAO,SAAS,SAAS,UAAU,EAAE,IAAI,SAAS,EAAE;AAAA,EACrD;AACA,SAAO,aAAa,UAAU,MAAM,QAAQ,CAAC,CAAC;AAC/C;;;"}