mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-24 06:40:20 +03:00
Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3ef7e50b5b | ||
|
|
6fd79d6e06 | ||
|
|
88ecf60587 |
No files matched your search
@@ -35,6 +35,7 @@
|
||||
"release": "node ./scripts/release.mjs"
|
||||
},
|
||||
"dependencies": {
|
||||
"@date-fns/utc": "^1.2.0",
|
||||
"@it-tools/bip39": "^0.0.4",
|
||||
"@it-tools/oggen": "^1.3.0",
|
||||
"@sindresorhus/slugify": "^2.2.1",
|
||||
|
||||
Generated
+16
-8
@@ -5,6 +5,9 @@ settings:
|
||||
excludeLinksFromLockfile: false
|
||||
|
||||
dependencies:
|
||||
'@date-fns/utc':
|
||||
specifier: ^1.2.0
|
||||
version: 1.2.0
|
||||
'@it-tools/bip39':
|
||||
specifier: ^0.0.4
|
||||
version: 0.0.4
|
||||
@@ -1905,6 +1908,10 @@ packages:
|
||||
vue: 3.3.4
|
||||
dev: false
|
||||
|
||||
/@date-fns/utc@1.2.0:
|
||||
resolution: {integrity: sha512-YLq+crMPJiBmIdkRmv9nZuZy1mVtMlDcUKlg4mvI0UsC/dZeIaGoGB5p/C4FrpeOhZ7zBTK03T58S0DFkRNMnw==}
|
||||
dev: false
|
||||
|
||||
/@emotion/hash@0.8.0:
|
||||
resolution: {integrity: sha512-kBJtf7PH6aWwZ6fka3zQ0p6SBYzx4fl1LoZXE2RrnYST9Xljm7WfKJrU4g/Xr3Beg72MLrp1AWNUmuYJTL7Cow==}
|
||||
dev: false
|
||||
@@ -3341,7 +3348,7 @@ packages:
|
||||
dependencies:
|
||||
'@unhead/dom': 0.5.1
|
||||
'@unhead/schema': 0.5.1
|
||||
'@vueuse/shared': 10.7.2(vue@3.3.4)
|
||||
'@vueuse/shared': 11.0.3(vue@3.3.4)
|
||||
unhead: 0.5.1
|
||||
vue: 3.3.4
|
||||
transitivePeerDependencies:
|
||||
@@ -3983,10 +3990,10 @@ packages:
|
||||
- vue
|
||||
dev: false
|
||||
|
||||
/@vueuse/shared@10.7.2(vue@3.3.4):
|
||||
resolution: {integrity: sha512-qFbXoxS44pi2FkgFjPvF4h7c9oMDutpyBdcJdMYIMg9XyXli2meFMuaKn+UMgsClo//Th6+beeCgqweT/79BVA==}
|
||||
/@vueuse/shared@11.0.3(vue@3.3.4):
|
||||
resolution: {integrity: sha512-0rY2m6HS5t27n/Vp5cTDsKTlNnimCqsbh/fmT2LgE+aaU42EMfXo8+bNX91W9I7DDmxfuACXMmrd7d79JxkqWA==}
|
||||
dependencies:
|
||||
vue-demi: 0.14.6(vue@3.3.4)
|
||||
vue-demi: 0.14.10(vue@3.3.4)
|
||||
transitivePeerDependencies:
|
||||
- '@vue/composition-api'
|
||||
- vue
|
||||
@@ -9120,8 +9127,8 @@ packages:
|
||||
vue: 3.3.4
|
||||
dev: false
|
||||
|
||||
/vue-demi@0.14.5(vue@3.3.4):
|
||||
resolution: {integrity: sha512-o9NUVpl/YlsGJ7t+xuqJKx8EBGf1quRhCiT6D/J0pfwmk9zUwYkC7yrF4SZCe6fETvSM3UNL2edcbYrSyc4QHA==}
|
||||
/vue-demi@0.14.10(vue@3.3.4):
|
||||
resolution: {integrity: sha512-nMZBOwuzabUO0nLgIcc6rycZEebF6eeUfaiQx9+WSk8e29IbLvPU9feI6tqW4kTo3hvoYAJkMh8n8D0fuISphg==}
|
||||
engines: {node: '>=12'}
|
||||
hasBin: true
|
||||
requiresBuild: true
|
||||
@@ -9135,8 +9142,8 @@ packages:
|
||||
vue: 3.3.4
|
||||
dev: false
|
||||
|
||||
/vue-demi@0.14.6(vue@3.3.4):
|
||||
resolution: {integrity: sha512-8QA7wrYSHKaYgUxDA5ZC24w+eHm3sYCbp0EzcDwKqN3p6HqtTCGR/GVsPyZW92unff4UlcSh++lmqDWN3ZIq4w==}
|
||||
/vue-demi@0.14.5(vue@3.3.4):
|
||||
resolution: {integrity: sha512-o9NUVpl/YlsGJ7t+xuqJKx8EBGf1quRhCiT6D/J0pfwmk9zUwYkC7yrF4SZCe6fETvSM3UNL2edcbYrSyc4QHA==}
|
||||
engines: {node: '>=12'}
|
||||
hasBin: true
|
||||
requiresBuild: true
|
||||
@@ -9426,6 +9433,7 @@ packages:
|
||||
|
||||
/workbox-google-analytics@7.0.0:
|
||||
resolution: {integrity: sha512-MEYM1JTn/qiC3DbpvP2BVhyIH+dV/5BjHk756u9VbwuAhu0QHyKscTnisQuz21lfRpOwiS9z4XdqeVAKol0bzg==}
|
||||
deprecated: It is not compatible with newer versions of GA starting with v4, as long as you are using GAv3 it should be ok, but the package is not longer being maintained
|
||||
dependencies:
|
||||
workbox-background-sync: 7.0.0
|
||||
workbox-core: 7.0.0
|
||||
|
||||
@@ -11,7 +11,7 @@ test.describe('Date time converter - json to yaml', () => {
|
||||
|
||||
test('Format is auto detected from a date and the date is correctly converted', async ({ page }) => {
|
||||
const initialFormat = await page.getByTestId('date-time-converter-format-select').innerText();
|
||||
expect(initialFormat.trim()).toEqual('Timestamp');
|
||||
expect(initialFormat.trim()).toEqual('Unix timestamp');
|
||||
|
||||
await page.getByTestId('date-time-converter-input').fill('2023-04-12T23:10:24+02:00');
|
||||
|
||||
@@ -22,6 +22,7 @@ test.describe('Date time converter - json to yaml', () => {
|
||||
'Wed Apr 12 2023 23:10:24 GMT+0200 (Central European Summer Time)',
|
||||
);
|
||||
expect((await page.getByTestId('ISO 8601').inputValue()).trim()).toEqual('2023-04-12T23:10:24+02:00');
|
||||
expect((await page.getByTestId('ISO 8601 UTC').inputValue()).trim()).toEqual('2023-04-12T21:10:24.000Z');
|
||||
expect((await page.getByTestId('ISO 9075').inputValue()).trim()).toEqual('2023-04-12 23:10:24');
|
||||
expect((await page.getByTestId('Unix timestamp').inputValue()).trim()).toEqual('1681333824');
|
||||
expect((await page.getByTestId('RFC 7231').inputValue()).trim()).toEqual('Wed, 12 Apr 2023 21:10:24 GMT');
|
||||
|
||||
@@ -2,15 +2,20 @@ import { describe, expect, test } from 'vitest';
|
||||
import {
|
||||
dateToExcelFormat,
|
||||
excelFormatToDate,
|
||||
fromJSDate,
|
||||
fromTimestamp,
|
||||
isExcelFormat,
|
||||
isISO8601DateTimeString,
|
||||
isISO9075DateString,
|
||||
isJSDate,
|
||||
isMongoObjectId,
|
||||
isRFC3339DateString,
|
||||
isRFC7231DateString,
|
||||
isTimestamp,
|
||||
isTimestampMicroSeconds,
|
||||
isUTCDateString,
|
||||
isUnixTimestamp,
|
||||
toJSDate,
|
||||
} from './date-time-converter.models';
|
||||
|
||||
describe('date-time-converter models', () => {
|
||||
@@ -113,6 +118,45 @@ describe('date-time-converter models', () => {
|
||||
expect(isTimestamp('foo')).toBe(false);
|
||||
expect(isTimestamp('')).toBe(false);
|
||||
});
|
||||
|
||||
test('should return true for valid Unix timestamps in microseconds', () => {
|
||||
expect(isTimestamp('1701227351995845')).toBe(true);
|
||||
});
|
||||
|
||||
test('should return false for invalid Unix timestamps in microseconds', () => {
|
||||
expect(isTimestamp('170122735199584')).toBe(false);
|
||||
expect(isTimestamp('17012273519958')).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isTimestampMicroSeconds', () => {
|
||||
test('should return true for valid Unix timestamps in microseconds', () => {
|
||||
expect(isTimestampMicroSeconds('1649792026123123')).toBe(true);
|
||||
expect(isTimestampMicroSeconds('1701227351995845')).toBe(true);
|
||||
});
|
||||
|
||||
test('should return false for invalid Unix timestamps in microseconds', () => {
|
||||
expect(isTimestampMicroSeconds('foo')).toBe(false);
|
||||
expect(isTimestampMicroSeconds('')).toBe(false);
|
||||
});
|
||||
|
||||
test('should return false for invalid Unix timestamps not in microseconds', () => {
|
||||
expect(isTimestampMicroSeconds('170122735199584')).toBe(false);
|
||||
expect(isTimestampMicroSeconds('17012273519958')).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('fromTimestamp', () => {
|
||||
test('should return valid Date for valid Unix timestamps in microseconds', () => {
|
||||
expect(fromTimestamp('1649792026123123').toString()).toBe(new Date(1649792026123).toString());
|
||||
expect(fromTimestamp('1701227351995845').toString()).toBe(new Date(1701227351995).toString());
|
||||
expect(fromTimestamp('0').toString()).toBe(new Date(0).toString());
|
||||
});
|
||||
|
||||
test('should return Date(0) for invalid Unix timestamps not in microseconds', () => {
|
||||
expect(fromTimestamp('170122735199584').toString()).toBe(new Date(0).toString());
|
||||
expect(fromTimestamp('17012273519958').toString()).toBe(new Date(0).toString());
|
||||
});
|
||||
});
|
||||
|
||||
describe('isUTCDateString', () => {
|
||||
@@ -177,4 +221,36 @@ describe('date-time-converter models', () => {
|
||||
expect(excelFormatToDate('-1000')).toEqual(new Date('1897-04-04T00:00:00.000Z'));
|
||||
});
|
||||
});
|
||||
|
||||
describe('isJSDate', () => {
|
||||
test('a JS date is a new Date()', () => {
|
||||
expect(isJSDate('new Date(2000, 0)')).toBe(true);
|
||||
expect(isJSDate('new Date(2000, 0, 1, 12, 12)')).toBe(true);
|
||||
expect(isJSDate('new Date(2000, 0, 1, 12, 12, 12)')).toBe(true);
|
||||
expect(isJSDate('new Date(2000, 0, 1, 12, 12, 12, 1)')).toBe(true);
|
||||
|
||||
expect(isJSDate('new Date(2000)')).toBe(false);
|
||||
expect(isJSDate('')).toBe(false);
|
||||
expect(isJSDate('foo')).toBe(false);
|
||||
expect(isJSDate('1.1.1')).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('fromJSDate', () => {
|
||||
test('convert a JS new Date() to date', () => {
|
||||
expect(fromJSDate('new Date(2000, 0)')).toEqual(new Date(2000, 0));
|
||||
expect(fromJSDate('new Date(2000, 0, 1, 12, 12)')).toEqual(new Date(2000, 0, 1, 12, 12));
|
||||
expect(fromJSDate('new Date(2000, 0, 1, 12, 12, 12)')).toEqual(new Date(2000, 0, 1, 12, 12, 12));
|
||||
expect(fromJSDate('new Date(2000, 0, 1, 12, 12, 12, 1)')).toEqual(new Date(2000, 0, 1, 12, 12, 12, 1));
|
||||
});
|
||||
});
|
||||
|
||||
describe('toJSDate', () => {
|
||||
test('convert a date to JS new Date()', () => {
|
||||
expect(toJSDate(new Date(2000, 0))).toEqual('new Date(2000, 0, 1, 0, 0, 0, 0);');
|
||||
expect(toJSDate(new Date(2000, 0, 1, 12, 12))).toEqual('new Date(2000, 0, 1, 12, 12, 0, 0);');
|
||||
expect(toJSDate(new Date(2000, 0, 1, 12, 12, 12))).toEqual('new Date(2000, 0, 1, 12, 12, 12, 0);');
|
||||
expect(toJSDate(new Date(2000, 0, 1, 12, 12, 12, 1))).toEqual('new Date(2000, 0, 1, 12, 12, 12, 1);');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,5 @@
|
||||
import _ from 'lodash';
|
||||
import { addMilliseconds } from 'date-fns';
|
||||
|
||||
export {
|
||||
isISO8601DateTimeString,
|
||||
@@ -12,6 +13,11 @@ export {
|
||||
dateToExcelFormat,
|
||||
excelFormatToDate,
|
||||
isExcelFormat,
|
||||
fromTimestamp,
|
||||
isTimestampMicroSeconds,
|
||||
isJSDate,
|
||||
fromJSDate,
|
||||
toJSDate,
|
||||
};
|
||||
|
||||
const ISO8601_REGEX
|
||||
@@ -26,6 +32,8 @@ const RFC7231_REGEX = /^[A-Za-z]{3},\s[0-9]{2}\s[A-Za-z]{3}\s[0-9]{4}\s[0-9]{2}:
|
||||
|
||||
const EXCEL_FORMAT_REGEX = /^-?\d+(\.\d+)?$/;
|
||||
|
||||
const JS_DATE_REGEX = /^new\s+Date\(\s*(?:(\d+)\s*,\s*)(?:(\d|11)\s*,\s*(?:(\d+)\s*,\s*(?:(\d+)\s*,\s*(?:(\d+)\s*,\s*(?:(\d+)\s*,\s*)?)?)?)?)?(\d+)\)\s*;?$/;
|
||||
|
||||
function createRegexMatcher(regex: RegExp) {
|
||||
return (date?: string) => !_.isNil(date) && regex.test(date);
|
||||
}
|
||||
@@ -35,9 +43,19 @@ const isISO9075DateString = createRegexMatcher(ISO9075_REGEX);
|
||||
const isRFC3339DateString = createRegexMatcher(RFC3339_REGEX);
|
||||
const isRFC7231DateString = createRegexMatcher(RFC7231_REGEX);
|
||||
const isUnixTimestamp = createRegexMatcher(/^[0-9]{1,10}$/);
|
||||
const isTimestamp = createRegexMatcher(/^[0-9]{1,13}$/);
|
||||
const isTimestamp = createRegexMatcher(/^([0-9]{1,13}|[0-9]{16})$/);
|
||||
const isTimestampMilliSeconds = createRegexMatcher(/^[0-9]{1,13}$/);
|
||||
const isTimestampMicroSeconds = createRegexMatcher(/^[0-9]{16}$/);
|
||||
const isMongoObjectId = createRegexMatcher(/^[0-9a-fA-F]{24}$/);
|
||||
|
||||
const isJSDate = createRegexMatcher(JS_DATE_REGEX);
|
||||
function fromJSDate(date: string): Date {
|
||||
const res = JS_DATE_REGEX.exec(date);
|
||||
const parts = (res || []).filter(p => p !== undefined).map(p => Number.parseInt(p, 10)).slice(1);
|
||||
return new (Function.prototype.bind.apply(Date, [null, ...parts]))();
|
||||
}
|
||||
const toJSDate = (date: Date) => `new Date(${date.getFullYear()}, ${date.getMonth()}, ${date.getDate()}, ${date.getHours()}, ${date.getMinutes()}, ${date.getSeconds()}, ${date.getMilliseconds()});`;
|
||||
|
||||
const isExcelFormat = createRegexMatcher(EXCEL_FORMAT_REGEX);
|
||||
|
||||
function isUTCDateString(date?: string) {
|
||||
@@ -60,3 +78,14 @@ function dateToExcelFormat(date: Date) {
|
||||
function excelFormatToDate(excelFormat: string | number) {
|
||||
return new Date((Number(excelFormat) - 25569) * 86400 * 1000);
|
||||
}
|
||||
|
||||
function fromTimestamp(timestamp: string, type: 'auto' | 'milliseconds' | 'microseconds' = 'auto') {
|
||||
let milliSeconds = 0;
|
||||
if (type === 'microseconds' || isTimestampMicroSeconds(timestamp)) {
|
||||
milliSeconds = Number(timestamp) / 1000;
|
||||
}
|
||||
else if (type === 'milliseconds' || isTimestampMilliSeconds(timestamp)) {
|
||||
milliSeconds = Number(timestamp);
|
||||
}
|
||||
return addMilliseconds(new Date(0), milliSeconds);
|
||||
}
|
||||
@@ -10,21 +10,25 @@ import {
|
||||
isDate,
|
||||
isValid,
|
||||
parseISO,
|
||||
parseJSON,
|
||||
} from 'date-fns';
|
||||
import { UTCDate } from '@date-fns/utc';
|
||||
import type { DateFormat, ToDateMapper } from './date-time-converter.types';
|
||||
import {
|
||||
dateToExcelFormat,
|
||||
excelFormatToDate,
|
||||
fromJSDate,
|
||||
fromTimestamp,
|
||||
isExcelFormat,
|
||||
isISO8601DateTimeString,
|
||||
isISO9075DateString,
|
||||
isJSDate,
|
||||
isMongoObjectId,
|
||||
isRFC3339DateString,
|
||||
isRFC7231DateString,
|
||||
isTimestamp,
|
||||
isUTCDateString,
|
||||
isUnixTimestamp,
|
||||
toJSDate,
|
||||
} from './date-time-converter.models';
|
||||
import { withDefaultOnError } from '@/utils/defaults';
|
||||
import { useValidation } from '@/composable/validation';
|
||||
@@ -46,6 +50,12 @@ const formats: DateFormat[] = [
|
||||
toDate: parseISO,
|
||||
formatMatcher: date => isISO8601DateTimeString(date),
|
||||
},
|
||||
{
|
||||
name: 'ISO 8601 UTC',
|
||||
fromDate: date => (new UTCDate(date)).toISOString(),
|
||||
toDate: parseISO,
|
||||
formatMatcher: date => isISO8601DateTimeString(date),
|
||||
},
|
||||
{
|
||||
name: 'ISO 9075',
|
||||
fromDate: formatISO9075,
|
||||
@@ -73,7 +83,7 @@ const formats: DateFormat[] = [
|
||||
{
|
||||
name: 'Timestamp',
|
||||
fromDate: date => String(getTime(date)),
|
||||
toDate: ms => parseJSON(+ms),
|
||||
toDate: ms => fromTimestamp(ms),
|
||||
formatMatcher: date => isTimestamp(date),
|
||||
},
|
||||
{
|
||||
@@ -94,6 +104,12 @@ const formats: DateFormat[] = [
|
||||
toDate: excelFormatToDate,
|
||||
formatMatcher: isExcelFormat,
|
||||
},
|
||||
{
|
||||
name: 'JS Date',
|
||||
fromDate: date => toJSDate(date),
|
||||
toDate: date => fromJSDate(date),
|
||||
formatMatcher: isJSDate,
|
||||
},
|
||||
];
|
||||
|
||||
const formatIndex = ref(6);
|
||||
|
||||
@@ -11,24 +11,6 @@ describe('integer-base-converter', () => {
|
||||
expect(convertBase({ value: '10100101', fromBase: 2, toBase: 16 })).toEqual('a5');
|
||||
expect(convertBase({ value: '192654', fromBase: 10, toBase: 8 })).toEqual('570216');
|
||||
expect(convertBase({ value: 'zz', fromBase: 64, toBase: 10 })).toEqual('2275');
|
||||
expect(convertBase({ value: 'AA', fromBase: 16, toBase: 10 })).toEqual('170');
|
||||
expect(convertBase({ value: 'aa', fromBase: 16, toBase: 10 })).toEqual('170');
|
||||
expect(convertBase({ value: '0xAA', fromBase: -1, toBase: 10 })).toEqual('170');
|
||||
expect(convertBase({ value: '&HAA', fromBase: -1, toBase: 10 })).toEqual('170');
|
||||
expect(convertBase({ value: '0xAAUL', fromBase: -1, toBase: 10 })).toEqual('170');
|
||||
expect(convertBase({ value: '0XAAUL', fromBase: -1, toBase: 10 })).toEqual('170');
|
||||
expect(convertBase({ value: '10UL', fromBase: 10, toBase: 10 })).toEqual('10');
|
||||
expect(convertBase({ value: '10n', fromBase: 10, toBase: 10 })).toEqual('10');
|
||||
expect(convertBase({ value: '0o252', fromBase: -1, toBase: 10 })).toEqual('170');
|
||||
expect(convertBase({ value: '&O252', fromBase: -1, toBase: 10 })).toEqual('170');
|
||||
expect(convertBase({ value: '192 654', fromBase: 10, toBase: 8 })).toEqual('570216');
|
||||
expect(convertBase({ value: '192.654', fromBase: 10, toBase: 8 })).toEqual('570216');
|
||||
expect(convertBase({ value: '0b10101010', fromBase: -1, toBase: 10 })).toEqual('170');
|
||||
expect(convertBase({ value: '0b_1010_1010', fromBase: -1, toBase: 10 })).toEqual('170');
|
||||
expect(convertBase({ value: '192,654', fromBase: 10, toBase: 8 })).toEqual('570216');
|
||||
expect(convertBase({ value: '42540766411283223938465490632011909384', fromBase: 10, toBase: 10 })).toEqual('42540766411283223938465490632011909384');
|
||||
expect(convertBase({ value: '42540766411283223938465490632011909384', fromBase: 10, toBase: 16 })).toEqual('20010db8000085a300000000ac1f8908');
|
||||
expect(convertBase({ value: '20010db8000085a300000000ac1f8908', fromBase: 16, toBase: 10 })).toEqual('42540766411283223938465490632011909384');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,61 +1,20 @@
|
||||
export function hasNumberPrefix(value: string) {
|
||||
return (value ?? '').trim().match(/^(0[xob].|&[hob].)/i);
|
||||
}
|
||||
|
||||
export function convertBase(
|
||||
{
|
||||
value, fromBase, toBase,
|
||||
ignorePunctuationsRegexChars = ' \u00A0_\.,-',
|
||||
handlePrefixSuffix = true,
|
||||
ignoreCase = true,
|
||||
}: {
|
||||
value: string
|
||||
fromBase: number
|
||||
toBase: number
|
||||
ignorePunctuationsRegexChars?: string
|
||||
handlePrefixSuffix?: boolean
|
||||
ignoreCase?: boolean
|
||||
}) {
|
||||
let cleanedValue = (value ?? '0').trim();
|
||||
if (ignorePunctuationsRegexChars) {
|
||||
cleanedValue = cleanedValue.replace(new RegExp(`[${ignorePunctuationsRegexChars}]`, 'g'), '');
|
||||
}
|
||||
let finalFromBase = fromBase;
|
||||
if (handlePrefixSuffix) {
|
||||
for (const regBase of [
|
||||
{ base: 2, regex: /^(&b|0b)?([01]+)([IULZn]*)$/i },
|
||||
{ base: 8, regex: /^(&o|0o)?([0-7]+)([IULZn]*)$/i },
|
||||
{ base: 16, regex: /^(&h|0x)?([a-f0-9]+)([IULZn]*)$/i },
|
||||
]) {
|
||||
const match = cleanedValue.match(regBase.regex);
|
||||
if (match) {
|
||||
if (match[1]) {
|
||||
finalFromBase = regBase.base;
|
||||
}
|
||||
cleanedValue = match[2];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ignoreCase && finalFromBase <= 36) {
|
||||
cleanedValue = cleanedValue.toLowerCase();
|
||||
}
|
||||
export function convertBase({ value, fromBase, toBase }: { value: string; fromBase: number; toBase: number }) {
|
||||
const range = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/'.split('');
|
||||
const fromRange = range.slice(0, finalFromBase);
|
||||
const fromRange = range.slice(0, fromBase);
|
||||
const toRange = range.slice(0, toBase);
|
||||
let decValue = cleanedValue
|
||||
let decValue = value
|
||||
.split('')
|
||||
.reverse()
|
||||
.reduce((carry: bigint, digit: string, index: number) => {
|
||||
.reduce((carry: number, digit: string, index: number) => {
|
||||
if (!fromRange.includes(digit)) {
|
||||
throw new Error(`Invalid digit "${digit}" for base ${finalFromBase}.`);
|
||||
throw new Error(`Invalid digit "${digit}" for base ${fromBase}.`);
|
||||
}
|
||||
return (carry += BigInt(fromRange.indexOf(digit)) * BigInt(finalFromBase) ** BigInt(index));
|
||||
}, 0n);
|
||||
return (carry += fromRange.indexOf(digit) * fromBase ** index);
|
||||
}, 0);
|
||||
let newValue = '';
|
||||
while (decValue > 0) {
|
||||
newValue = toRange[Number(decValue % BigInt(toBase))] + newValue;
|
||||
decValue = (decValue - (decValue % BigInt(toBase))) / BigInt(toBase);
|
||||
newValue = toRange[decValue % toBase] + newValue;
|
||||
decValue = (decValue - (decValue % toBase)) / toBase;
|
||||
}
|
||||
return newValue || '0';
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
<script setup lang="ts">
|
||||
import InputCopyable from '../../components/InputCopyable.vue';
|
||||
import { convertBase, hasNumberPrefix } from './integer-base-converter.model';
|
||||
import { convertBase } from './integer-base-converter.model';
|
||||
import { getErrorMessageIfThrows } from '@/utils/error';
|
||||
|
||||
const inputProps = {
|
||||
@@ -15,8 +15,6 @@ const input = ref('42');
|
||||
const inputBase = ref(10);
|
||||
const outputBase = ref(42);
|
||||
|
||||
const hasInputNumberPrefix = computed(() => hasNumberPrefix(input.value));
|
||||
|
||||
function errorlessConvert(...args: Parameters<typeof convertBase>) {
|
||||
try {
|
||||
return convertBase(...args);
|
||||
@@ -38,7 +36,7 @@ const error = computed(() =>
|
||||
<c-card>
|
||||
<c-input-text v-model:value="input" label="Input number" placeholder="Put your number here (ex: 42)" label-position="left" label-width="110px" mb-2 label-align="right" />
|
||||
|
||||
<n-form-item v-if="!hasInputNumberPrefix" label="Input base" label-placement="left" label-width="110" :show-feedback="false">
|
||||
<n-form-item label="Input base" label-placement="left" label-width="110" :show-feedback="false">
|
||||
<n-input-number v-model:value="inputBase" max="64" min="2" placeholder="Put your input base here (ex: 10)" w-full />
|
||||
</n-form-item>
|
||||
|
||||
|
||||
Reference in new issue
Block a user