Skip to content
This repository has been archived by the owner on Dec 15, 2023. It is now read-only.

Commit

Permalink
fix: change to fnv algo
Browse files Browse the repository at this point in the history
  • Loading branch information
braddialpad authored and juliodialpad committed May 19, 2023
1 parent 469fe05 commit f6e01a1
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 44 deletions.
25 changes: 4 additions & 21 deletions common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
VALIDATION_MESSAGE_TYPES,
} from './constants';
import Vue from 'vue';
import fnv from 'fnv-plus';

let UNIQUE_ID_COUNTER = 0;
let TIMER;
Expand All @@ -30,10 +31,10 @@ export function getUniqueString (prefix = DEFAULT_PREFIX) {
* based on that string.
* @returns {*} - the random element
*/
export async function getRandomElement (array, seed) {
export function getRandomElement (array, seed) {
if (seed) {
const hash = await hashSha256(seed);
return array[hash % array.length];
const hash = fnv.hash(seed);
return array[hash.value % array.length];
} else {
return array[getRandomInt(array.length)];
}
Expand Down Expand Up @@ -157,23 +158,6 @@ export const pascalCaseToKebabCase = (string) => {
.replace(/^-/, '');
};

/**
* Hash a string using SHA-256
* @param {string} the string to hash
* @returns {Number} the number that was hashed from the string
*/
export function hashSha256 (string) {
const utf8 = new TextEncoder().encode(string);
return crypto.subtle.digest('SHA-256', utf8).then((hashBuffer) => {
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashHex = hashArray
.map((bytes) => bytes.toString(16).padStart(2, '0'))
.join('');

return Number('0x' + hashHex);
});
}

/*
* Set's a global timer to debounce the execution of a function.
* @param { object } func - the function that is going to be called after timeout
Expand Down Expand Up @@ -215,7 +199,6 @@ export default {
htmlFragment,
flushPromises,
kebabCaseToPascalCase,
hashSha256,
debounce,
isOutOfViewPort,
};
26 changes: 3 additions & 23 deletions components/avatar/avatar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ export default {
slottedInitials: '',
formattedInitials: '',
initializing: false,
internalColor: '',
};
},
Expand All @@ -219,7 +218,7 @@ export default {
this.avatarClass,
{
'd-avatar--group': this.showGroup,
[`d-avatar--color-${this.internalColor}`]: this.kind !== 'icon' && this.internalColor,
[`d-avatar--color-${this.getColor()}`]: this.kind !== 'icon',
},
];
},
Expand Down Expand Up @@ -255,24 +254,6 @@ export default {
},
},
watch: {
color: {
handler: function () {
this.getColor();
},
immediate: true,
},
seed: {
handler: function () {
this.getColor();
},
immediate: true,
},
},
mounted () {
this.init();
},
Expand Down Expand Up @@ -344,9 +325,8 @@ export default {
return element?.tagName?.toUpperCase() === 'IMG';
},
async getColor () {
const color = this.color ?? await getRandomElement(AVATAR_COLORS, this.seed);
this.internalColor = color;
getColor () {
return this.color ?? getRandomElement(AVATAR_COLORS, this.seed);
},
validateImageAttrsPresence () {
Expand Down
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"@tiptap/vue-2": "^2.0.2",
"emoji-regex": "^10.2.1",
"emoji-toolkit": "^6.6.0",
"fnv-plus": "^1.3.1",
"tippy.js": "^6.3.7"
},
"devDependencies": {
Expand Down

0 comments on commit f6e01a1

Please sign in to comment.