mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-05-04 05:19:12 -04:00
Merge 382a5cc696
into 87984e2081
This commit is contained in:
commit
906b712d04
6 changed files with 138 additions and 0 deletions
3
components.d.ts
vendored
3
components.d.ts
vendored
|
@ -129,11 +129,14 @@ declare module '@vue/runtime-core' {
|
|||
MenuLayout: typeof import('./src/components/MenuLayout.vue')['default']
|
||||
MetaTagGenerator: typeof import('./src/tools/meta-tag-generator/meta-tag-generator.vue')['default']
|
||||
MimeTypes: typeof import('./src/tools/mime-types/mime-types.vue')['default']
|
||||
MongoObjectidConverter: typeof import('./src/tools/mongo-objectid-converter/mongo-objectid-converter.vue')['default']
|
||||
NavbarButtons: typeof import('./src/components/NavbarButtons.vue')['default']
|
||||
NButton: typeof import('naive-ui')['NButton']
|
||||
NCode: typeof import('naive-ui')['NCode']
|
||||
NCollapseTransition: typeof import('naive-ui')['NCollapseTransition']
|
||||
NConfigProvider: typeof import('naive-ui')['NConfigProvider']
|
||||
NDatePicker: typeof import('naive-ui')['NDatePicker']
|
||||
NDivider: typeof import('naive-ui')['NDivider']
|
||||
NEllipsis: typeof import('naive-ui')['NEllipsis']
|
||||
NForm: typeof import('naive-ui')['NForm']
|
||||
NFormItem: typeof import('naive-ui')['NFormItem']
|
||||
|
|
|
@ -7,6 +7,7 @@ import { tool as asciiTextDrawer } from './ascii-text-drawer';
|
|||
|
||||
import { tool as textToUnicode } from './text-to-unicode';
|
||||
import { tool as safelinkDecoder } from './safelink-decoder';
|
||||
import { tool as mongoObjectidConverter } from './mongo-objectid-converter';
|
||||
import { tool as xmlToJson } from './xml-to-json';
|
||||
import { tool as jsonToXml } from './json-to-xml';
|
||||
import { tool as markdownToHtml } from './markdown-to-html';
|
||||
|
@ -156,6 +157,7 @@ export const toolsByCategory: ToolCategory[] = [
|
|||
xmlFormatter,
|
||||
yamlViewer,
|
||||
emailNormalizer,
|
||||
mongoObjectidConverter,
|
||||
],
|
||||
},
|
||||
{
|
||||
|
|
12
src/tools/mongo-objectid-converter/index.ts
Normal file
12
src/tools/mongo-objectid-converter/index.ts
Normal file
|
@ -0,0 +1,12 @@
|
|||
import { Database } from '@vicons/tabler';
|
||||
import { defineTool } from '../tool';
|
||||
|
||||
export const tool = defineTool({
|
||||
name: 'MongoDB ObjectId Converter',
|
||||
path: '/mongo-objectid-converter',
|
||||
description: 'Convert between MongoDB ObjectId and internal timestamp',
|
||||
keywords: ['mongo', 'objectid', 'converter', 'timestamp'],
|
||||
component: () => import('./mongo-objectid-converter.vue'),
|
||||
icon: Database,
|
||||
createdAt: new Date('2024-08-15'),
|
||||
});
|
|
@ -0,0 +1,30 @@
|
|||
import { describe, expect, it } from 'vitest';
|
||||
import { dateFromObjectId, generateMongoFilter, objectIdFromDate, objectIdSyntaxFromDate } from './mongo-objectid-converter.service';
|
||||
|
||||
describe('mongo-objectid-converter', () => {
|
||||
describe('objectIdFromDate', () => {
|
||||
it('convert a Date to ObjectId', () => {
|
||||
expect(objectIdFromDate(new Date(Date.UTC(2024, 0, 1)))).to.eql('659200800000000000000000');
|
||||
expect(objectIdFromDate(new Date(Date.UTC(2024, 0, 1, 12, 12, 12)))).to.eql('6592ac1c0000000000000000');
|
||||
});
|
||||
});
|
||||
describe('objectIdSyntaxFromDate', () => {
|
||||
it('convert a Date to ObjectId', () => {
|
||||
expect(objectIdSyntaxFromDate(new Date(Date.UTC(2024, 0, 1)))).to.eql('ObjectId("659200800000000000000000")');
|
||||
});
|
||||
});
|
||||
describe('dateFromObjectId', () => {
|
||||
it('convert an ObjectId to Date', () => {
|
||||
expect(dateFromObjectId('659200800000000000000000')).to.eql(new Date(Date.UTC(2024, 0, 1)));
|
||||
expect(dateFromObjectId('6592ac1c0000000000000000')).to.eql(new Date(Date.UTC(2024, 0, 1, 12, 12, 12)));
|
||||
});
|
||||
});
|
||||
describe('generateMongoFilter', () => {
|
||||
it('convert a date to mongo query', () => {
|
||||
expect(generateMongoFilter({
|
||||
date: new Date(Date.UTC(2024, 0, 1, 12, 12, 12)),
|
||||
tableName: 'comments',
|
||||
})).to.eql('db.comments.find({_id: {$gt: ObjectId("6592ac1c0000000000000000")}})');
|
||||
});
|
||||
});
|
||||
});
|
|
@ -0,0 +1,23 @@
|
|||
export function objectIdFromDate(date: Date) {
|
||||
return `${Math.floor(date.getTime() / 1000).toString(16)}0000000000000000`;
|
||||
};
|
||||
|
||||
export function objectIdSyntaxFromDate(date: Date) {
|
||||
return `ObjectId("${objectIdFromDate(date)}")`;
|
||||
};
|
||||
|
||||
export function generateMongoFilter(
|
||||
{
|
||||
tableName, date, operator = 'gt',
|
||||
}:
|
||||
{
|
||||
tableName: string
|
||||
date: Date
|
||||
operator?: 'gt' | 'lt'
|
||||
}) {
|
||||
return `db.${tableName}.find({_id: {$${operator}: ObjectId("${objectIdFromDate(date)}")}})`;
|
||||
}
|
||||
|
||||
export function dateFromObjectId(objectId: string) {
|
||||
return new Date(Number.parseInt(objectId.substring(0, 8), 16) * 1000);
|
||||
};
|
|
@ -0,0 +1,68 @@
|
|||
<script setup lang="ts">
|
||||
import { dateFromObjectId, generateMongoFilter, objectIdFromDate, objectIdSyntaxFromDate } from './mongo-objectid-converter.service';
|
||||
import { withDefaultOnError } from '@/utils/defaults';
|
||||
|
||||
const currentTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
||||
|
||||
const objectIdInput = ref(objectIdFromDate(new Date()));
|
||||
const dateOutput = computed(() =>
|
||||
withDefaultOnError(() => dateFromObjectId(objectIdInput.value), 'Invalid ObjectId'),
|
||||
);
|
||||
|
||||
const dateInput = ref(Date.now());
|
||||
const dateValue = computed(() => new Date(dateInput.value));
|
||||
const tableName = ref('tbl');
|
||||
const objectIdOutput = computed(() =>
|
||||
withDefaultOnError(() => objectIdFromDate(dateValue.value), 'Invalid Date'),
|
||||
);
|
||||
const objectIdSyntaxOutput = computed(() =>
|
||||
withDefaultOnError(() => objectIdSyntaxFromDate(dateValue.value), 'Invalid Date'),
|
||||
);
|
||||
const objectIdQueryOutput = computed(() =>
|
||||
withDefaultOnError(() => generateMongoFilter({ date: dateValue.value, tableName: tableName.value }), 'Invalid Date'),
|
||||
);
|
||||
const objectIdUTCDate = computed(() =>
|
||||
dateValue.value.toISOString(),
|
||||
);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<c-card :title="`ObjectId to Date (${currentTimeZone})`">
|
||||
<c-input-text
|
||||
v-model:value="objectIdInput"
|
||||
placeholder="Put your ObjectId here..."
|
||||
label="ObjectId to encode"
|
||||
raw-text
|
||||
mb-5
|
||||
/>
|
||||
|
||||
<n-divider />
|
||||
|
||||
<textarea-copyable
|
||||
:value="dateOutput instanceof Date ? dateOutput.toLocaleString(undefined, { timeZoneName: 'short' }) : dateOutput"
|
||||
/>
|
||||
<textarea-copyable
|
||||
:value="dateOutput instanceof Date ? dateOutput.toISOString() : dateOutput"
|
||||
/>
|
||||
</c-card>
|
||||
|
||||
<c-card :title="`Date to ObjectId (${currentTimeZone})`">
|
||||
<n-form-item label="Date and time" label-placement="left" mb-2 flex-1>
|
||||
<n-date-picker v-model:value="dateInput" type="datetime" />
|
||||
</n-form-item>
|
||||
|
||||
<c-input-text
|
||||
v-model:value="tableName"
|
||||
placeholder="Put your Table Name here..."
|
||||
label="Table Name"
|
||||
label-position="left"
|
||||
raw-text
|
||||
mb-5
|
||||
/>
|
||||
|
||||
<textarea-copyable :value="objectIdUTCDate" />
|
||||
<textarea-copyable :value="objectIdOutput" />
|
||||
<textarea-copyable :value="objectIdSyntaxOutput" />
|
||||
<textarea-copyable :value="objectIdQueryOutput" />
|
||||
</c-card>
|
||||
</template>
|
Loading…
Add table
Add a link
Reference in a new issue