mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-04-24 08:46:15 -04:00
add objectid util functions
This commit is contained in:
parent
64082d4e6d
commit
7e5e4d00ee
3 changed files with 28 additions and 5 deletions
|
@ -8,7 +8,6 @@ export {
|
||||||
isUnixTimestamp,
|
isUnixTimestamp,
|
||||||
isTimestamp,
|
isTimestamp,
|
||||||
isUTCDateString,
|
isUTCDateString,
|
||||||
isMongoObjectId,
|
|
||||||
dateToExcelFormat,
|
dateToExcelFormat,
|
||||||
excelFormatToDate,
|
excelFormatToDate,
|
||||||
isExcelFormat,
|
isExcelFormat,
|
||||||
|
@ -36,7 +35,6 @@ const isRFC3339DateString = createRegexMatcher(RFC3339_REGEX);
|
||||||
const isRFC7231DateString = createRegexMatcher(RFC7231_REGEX);
|
const isRFC7231DateString = createRegexMatcher(RFC7231_REGEX);
|
||||||
const isUnixTimestamp = createRegexMatcher(/^[0-9]{1,10}$/);
|
const isUnixTimestamp = createRegexMatcher(/^[0-9]{1,10}$/);
|
||||||
const isTimestamp = createRegexMatcher(/^[0-9]{1,13}$/);
|
const isTimestamp = createRegexMatcher(/^[0-9]{1,13}$/);
|
||||||
const isMongoObjectId = createRegexMatcher(/^[0-9a-fA-F]{24}$/);
|
|
||||||
|
|
||||||
const isExcelFormat = createRegexMatcher(EXCEL_FORMAT_REGEX);
|
const isExcelFormat = createRegexMatcher(EXCEL_FORMAT_REGEX);
|
||||||
|
|
||||||
|
|
|
@ -28,6 +28,7 @@ import {
|
||||||
} from './date-time-converter.models';
|
} from './date-time-converter.models';
|
||||||
import { withDefaultOnError } from '@/utils/defaults';
|
import { withDefaultOnError } from '@/utils/defaults';
|
||||||
import { useValidation } from '@/composable/validation';
|
import { useValidation } from '@/composable/validation';
|
||||||
|
import { isValidObjectId, objectIdFromDate, objectIdToDate } from '@/utils/objectId';
|
||||||
|
|
||||||
const inputDate = ref('');
|
const inputDate = ref('');
|
||||||
|
|
||||||
|
@ -84,9 +85,9 @@ const formats: DateFormat[] = [
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'Mongo ObjectID',
|
name: 'Mongo ObjectID',
|
||||||
fromDate: date => `${Math.floor(date.getTime() / 1000).toString(16)}0000000000000000`,
|
fromDate: date => objectIdFromDate(date.getTime(), true),
|
||||||
toDate: objectId => new Date(Number.parseInt(objectId.substring(0, 8), 16) * 1000),
|
toDate: objectIdToDate,
|
||||||
formatMatcher: date => isMongoObjectId(date),
|
formatMatcher: date => isValidObjectId(date),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'Excel date/time',
|
name: 'Excel date/time',
|
||||||
|
|
24
src/utils/objectId.ts
Normal file
24
src/utils/objectId.ts
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
import _ from 'lodash';
|
||||||
|
|
||||||
|
export function isValidObjectId(objectId?: string): boolean {
|
||||||
|
return !_.isNil(objectId) && /^[0-9a-fA-F]{24}$/.test(objectId);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function objectIdToDate(objectId: string): Date {
|
||||||
|
return new Date(Number.parseInt(objectId.substring(0, 8), 16) * 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function objectIdFromDate(milliseconds: number, onlyDate: boolean = false): string {
|
||||||
|
const suffixReplacer = () => {
|
||||||
|
if (onlyDate) {
|
||||||
|
return '0';
|
||||||
|
}
|
||||||
|
|
||||||
|
return (Math.random() * 16 | 0).toString(16);
|
||||||
|
};
|
||||||
|
|
||||||
|
const timestamp = (milliseconds / 1000 | 0).toString(16);
|
||||||
|
const suffix = 'xxxxxxxxxxxxxxxx'.replace(/[x]/g, suffixReplacer).toLowerCase();
|
||||||
|
|
||||||
|
return `${timestamp}${suffix}`;
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue