const lexicographicCompare = (a, b) => {
const preparedA = a.trim();
const preparedB = b.trim();
const count = Math.max(preparedA.length, preparedB.length);
let index = 0;
for (let index = 0; index < count; index++) {
const diff = (preparedA.charCodeAt(index) || 0) - (preparedB.charCodeAt(index) || 0);
if (diff !== 0) {
return Math.sign(diff);
}
}
return 0;
};
const result = lexicographicCompare('111', '123');
if (result > 0) {
console.log('Second earlier');
} else if (result < 0) {
console.log('First earlier');
} else {
console.log('Equals');
}
embed.description: Must be 2048 or fewer in length.
/**
* @param {{ id : number, title: string, budget: number }} entry
* @return {{ id: number, name: string, price: number }}
*/
const transform = (entry) => ({
id: entry.id,
name: entry.title,
price: entry.budget
});
const handle = el => {
setItems(items => items.map(item => item === el
? {
...item,
show: !item.show
}
: item
));
};
for (const key of utm) {
params.delete(key);
}
const href = 'https://example.com/?user=John&some_key=1&id=3';
const url = new URL(href);
const availableKeys = ['user', 'id'];
for (const key of [...url.searchParams.keys()]) {
if (!availableKeys.includes(key)) {
url.searchParams.delete(key);
}
}
console.log(url.toString()); // 'https://example.com/?user=John&id=3'
const strings = [
'<img src="link">1234566 4553342 aaa',
'<div><img class="super-img" src="https://example.com"/></div>',
'src="1"',
'<img src=\'../img.png\'/>'
];
const expression = /img.+?src=(["'])(.+)\1/;
for (const string of strings) {
const src = (string.match(expression) || [])[2] ?? null;
console.log(string);
console.log(src);
}
/*
<img src="link">1234566 4553342 aaa
link
<div><img class="super-img" src="https://example.com"/></div>
https://example.com
src="1"
null
<img src='../img.png'/>
../img.png
*/
- document.addEventListener('mousemove', () => action('action'));
+ document.addEventListener('mousemove', action);
const string = '<tr><td>11</td><td>22</td></tr><tr><td>33</td><td>44</td></tr>';
const match = string.match(/(\d+)(?=<\/td><\/tr>)/g);
// или так: string.match(/(\d+)(?=[^\d]+?<\/tr>)/g)
// или так: string.match(/(\d+)(?=\D+?<\/tr>)/g)
console.log(match); // [ '22', '44' ]
const array = [
[
{ name: 'A', age: 10 },
{ name: 'B', age: 10 },
{ name: 'A', age: 10 },
{ name: 'B', age: 10 },
],
[
{ name: 'C', age: 10 },
{ name: 'D', age: 10 },
{ name: 'C', age: 10 },
{ name: 'D', age: 10 },
],
];
const groupBy = (collection, extractKey) => {
const cache = new Map();
return collection.reduce((accumulator, entry) => {
const key = extractKey(entry);
if (!cache.has(key)) {
const group = [];
cache.set(key, group);
accumulator.push(group);
}
cache.get(key).push(entry);
return accumulator;
}, []);
};
const newArray = array.map(entry => groupBy(entry, item => item.name));
console.log(newArray);
/*
[
[
[ { name: 'A', age: 10 }, { name: 'A', age: 10 } ],
[ { name: 'B', age: 10 }, { name: 'B', age: 10 } ]
],
[
[ { name: 'C', age: 10 }, { name: 'C', age: 10 } ],
[ { name: 'D', age: 10 }, { name: 'D', age: 10 } ]
]
]
*/
const groupBy = (collection, extractKey) => {
const cache = new Map();
for (const entry of collection) {
const key = extractKey(entry);
if (!cache.has(key)) {
cache.set(key, []);
}
cache.get(key).push(entry);
}
return [...cache.values()];
};
const api = (isError = false, callback) => !isError
? callback(null, { code: 200 })
: callback(new Error(), null);
const promisify = (handle = () => {}) => (...args) => new Promise((resolve, reject) => {
handle(...args, (error, data) => {
if (error) {
reject(error);
} else {
resolve(data);
}
});
});
promisify(api)(true).then(data => {
console.log('data', data);
}).catch(error => {
console.log('error', error);
});