UNPKG

@nextcloud/vue

Version:
1 lines 1.73 kB
{"version":3,"file":"index.mjs","sources":["../../../src/functions/usernameToColor/index.ts"],"sourcesContent":["/**\n * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors\n * SPDX-License-Identifier: AGPL-3.0-or-later\n */\n\nimport type { Color } from '../../utils/colors.ts'\n\nimport { Md5 } from 'ts-md5'\nimport { generatePalette } from '../../utils/colors.ts'\n\n/**\n * Create a simple hash from a string\n *\n * @param str - The string to hash\n */\nfunction hashCode(str: string): number {\n\tlet hash = str\n\n\t// Hash a given string, if it is not md5 hash already\n\tif (str.match(/^([0-9a-f]{4}-?){8}$/) === null) {\n\t\thash = Md5.hashStr(str)\n\t}\n\n\thash = hash.replace(/[^0-9a-f]/g, '')\n\n\tlet finalInt = 0\n\n\tfor (let i = 0; i < hash.length; i++) {\n\t\t// chars in md5 are [0-9a-f] (base-16)\n\t\tfinalInt += parseInt(hash.charAt(i), 16)\n\t}\n\n\treturn finalInt\n}\n\n/**\n * Generate a color from a username\n *\n * @param username - Display name or user id to generate from\n * @return The RGB color\n */\nexport function usernameToColor(username: string): Color {\n\tconst steps = 6\n\tconst finalPalette = generatePalette(steps)\n\tconst hash = hashCode(username.toLocaleLowerCase())\n\n\treturn finalPalette[hash % finalPalette.length]!\n}\n"],"names":[],"mappings":";;AAeA,SAAS,SAAS,KAAqB;AACtC,MAAI,OAAO;AAGX,MAAI,IAAI,MAAM,sBAAsB,MAAM,MAAM;AAC/C,WAAO,IAAI,QAAQ,GAAG;AAAA,EACvB;AAEA,SAAO,KAAK,QAAQ,cAAc,EAAE;AAEpC,MAAI,WAAW;AAEf,WAAS,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK;AAErC,gBAAY,SAAS,KAAK,OAAO,CAAC,GAAG,EAAE;AAAA,EACxC;AAEA,SAAO;AACR;AAQO,SAAS,gBAAgB,UAAyB;AACxD,QAAM,QAAQ;AACd,QAAM,eAAe,gBAAgB,KAAK;AAC1C,QAAM,OAAO,SAAS,SAAS,kBAAA,CAAmB;AAElD,SAAO,aAAa,OAAO,aAAa,MAAM;AAC/C;"}