feat(new tool): Snowflake ID extractor

This commit is contained in:
antegral 2024-07-22 20:59:54 +09:00
parent 76a19d218d
commit 6540db35de
No known key found for this signature in database
GPG key ID: 5B30DAC88CB6890D
6 changed files with 149 additions and 3 deletions

View file

@ -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'),
});

View file

@ -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));
});
});

View file

@ -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());
}

View file

@ -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>