@dvcol/neo-svelte
Version:
Neomorphic ui library for svelte 5
72 lines (71 loc) • 2.44 kB
JavaScript
export function showDivider(divider, position = 'top') {
if (typeof divider !== 'boolean')
return divider?.[position];
return divider;
}
export function isButtonTag(tag) {
if (typeof tag === 'string')
return false;
if ('tag' in tag && (tag.tag === 'button' || tag.tag === 'a'))
return true;
return 'href' in tag || 'onclick' in tag;
}
export const isSection = (item) => 'items' in item;
export function 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 function 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;
}
export function findByValuesInList(values, array) {
if (!Array.isArray(values))
return findByValueInList(values, array);
const result = [];
values.forEach((value) => {
const item = findByValueInList(value, array);
if (item)
result.push(item);
});
return result;
}