@dvcol/neo-svelte
Version:
Neomorphic ui library for svelte 5
54 lines (53 loc) • 1.92 kB
JavaScript
export const showDivider = (item, position = 'top') => {
if (typeof item.divider !== 'boolean')
return item.divider?.[position];
return item.divider;
};
export const isSection = (item) => 'items' in item;
export const findByIdInList = (selection, array) => {
const result = { index: -1 };
const search = array?.some((item, index) => {
if (isSection(item)) {
// if section differs, skip
if (selection?.section?.id !== item.id)
return false;
const sectionIndex = item?.items?.findIndex(sub => Object.is(sub, selection?.item) || sub.id === selection?.item?.id);
if (sectionIndex < 0)
return false;
result.index = sectionIndex;
result.item = item.items[sectionIndex];
result.section = item;
result.sectionIndex = index;
return true;
}
if (item.id !== selection?.item?.id)
return false;
if (item?.id === undefined && !Object.is(item, selection?.item))
return false;
result.index = index;
result.item = item;
return true;
});
return search ? result : undefined;
};
export const findByValueInList = (value, array) => {
const result = { index: -1 };
const search = array.some((item, index) => {
if (isSection(item)) {
const sectionIndex = item.items?.findIndex(si => si.value === value);
if (sectionIndex < 0)
return false;
result.index = sectionIndex;
result.item = item.items[sectionIndex];
result.section = item;
result.sectionIndex = index;
return true;
}
if (item.value !== value)
return false;
result.index = index;
result.item = item;
return true;
});
return search ? result : undefined;
};