mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-05-01 03:49:13 -04:00
chore(lint): switched to a better lint config
This commit is contained in:
parent
4d2b037dbe
commit
33c9b6643f
178 changed files with 4105 additions and 3371 deletions
|
@ -1,4 +1,4 @@
|
|||
import { test, expect } from '@playwright/test';
|
||||
import { expect, test } from '@playwright/test';
|
||||
|
||||
test.describe('Date time converter - json to yaml', () => {
|
||||
test.beforeEach(async ({ page }) => {
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
import { describe, test, expect } from 'vitest';
|
||||
import { describe, expect, test } from 'vitest';
|
||||
import {
|
||||
isISO8601DateTimeString,
|
||||
isISO9075DateString,
|
||||
isMongoObjectId,
|
||||
isRFC3339DateString,
|
||||
isRFC7231DateString,
|
||||
isUnixTimestamp,
|
||||
isTimestamp,
|
||||
isUTCDateString,
|
||||
isMongoObjectId,
|
||||
isUnixTimestamp,
|
||||
} from './date-time-converter.models';
|
||||
|
||||
describe('date-time-converter models', () => {
|
||||
|
|
|
@ -11,13 +11,13 @@ export {
|
|||
isMongoObjectId,
|
||||
};
|
||||
|
||||
const ISO8601_REGEX =
|
||||
/^([+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24:?00)([.,]\d+(?!:))?)?(\17[0-5]\d([.,]\d+)?)?([zZ]|([+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/;
|
||||
const ISO9075_REGEX =
|
||||
/^([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})(\.[0-9]{1,6})?(([+-])([0-9]{2}):([0-9]{2})|Z)?$/;
|
||||
const ISO8601_REGEX
|
||||
= /^([+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24:?00)([.,]\d+(?!:))?)?(\17[0-5]\d([.,]\d+)?)?([zZ]|([+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/;
|
||||
const ISO9075_REGEX
|
||||
= /^([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})(\.[0-9]{1,6})?(([+-])([0-9]{2}):([0-9]{2})|Z)?$/;
|
||||
|
||||
const RFC3339_REGEX =
|
||||
/^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(\.[0-9]{1,9})?(([+-])([0-9]{2}):([0-9]{2})|Z)$/;
|
||||
const RFC3339_REGEX
|
||||
= /^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(\.[0-9]{1,9})?(([+-])([0-9]{2}):([0-9]{2})|Z)$/;
|
||||
|
||||
const RFC7231_REGEX = /^[A-Za-z]{3},\s[0-9]{2}\s[A-Za-z]{3}\s[0-9]{4}\s[0-9]{2}:[0-9]{2}:[0-9]{2}\sGMT$/;
|
||||
|
||||
|
@ -40,7 +40,8 @@ function isUTCDateString(date?: string) {
|
|||
|
||||
try {
|
||||
return new Date(date).toUTCString() === date;
|
||||
} catch (_ignored) {
|
||||
}
|
||||
catch (_ignored) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
export type ToDateMapper = (value: string) => Date;
|
||||
|
||||
export type DateFormat = {
|
||||
name: string;
|
||||
fromDate: (date: Date) => string;
|
||||
toDate: (value: string) => Date;
|
||||
formatMatcher: (dateString: string) => boolean;
|
||||
};
|
||||
export interface DateFormat {
|
||||
name: string
|
||||
fromDate: (date: Date) => string
|
||||
toDate: (value: string) => Date
|
||||
formatMatcher: (dateString: string) => boolean
|
||||
}
|
||||
|
|
|
@ -1,3 +1,145 @@
|
|||
<script setup lang="ts">
|
||||
import {
|
||||
formatISO,
|
||||
formatISO9075,
|
||||
formatRFC3339,
|
||||
formatRFC7231,
|
||||
fromUnixTime,
|
||||
getTime,
|
||||
getUnixTime,
|
||||
isDate,
|
||||
isValid,
|
||||
parseISO,
|
||||
parseJSON,
|
||||
} from 'date-fns';
|
||||
import type { DateFormat, ToDateMapper } from './date-time-converter.types';
|
||||
import {
|
||||
isISO8601DateTimeString,
|
||||
isISO9075DateString,
|
||||
isMongoObjectId,
|
||||
isRFC3339DateString,
|
||||
isRFC7231DateString,
|
||||
isTimestamp,
|
||||
isUTCDateString,
|
||||
isUnixTimestamp,
|
||||
} from './date-time-converter.models';
|
||||
import { withDefaultOnError } from '@/utils/defaults';
|
||||
import { useValidation } from '@/composable/validation';
|
||||
|
||||
const inputDate = ref('');
|
||||
|
||||
const toDate: ToDateMapper = date => new Date(date);
|
||||
|
||||
const formats: DateFormat[] = [
|
||||
{
|
||||
name: 'JS locale date string',
|
||||
fromDate: date => date.toString(),
|
||||
toDate,
|
||||
formatMatcher: () => false,
|
||||
},
|
||||
{
|
||||
name: 'ISO 8601',
|
||||
fromDate: formatISO,
|
||||
toDate: parseISO,
|
||||
formatMatcher: date => isISO8601DateTimeString(date),
|
||||
},
|
||||
{
|
||||
name: 'ISO 9075',
|
||||
fromDate: formatISO9075,
|
||||
toDate: parseISO,
|
||||
formatMatcher: date => isISO9075DateString(date),
|
||||
},
|
||||
{
|
||||
name: 'RFC 3339',
|
||||
fromDate: formatRFC3339,
|
||||
toDate,
|
||||
formatMatcher: date => isRFC3339DateString(date),
|
||||
},
|
||||
{
|
||||
name: 'RFC 7231',
|
||||
fromDate: formatRFC7231,
|
||||
toDate,
|
||||
formatMatcher: date => isRFC7231DateString(date),
|
||||
},
|
||||
{
|
||||
name: 'Unix timestamp',
|
||||
fromDate: date => String(getUnixTime(date)),
|
||||
toDate: sec => fromUnixTime(+sec),
|
||||
formatMatcher: date => isUnixTimestamp(date),
|
||||
},
|
||||
{
|
||||
name: 'Timestamp',
|
||||
fromDate: date => String(getTime(date)),
|
||||
toDate: ms => parseJSON(+ms),
|
||||
formatMatcher: date => isTimestamp(date),
|
||||
},
|
||||
{
|
||||
name: 'UTC format',
|
||||
fromDate: date => date.toUTCString(),
|
||||
toDate,
|
||||
formatMatcher: date => isUTCDateString(date),
|
||||
},
|
||||
{
|
||||
name: 'Mongo ObjectID',
|
||||
fromDate: date => `${Math.floor(date.getTime() / 1000).toString(16)}0000000000000000`,
|
||||
toDate: objectId => new Date(parseInt(objectId.substring(0, 8), 16) * 1000),
|
||||
formatMatcher: date => isMongoObjectId(date),
|
||||
},
|
||||
];
|
||||
|
||||
const formatIndex = ref(6);
|
||||
const now = useNow();
|
||||
|
||||
const normalizedDate = computed(() => {
|
||||
if (!inputDate.value) {
|
||||
return now.value;
|
||||
}
|
||||
|
||||
const { toDate } = formats[formatIndex.value];
|
||||
|
||||
try {
|
||||
return toDate(inputDate.value);
|
||||
}
|
||||
catch (_ignored) {
|
||||
return undefined;
|
||||
}
|
||||
});
|
||||
|
||||
function onDateInputChanged(value: string) {
|
||||
const matchingIndex = formats.findIndex(({ formatMatcher }) => formatMatcher(value));
|
||||
if (matchingIndex !== -1) {
|
||||
formatIndex.value = matchingIndex;
|
||||
}
|
||||
}
|
||||
|
||||
const validation = useValidation({
|
||||
source: inputDate,
|
||||
watch: [formatIndex],
|
||||
rules: [
|
||||
{
|
||||
message: 'This date is invalid for this format',
|
||||
validator: value =>
|
||||
withDefaultOnError(() => {
|
||||
if (value === '') {
|
||||
return true;
|
||||
}
|
||||
|
||||
const maybeDate = formats[formatIndex.value].toDate(value);
|
||||
return isDate(maybeDate) && isValid(maybeDate);
|
||||
}, false),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
function formatDateUsingFormatter(formatter: (date: Date) => string, date?: Date) {
|
||||
if (!date || !validation.isValid) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return withDefaultOnError(() => formatter(date), '');
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div>
|
||||
<n-input-group>
|
||||
|
@ -36,142 +178,3 @@
|
|||
/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import {
|
||||
formatISO,
|
||||
formatISO9075,
|
||||
formatRFC3339,
|
||||
formatRFC7231,
|
||||
fromUnixTime,
|
||||
getTime,
|
||||
getUnixTime,
|
||||
parseISO,
|
||||
parseJSON,
|
||||
isDate,
|
||||
isValid,
|
||||
} from 'date-fns';
|
||||
import { withDefaultOnError } from '@/utils/defaults';
|
||||
import { useValidation } from '@/composable/validation';
|
||||
import type { DateFormat, ToDateMapper } from './date-time-converter.types';
|
||||
import {
|
||||
isISO8601DateTimeString,
|
||||
isISO9075DateString,
|
||||
isRFC3339DateString,
|
||||
isRFC7231DateString,
|
||||
isTimestamp,
|
||||
isUTCDateString,
|
||||
isUnixTimestamp,
|
||||
isMongoObjectId,
|
||||
} from './date-time-converter.models';
|
||||
|
||||
const inputDate = ref('');
|
||||
|
||||
const toDate: ToDateMapper = (date) => new Date(date);
|
||||
|
||||
function formatDateUsingFormatter(formatter: (date: Date) => string, date?: Date) {
|
||||
if (!date || !validation.isValid) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return withDefaultOnError(() => formatter(date), '');
|
||||
}
|
||||
|
||||
const formats: DateFormat[] = [
|
||||
{
|
||||
name: 'JS locale date string',
|
||||
fromDate: (date) => date.toString(),
|
||||
toDate,
|
||||
formatMatcher: () => false,
|
||||
},
|
||||
{
|
||||
name: 'ISO 8601',
|
||||
fromDate: formatISO,
|
||||
toDate: parseISO,
|
||||
formatMatcher: (date) => isISO8601DateTimeString(date),
|
||||
},
|
||||
{
|
||||
name: 'ISO 9075',
|
||||
fromDate: formatISO9075,
|
||||
toDate: parseISO,
|
||||
formatMatcher: (date) => isISO9075DateString(date),
|
||||
},
|
||||
{
|
||||
name: 'RFC 3339',
|
||||
fromDate: formatRFC3339,
|
||||
toDate,
|
||||
formatMatcher: (date) => isRFC3339DateString(date),
|
||||
},
|
||||
{
|
||||
name: 'RFC 7231',
|
||||
fromDate: formatRFC7231,
|
||||
toDate,
|
||||
formatMatcher: (date) => isRFC7231DateString(date),
|
||||
},
|
||||
{
|
||||
name: 'Unix timestamp',
|
||||
fromDate: (date) => String(getUnixTime(date)),
|
||||
toDate: (sec) => fromUnixTime(+sec),
|
||||
formatMatcher: (date) => isUnixTimestamp(date),
|
||||
},
|
||||
{
|
||||
name: 'Timestamp',
|
||||
fromDate: (date) => String(getTime(date)),
|
||||
toDate: (ms) => parseJSON(+ms),
|
||||
formatMatcher: (date) => isTimestamp(date),
|
||||
},
|
||||
{
|
||||
name: 'UTC format',
|
||||
fromDate: (date) => date.toUTCString(),
|
||||
toDate,
|
||||
formatMatcher: (date) => isUTCDateString(date),
|
||||
},
|
||||
{
|
||||
name: 'Mongo ObjectID',
|
||||
fromDate: (date) => Math.floor(date.getTime() / 1000).toString(16) + '0000000000000000',
|
||||
toDate: (objectId) => new Date(parseInt(objectId.substring(0, 8), 16) * 1000),
|
||||
formatMatcher: (date) => isMongoObjectId(date),
|
||||
},
|
||||
];
|
||||
|
||||
const formatIndex = ref(6);
|
||||
const now = useNow();
|
||||
|
||||
const normalizedDate = computed(() => {
|
||||
if (!inputDate.value) {
|
||||
return now.value;
|
||||
}
|
||||
|
||||
const { toDate } = formats[formatIndex.value];
|
||||
|
||||
try {
|
||||
return toDate(inputDate.value);
|
||||
} catch (_ignored) {
|
||||
return undefined;
|
||||
}
|
||||
});
|
||||
|
||||
function onDateInputChanged(value: string) {
|
||||
const matchingIndex = formats.findIndex(({ formatMatcher }) => formatMatcher(value));
|
||||
if (matchingIndex !== -1) {
|
||||
formatIndex.value = matchingIndex;
|
||||
}
|
||||
}
|
||||
|
||||
const validation = useValidation({
|
||||
source: inputDate,
|
||||
watch: [formatIndex],
|
||||
rules: [
|
||||
{
|
||||
message: 'This date is invalid for this format',
|
||||
validator: (value) =>
|
||||
withDefaultOnError(() => {
|
||||
if (value === '') return true;
|
||||
|
||||
const maybeDate = formats[formatIndex.value].toDate(value);
|
||||
return isDate(maybeDate) && isValid(maybeDate);
|
||||
}, false),
|
||||
},
|
||||
],
|
||||
});
|
||||
</script>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue