import { parse, format } from 'date-fns';
const getDate = (date) => format(
parse(date, 'yyyy-MM-dd HH:mm:ss', new Date()),
'MMM dd, yyyy'
);
const addAdditionally = (addtive) => setActiveAddtive((currentAddtive) =>
currentAddtive.includes(addtive)
? currentAddtive.map((entry) => entry === addtive ? 1 : entry)
: [...currentAddtive, addtive]
);
<script src="main.js"></srcipt> -> <script src="main.js?1234567890"></srcipt>
button.addEventListener('click', (event) => {
event.preventDefault();
fetch('https://test.ru/?id=5&rating=5');
});
document.querySelector('.bx-soa-customer > .bx-soa-customer-field:last-child')?.classList.add('xyz');
document.querySelectorAll('.count-wrapper').forEach((wrapper) => {
const minusButton = wrapper.querySelector('.count-minus');
const plusButton = wrapper.querySelector('.count-plus');
const counterElement = wrapper.querySelector('.counter');
if (!minusButton || !plusButton || !counterElement) {
return;
}
let counter = Number(counterElement.textContent.trim()) || 0;
minusButton.addEventListener('click', (event) => {
event.preventDefault();
if (counter > 0) {
counter--;
}
counterElement.textContent = counter;
});
plusButton.addEventListener('click', (event) => {
event.preventDefault();
counter++;
counterElement.textContent = counter;
});
});
const string = '100 EUR * 10 +100 EUR - 300 EUR* 50 EUR';
const anotherString = string.replace(/\s*([-+/*])\s*/g, (match, char) => ` ${char} `);
console.log(anotherString);
interface IMain {
user: string;
val1: number;
val2: number;
val3: number;
}
type IShort = Partial<Omit<IMain, 'user'>>;
function sum(a: IMain, b: IShort) {
const keys = Object.keys(b) as (keyof IShort)[];
for (const key of keys) {
b[key] = a[key] - (b[key] ?? 0);
}
return b;
}
const makeTree = (root) => {
const tree = {};
const step = (root, branch) => {
root.querySelectorAll(':scope > [class^="block"]').forEach((element, index) => {
const className = [...element.classList.values()].find(
(className) => className.startsWith('block')
) ?? element.className;
const key = `${className}_${index + 1}`;
branch[key] = {};
const control = element.querySelector(':scope > input');
if (control) {
branch[key].value = control.value;
}
step(element, branch[key]);
});
};
step(root, tree);
return tree;
};
const tree = makeTree(document.body);
console.log(tree);
const makeTree = (root) => {
const tree = {};
const step = (root, branch) => {
root.querySelectorAll(':scope > [class^="block"]').forEach((element, index) => {
const className = [...element.classList.values()].find(
(className) => className.startsWith('block')
) ?? element.className;
const key = `${className}_${index + 1}`;
const controls = element.querySelectorAll(':scope > input');
branch[key] = {
values: [...controls].reduce((acc, control) => {
if (control.id) {
acc[control.id] = control.value;
}
return acc;
}, {})
};
step(element, branch[key]);
});
};
step(root, tree);
return tree;
};
const tree = makeTree(document.body);
console.log(tree);
const pages = Array.from({ length: Math.ceil(reposCount / elementsCount) }, (_, i) => i + 1);
const pages = new Array(Math.ceil(reposCount / elementsCount)).fill(null).map((_, i) => i + 1);
function* range(from, to) {
for (let i = from; i <= to; i++) {
yield i;
}
}
const pages = [...range(1, Math.ceil(reposCount / elementsCount))];
encodeURIComponent(
decodeURIComponent('test+%0D%0Atest.+%0D%0ADtestestestes%0D%0Atesgdsger%0D%0A')
.replace(/\+?\r\n/g, '+\r\n')
)
if (${data.title} != 'null')
такое вообще работать не будет.null
, то можно решить проблему так:moviesEl.textContent = data.title ?? 'Another title';