diff options
Diffstat (limited to 'apps/files/src/utils/fileUtils.ts')
-rw-r--r-- | apps/files/src/utils/fileUtils.ts | 73 |
1 files changed, 20 insertions, 53 deletions
diff --git a/apps/files/src/utils/fileUtils.ts b/apps/files/src/utils/fileUtils.ts index 2427e330352..f0b974be21d 100644 --- a/apps/files/src/utils/fileUtils.ts +++ b/apps/files/src/utils/fileUtils.ts @@ -2,51 +2,16 @@ * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later */ -import { basename, extname } from 'path' import { FileType, type Node } from '@nextcloud/files' -import { translate as t, translatePlural as n } from '@nextcloud/l10n' - -// TODO: move to @nextcloud/files -/** - * Create an unique file name - * @param name The initial name to use - * @param otherNames Other names that are already used - * @param options Optional parameters for tuning the behavior - * @param options.suffix A function that takes an index and returns a suffix to add to the file name, defaults to '(index)' - * @param options.ignoreFileExtension Set to true to ignore the file extension when adding the suffix (when getting a unique directory name) - * @return Either the initial name, if unique, or the name with the suffix so that the name is unique - */ -export const getUniqueName = ( - name: string, - otherNames: string[], - options: { - suffix?: (i: number) => string, - ignoreFileExtension?: boolean, - } = {}, -): string => { - const opts = { - suffix: (n: number) => `(${n})`, - ignoreFileExtension: false, - ...options, - } - - let newName = name - let i = 1 - while (otherNames.includes(newName)) { - const ext = opts.ignoreFileExtension ? '' : extname(name) - const base = basename(name, ext) - newName = `${base} ${opts.suffix(i++)}${ext}` - } - return newName -} +import { n } from '@nextcloud/l10n' /** * Extract dir and name from file path * - * @param {string} path the full path - * @return {string[]} [dirPath, fileName] + * @param path - The full path + * @return [dirPath, fileName] */ -export const extractFilePaths = function(path) { +export function extractFilePaths(path: string): [string, string] { const pathSections = path.split('/') const fileName = pathSections[pathSections.length - 1] const dirPath = pathSections.slice(0, pathSections.length - 1).join('/') @@ -55,26 +20,28 @@ export const extractFilePaths = function(path) { /** * Generate a translated summary of an array of nodes - * @param {Node[]} nodes the nodes to summarize - * @return {string} + * + * @param nodes - The nodes to summarize + * @param hidden - The number of hidden nodes */ -export const getSummaryFor = (nodes: Node[]): string => { +export function getSummaryFor(nodes: Node[], hidden = 0): string { const fileCount = nodes.filter(node => node.type === FileType.File).length const folderCount = nodes.filter(node => node.type === FileType.Folder).length - if (fileCount === 0) { - return n('files', '{folderCount} folder', '{folderCount} folders', folderCount, { folderCount }) - } else if (folderCount === 0) { - return n('files', '{fileCount} file', '{fileCount} files', fileCount, { fileCount }) + const summary: string[] = [] + if (fileCount > 0 || folderCount === 0) { + const fileSummary = n('files', '%n file', '%n files', fileCount) + summary.push(fileSummary) } - - if (fileCount === 1) { - return n('files', '1 file and {folderCount} folder', '1 file and {folderCount} folders', folderCount, { folderCount }) + if (folderCount > 0) { + const folderSummary = n('files', '%n folder', '%n folders', folderCount) + summary.push(folderSummary) } - - if (folderCount === 1) { - return n('files', '{fileCount} file and 1 folder', '{fileCount} files and 1 folder', fileCount, { fileCount }) + if (hidden > 0) { + // TRANSLATORS: This is the number of hidden files or folders + const hiddenSummary = n('files', '%n hidden', '%n hidden', hidden) + summary.push(hiddenSummary) } - return t('files', '{fileCount} files and {folderCount} folders', { fileCount, folderCount }) + return summary.join(' ยท ') } |