{"version":3,"file":"fileSorting.mjs","sources":["../../lib/utils/fileSorting.ts"],"sourcesContent":["/*\n * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors\n * SPDX-License-Identifier: AGPL-3.0-or-later\n */\n\nimport type { INode } from '../node/node.ts'\nimport type { SortingOrder } from './sorting.ts'\n\nimport { FileType } from '../node/fileType.ts'\nimport { orderBy } from './sorting.ts'\n\nexport const FilesSortingMode = Object.freeze({\n\tName: 'basename',\n\tModified: 'mtime',\n\tSize: 'size',\n})\n\nexport type TFilesSortingMode = typeof FilesSortingMode[keyof typeof FilesSortingMode]\n\nexport interface FilesSortingOptions {\n\t/**\n\t * They key to order the files by\n\t *\n\t * @default FilesSortingMode.Name\n\t */\n\tsortingMode?: TFilesSortingMode | string\n\n\t/**\n\t * @default 'asc'\n\t */\n\tsortingOrder?: SortingOrder\n\n\t/**\n\t * If set to true nodes marked as favorites are ordered on top of all other nodes\n\t *\n\t * @default false\n\t */\n\tsortFavoritesFirst?: boolean\n\n\t/**\n\t * If set to true folders are ordered on top of files\n\t *\n\t * @default false\n\t */\n\tsortFoldersFirst?: boolean\n}\n\n/**\n * Sort files and folders according to the sorting options\n *\n * @param nodes Nodes to sort\n * @param options Sorting options\n */\nexport function sortNodes(nodes: readonly INode[], options: FilesSortingOptions = {}): INode[] {\n\tconst sortingOptions = {\n\t\t// Default to sort by name\n\t\tsortingMode: FilesSortingMode.Name,\n\t\t// Default to sort ascending\n\t\tsortingOrder: 'asc' as const,\n\t\t...options,\n\t}\n\n\t/**\n\t * Get the basename without any extension if the current node is a file\n\t *\n\t * @param node - The node to get the basename of\n\t */\n\tfunction basename(node: INode): string {\n\t\tconst name = node.displayname || node.attributes?.displayname || node.basename || ''\n\t\tif (node.type === FileType.Folder) {\n\t\t\treturn name\n\t\t}\n\n\t\treturn name.lastIndexOf('.') > 0\n\t\t\t? name.slice(0, name.lastIndexOf('.'))\n\t\t\t: name\n\t}\n\n\tconst identifiers = [\n\t\t// 1: Sort favorites first if enabled\n\t\t...(sortingOptions.sortFavoritesFirst ? [(v: INode) => v.attributes?.favorite !== 1] : []),\n\t\t// 2: Sort folders first if sorting by name\n\t\t...(sortingOptions.sortFoldersFirst ? [(v: INode) => v.type !== 'folder'] : []),\n\t\t// 3: Use sorting mode if NOT basename (to be able to use display name too)\n\t\t...(sortingOptions.sortingMode !== FilesSortingMode.Name ? [(v: INode) => v[sortingOptions.sortingMode] ?? v.attributes[sortingOptions.sortingMode]] : []),\n\t\t// 4: Use display name if available, fallback to name\n\t\t(v: INode) => basename(v),\n\t\t// 5: Finally, use basename if all previous sorting methods failed\n\t\t(v: INode) => v.basename,\n\t]\n\tconst orders = [\n\t\t// (for 1): always sort favorites before normal files\n\t\t...(sortingOptions.sortFavoritesFirst ? ['asc'] : []),\n\t\t// (for 2): always sort folders before files\n\t\t...(sortingOptions.sortFoldersFirst ? ['asc'] : []),\n\t\t// (for 3): Reverse if sorting by mtime as mtime higher means edited more recent -> lower\n\t\t...(sortingOptions.sortingMode === FilesSortingMode.Modified ? [sortingOptions.sortingOrder === 'asc' ? 'desc' : 'asc'] : []),\n\t\t// (also for 3 so make sure not to conflict with 2 and 3)\n\t\t...(sortingOptions.sortingMode !== FilesSortingMode.Modified && sortingOptions.sortingMode !== FilesSortingMode.Name ? [sortingOptions.sortingOrder] : []),\n\t\t// for 4: use configured sorting direction\n\t\tsortingOptions.sortingOrder,\n\t\t// for 5: use configured sorting direction\n\t\tsortingOptions.sortingOrder,\n\t] as ('asc' | 'desc')[]\n\n\treturn orderBy(nodes, identifiers, orders)\n}\n"],"names":[],"mappings":";;AAWO,MAAM,mBAAmB,OAAO,OAAO;AAAA,EAC7C,MAAM;AAAA,EACN,UAAU;AAAA,EACV,MAAM;AACP,CAAC;AAsCM,SAAS,UAAU,OAAyB,UAA+B,IAAa;AAC9F,QAAM,iBAAiB;AAAA;AAAA,IAEtB,aAAa,iBAAiB;AAAA;AAAA,IAE9B,cAAc;AAAA,IACd,GAAG;AAAA,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