owntone-server/htdocs/assets/GroupByList.js
2023-12-14 20:00:28 +00:00

2 lines
2.0 KiB
JavaScript

import{N as p}from"./index.js";const{t:u,locale:a}=p.global,m="GROUP_KEY_NONE";function l(){return{compareFn:null,groupKeyFn:s=>m}}function y(s,r=!1,t="_"){return{compareFn:r?null:(e,n)=>{const o=e[s]||t,i=n[s]||t;return o.localeCompare(i,a.value)},groupKeyFn:e=>{const n=(e[s]||t).charAt(0);return n.match(new RegExp("\\p{Letter}","gu"))?n.toUpperCase():n.match(new RegExp("\\p{Number}","gu"))?"#":"⌘"}}}function h(s,{direction:r="asc",defaultValue:t=0}){return{compareFn:(e,n)=>{const o=e[s]||t,i=n[s]||t,c=o-i;return r==="asc"?c:c*-1},groupKeyFn:e=>{const n=e[s]||t;return Math.floor(n/10)}}}function f(s,r="asc",t=1){return{compareFn:(e,n)=>{const o=e[s]||t,i=n[s]||t,c=o-i;return r==="asc"?c:c*-1},groupKeyFn:e=>e[s]||t}}function F(s,{direction:r="asc",defaultValue:t="0000"}){return{compareFn:(e,n)=>{const o=e[s]||t,i=n[s]||t,c=o.localeCompare(i,a.value);return r==="asc"?c:c*-1},groupKeyFn:e=>(e[s]||t).substring(0,4)}}function b(s,r="0000"){return{compareFn:(t,e)=>{const n=t[s]||r;return(e[s]||r).localeCompare(n,a.value)},groupKeyFn:t=>{const e=t[s];if(!e)return r;const n=new Date().getTime()-new Date(e).getTime();return n<864e5?u("group-by-list.today"):n<6048e5?u("group-by-list.last-week"):n<2592e6?u("group-by-list.last-month"):e.substring(0,4)}}}class d{constructor({items:r=[],total:t=0,offset:e=0,limit:n=-1}={}){this.items=r,this.total=t,this.offset=e,this.limit=n,this.count=r.length,this.indexList=[],this.group(l())}isEmpty(){return!this.items||this.items.length<=0}group(r,t=[]){const e=t?this.items.filter(o=>t.every(i=>i(o))):this.items;this.count=e.length;const n=r.compareFn?[...e].sort(r.compareFn):e;this.indexList=[...new Set(n.map(r.groupKeyFn))],this.itemsGrouped=n.reduce((o,i)=>{const c=r.groupKeyFn(i);return o[c]=[...o[c]||[],i],o},{})}*generate(){for(const r in this.itemsGrouped){r!==m&&(yield{groupKey:r,itemId:r,isItem:!1,item:{}});for(const t of this.itemsGrouped[r])yield{groupKey:r,itemId:t.id,isItem:!0,item:t}}}[Symbol.iterator](){return this.generate()}}export{d as G,y as a,f as b,F as c,h as d,b as e,l as n};