function replacer(string, expression, mapper) {
return (string.match(expression) || []).map(mapper);
}
function match(rawNumber) {
let number = parseInt(rawNumber);
if (number >= 10 && number < 30) {
return 'Третий';
} else if (number >= 30 && number < 50) {
return 'Второй';
} else if (number >= 50 && number < 100) {
return 'Первый';
} else {
return 'Что-то';
}
}
console.log(replacer('80 75 85 90 10 30 0', /\d+/g, match));
// ['Первый', 'Первый', 'Первый', 'Первый', 'Третий', 'Второй', 'Что-то']
const urls = [
'/page/profile',
'/page/article/123123',
'/page/article/new',
'/page/article',
'/page/some/nested/structure/here',
'/page/module/article/new'
];
for (const url of urls ) {
const expression = /^\/page\/(.*)?(article)(.*)?$/g;
console.log(url, expression.test(url));
}
/*
'/page/profile' false
'/page/article/123123' true
'/page/article/new' true
'/page/article' true
'/page/some/nested/structure/here' false
'/page/module/article/new' true
*/
console.log(Object.fromEntries(formData.entries()));
console.log([...formData.entries()].reduce((accumulator, [key, value]) => {
accumulator[key] = value;
return accumulator;
}, {}));
const point = (x, y) => ({ x, y });
const A = point(-1, 1);
const B = point(1, 1);
const C = point(1, -1);
const D = point(-1, -1);
const E = point(0, 0);
const side = (a, b, p) => Math.sign((b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x));
const inArea = side(A, B, E) === -1 &&
side(B, C, E) === -1 &&
side(C, D, E) === -1 &&
side(D, A, E) === -1;
console.log(inArea); // true
function point(x, y)
return { ["x"] = x, ["y"] = y }
end
function sign(number)
if (number < 0) then
return -1
elseif (number > 0) then
return 1
else
return number
end
end
function side(a, b, p)
return sign((b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x))
end
A = point(-1, 1)
B = point(1, 1)
C = point(1, -1)
D = point(-1, -1)
E = point(0, 0)
inArea = side(A, B, E) == -1 and
side(B, C, E) == -1 and
side(C, D, E) == -1 and
side(D, A, E) == -1;
print(inArea) -- true
[0; 1]
, либо в интервале [-1; 1]
, в зависимости от реализации. Если найдете ту, которая возвращает числа в интервале [-1; 1]
, тогда превращаете в интервал [0; 1]
(полученное число делите на 2 и прибавляете 0.5) и умножаете на некий коэффициент. const clone = (base, count, key = 'id') => [...new Array(count)].map((_, index) => {
const cloned = { ...base };
cloned[key] += index;
return cloned;
});
const a = {
id: 7,
type: 'apple'
};
console.log(clone(a, 8));
console.log(a);
/**
[
{ id: 7, type: 'apple' },
{ id: 8, type: 'apple' },
{ id: 9, type: 'apple' },
{ id: 10, type: 'apple' },
{ id: 11, type: 'apple' },
{ id: 12, type: 'apple' },
{ id: 13, type: 'apple' },
{ id: 14, type: 'apple' }
]
{ id: 7, type: 'apple' }
*/
const parseToDate = string => {
const [day, month, year, hour, minute] = string.match(/\d+/g).map(match => parseInt(match));
const normalizedYear = year < 2000 && year.toString().length <= 2 ? year + 2000 : year;
const date = new Date(normalizedYear, month - 1, day, hour || 0, minute || 0);
return date;
};
const strings = [
'01 02 2003 04 05',
'01 02 03 04 05',
'01/02/03 04 05',
'01-02-3 04-05',
'23-08/2019 3 4',
'1 1 1980 0 0',
'2 2 0'
];
for (const string of strings) {
const date = parseToDate(string);
console.log(date.toLocaleString());
}
/**
'01.02.2003, 04:05:00'
'01.02.2003, 04:05:00'
'01.02.2003, 04:05:00'
'01.02.2003, 04:05:00'
'23.08.2019, 03:04:00'
'01.01.1980, 00:00:00'
'02.02.2000, 00:00:00'
*/
const formatter = new Intl.DateTimeFormat('en-US', {
weekday: 'short'
});
const getMonthDays = (year, month) => {
const date = new Date(year, month + 1, 0);
const count = date.getDate();
const days = [];
for (let day = 1; day <= count; day++) {
date.setDate(day);
days.push({
week: formatter.format(date),
label: date.getDate()
});
}
return days;
};
getMonthDays(2020, 8); // Вернет дни сентября 2020 года
/**
[
{ week: 'Tue', label: 1 },
{ week: 'Wed', label: 2 },
{ week: 'Thu', label: 3 },
{ week: 'Fri', label: 4 },
{ week: 'Sat', label: 5 },
{ week: 'Sun', label: 6 },
{ week: 'Mon', label: 7 },
{ week: 'Tue', label: 8 },
{ week: 'Wed', label: 9 },
{ week: 'Thu', label: 10 },
{ week: 'Fri', label: 11 },
{ week: 'Sat', label: 12 },
{ week: 'Sun', label: 13 },
{ week: 'Mon', label: 14 },
{ week: 'Tue', label: 15 },
{ week: 'Wed', label: 16 },
{ week: 'Thu', label: 17 },
{ week: 'Fri', label: 18 },
{ week: 'Sat', label: 19 },
{ week: 'Sun', label: 20 },
{ week: 'Mon', label: 21 },
{ week: 'Tue', label: 22 },
{ week: 'Wed', label: 23 },
{ week: 'Thu', label: 24 },
{ week: 'Fri', label: 25 },
{ week: 'Sat', label: 26 },
{ week: 'Sun', label: 27 },
{ week: 'Mon', label: 28 },
{ week: 'Tue', label: 29 },
{ week: 'Wed', label: 30 }
]
*/
/**
* @param {1|2} param
*/
function temp(param) {}
/**
* @typedef {1|2} MyType
*/
/**
* @param {MyType} param
*/
function temp(param) {}
new Date(Date.parse('2020-10-30T10:00:00.000Z')); // Добавит таймзону
new Date(Date.parse('2020-10-30T10:00:00.000')); // Не добавит таймзону
const vmax = element => {
const rect = element.getBoundingClientRect();
const denominator = Math.max(innerWidth, innerHeight);
return {
width: (rect.width / denominator) * 100,
height: (rect.height / denominator) * 100
}
};
box-sizing: border-box;
чтобы ширина/высота блока корректно высчитывалась в CSS. 0--
, и естественно 0 превращается в false
, но после условия count
уменьшается. Хотите уменьшать число прямо в условии - используйте предекремент:let count = 10;
while (--count) {}
console.log(count)
const string = 'f, [, s, q, ], [abc def] jkl mno';
const matches = string.match(/(\[[\w\s]+\])|([^\s,]+)/g);
console.log(matches); // [ 'f', '[', 's', 'q', ']', '[abc def]', 'jkl', 'mno' ]