mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-20 21:23:08 +03:00
Merge branch 'up/feat/snowflake' into chore/all-my-stuffs
This commit is contained in:
commit
57281b6331
5 files changed
+100
No files matched your search
@@ -23,6 +23,7 @@ import { tool as energyConverter } from './energy-converter';
|
||||
import { tool as areaConverter } from './area-converter';
|
||||
import { tool as angleConverter } from './angle-converter';
|
||||
import { tool as floatingPointNumberConverter } from './floating-point-number-converter';
|
||||
import { tool as snowflakeIdExtractor } from './snowflake-id-extractor';
|
||||
import { tool as emailNormalizer } from './email-normalizer';
|
||||
import { tool as bounceParser } from './bounce-parser';
|
||||
import { tool as codeHighlighter } from './code-highlighter';
|
||||
@@ -361,6 +362,7 @@ export const toolsByCategory: ToolCategory[] = [
|
||||
jsonDiff,
|
||||
softwareLicenceCompatibility,
|
||||
safelinkDecoder,
|
||||
snowflakeIdExtractor,
|
||||
apiTester,
|
||||
bounceParser,
|
||||
colorWheel,
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import { Id } from '@vicons/tabler';
|
||||
import { defineTool } from '../tool';
|
||||
|
||||
export const tool = defineTool({
|
||||
name: 'Snowflake ID extractor',
|
||||
path: '/snowflake-id-extractor',
|
||||
description: 'Extract timestamp, machine ID, and sequence number from a Snowflake ID',
|
||||
keywords: ['snowflake', 'id', 'extractor'],
|
||||
component: () => import('./snowflake-id-extractor.vue'),
|
||||
icon: Id,
|
||||
createdAt: new Date('2024-07-22'),
|
||||
});
|
||||
@@ -0,0 +1,14 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { extractId, extractMachineId, extractTimestamp } from './snowflake-id-extractor.service';
|
||||
|
||||
describe('snowflake-id-extractor', () => {
|
||||
it('extract id from Snowflake ID', () => {
|
||||
expect(extractId(1263785187301658678n)).toBe(54);
|
||||
});
|
||||
it('extract machine id from Snowflake ID', () => {
|
||||
expect(extractMachineId(1263785187301658678n)).toBe(65);
|
||||
});
|
||||
it('extract timestamp from Snowflake ID', () => {
|
||||
expect(extractTimestamp(1263785187301658678n, new Date(1420070400000))).toStrictEqual(new Date(1721380268646));
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,15 @@
|
||||
export function extractId(id: bigint) {
|
||||
return Number(id & 4095n);
|
||||
}
|
||||
|
||||
export function extractMachineId(id: bigint) {
|
||||
return Number((id >> 12n) & 127n);
|
||||
}
|
||||
|
||||
export function extractTimestamp(id: bigint, epoch: bigint | Date) {
|
||||
return new Date(Number((id >> 22n) + getTimestamp(epoch)));
|
||||
}
|
||||
|
||||
function getTimestamp(time: bigint | Date) {
|
||||
return typeof time === 'bigint' ? time : BigInt(new Date(time).getTime());
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<script setup lang="ts">
|
||||
import { extractId, extractMachineId, extractTimestamp } from './snowflake-id-extractor.service';
|
||||
|
||||
const inputId = ref('1263785187301658678');
|
||||
const inputEpoch = ref('');
|
||||
|
||||
const inputProps = {
|
||||
'labelPosition': 'left',
|
||||
'labelWidth': '170px',
|
||||
'labelAlign': 'right',
|
||||
'readonly': true,
|
||||
'mb-2': '',
|
||||
} as const;
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div>
|
||||
<c-card>
|
||||
<c-input-text v-model:value="inputId" label="Snowflake ID" placeholder="Put Snowflake ID here (eg. 1263785187301658678)" label-position="left" label-width="110px" mb-2 label-align="right" />
|
||||
<c-input-text v-model:value="inputEpoch" label="Epoch" placeholder="Put Epoch Timestamp here (optional, eg. 1420070400000)" label-position="left" label-width="110px" mb-2 label-align="right" />
|
||||
|
||||
<n-divider />
|
||||
|
||||
<InputCopyable
|
||||
label="Local date"
|
||||
v-bind="inputProps"
|
||||
:value="inputEpoch ? new Date(extractTimestamp(BigInt(inputId.valueOf()), BigInt(inputEpoch.valueOf()))).toLocaleString() : ''"
|
||||
placeholder="Epoch Timestamp will be here..."
|
||||
label-position="left" label-width="110px" mb-2 label-align="right"
|
||||
/>
|
||||
|
||||
<InputCopyable
|
||||
label="Timestamp"
|
||||
v-bind="inputProps"
|
||||
:value="inputEpoch ? new Date(extractTimestamp(BigInt(inputId.valueOf()), BigInt(inputEpoch.valueOf()))).getTime() : ''"
|
||||
placeholder="Epoch Timestamp will be here..."
|
||||
label-position="left" label-width="110px" mb-2 label-align="right"
|
||||
/>
|
||||
|
||||
<InputCopyable
|
||||
label="Machine ID"
|
||||
v-bind="inputProps"
|
||||
:value="extractMachineId(BigInt(inputId.valueOf()))"
|
||||
placeholder="Machine ID will be here..."
|
||||
label-position="left" label-width="110px" mb-2 label-align="right"
|
||||
/>
|
||||
|
||||
<InputCopyable
|
||||
label="Sequence"
|
||||
v-bind="inputProps"
|
||||
:value="extractId(BigInt(inputId.valueOf()))"
|
||||
placeholder="Sequence number will be here..."
|
||||
label-position="left" label-width="110px" mb-2 label-align="right"
|
||||
/>
|
||||
</c-card>
|
||||
</div>
|
||||
</template>
|
||||
Reference in new issue
Block a user