Skip to content

Commit

Permalink
fix: move generateHash to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
krosy1337 committed Oct 23, 2023
1 parent 2d2599c commit 4769cdb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import crc32 from 'crc-32';
import cn from 'bem-cn-lite';

import DataTable, {type Column} from '@gravity-ui/react-data-table';

import type {KeyValueRow} from '../../../../types/api/query';
import {formatDateTime, formatNumber} from '../../../../utils/dataFormatters/dataFormatters';
import {generateHash} from '../../../../utils/generateHash';
import {
TruncatedQuery,
OneLineQueryWithPopover,
Expand Down Expand Up @@ -81,10 +81,7 @@ const oneLineQueryTextColumn: Column<KeyValueRow> = {

const queryHashColumn: Column<KeyValueRow> = {
name: TOP_QUERIES_COLUMNS_IDS.QueryHash,
render: ({row}) =>
// We use unsigned right shift operator (>>>) to avoid negative values
// eslint-disable-next-line no-bitwise
(crc32.str(String(row.QueryText)) >>> 0).toString(16).toUpperCase().padStart(8, '0'),
render: ({row}) => generateHash(String(row.QueryText)),
width: 130,
sortable: false,
};
Expand Down
11 changes: 11 additions & 0 deletions src/utils/generateHash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import crc32 from 'crc-32';

export const generateHash = (value: string) => {
// 1. crc32.str(value) - generate crc32 hash
// 2. (>>>) - use unsigned right shift operator (>>>) to avoid negative values
// 3. toString(16) - convert hash to hex format
// 4. toUpperCase() - convert hash to uppercase
// 5. padStart(8, '0') - fill hash with leading zeros if hash length < 8
// eslint-disable-next-line no-bitwise
return (crc32.str(value) >>> 0).toString(16).toUpperCase().padStart(8, '0');
};

0 comments on commit 4769cdb

Please sign in to comment.