function max(list) {
return list.reduce((a, b) => a > b ? a : b);
}
function max(list) {
let max = list[0];
list.forEach(function(e) {
if (e > max) max = e;
});
return max;
}
const classPrefix = 'modal--';
Array.from(el.classList).forEach(n => el.classList.toggle(n, !!n.indexOf(classPrefix)));
el.classList.remove(...[...el.classList].filter(n => n.startsWith(classPrefix)));
for (let i = el.classList.length; i--;) {
if (el.classList[i].search(classPrefix) === 0) {
el.classList.remove(el.classList[i]);
}
}
el.classList.value = el.classList.value
.split(' ')
.filter(RegExp.prototype.test.bind(RegExp(`^(?!${classPrefix})`)))
.join(' ');
el.className = el.className.replace(RegExp(`(^| )${classPrefix}\\S*`, 'g'), '').trim();
document.querySelectorAll('.row').forEach(addClass);
// или
for (const n of document.getElementsByClassName('row')) {
addClass(n);
}
const addClass = el => el.classList.add(`row-${el.children.length}`);
const classes = {
3: 'row-3',
5: 'row-5',
7: 'row-xxx',
};
function addClass(el) {
const className = classes[el.children.length];
if (className) {
el.classList.add(className);
}
}
const classes = [
{ max: 3, name: 'row-0-3' },
{ max: 5, name: '' }, // если дочерних элементов 4 или 5, класс не добавляется
{ max: 7, name: 'row-6-7' },
{ max: Infinity, name: 'row-7-x' },
];
function addClass(el) {
const { name } = classes.find(n => n.max >= el.children.length) ?? {};
if (name) {
el.classList.add(name);
}
}
const classes = [
[ c => c < 5, 'row-0-4' ],
[ c => c % 2, 'row-odd' ],
[ c => 6 < c && c < 9, 'row-7-8' ],
];
const addClass = ({ classList, children: { length } }) =>
classList.add(...classes.reduce((acc, n) => (
n[0](length) && acc.push(n[1]),
acc
), []));