aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files/src/utils/fileUtils.ts
blob: f0b974be21dd0fda4db7be52fb650f7f12aadfbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
 * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
import { FileType, type Node } from '@nextcloud/files'
import { n } from '@nextcloud/l10n'

/**
 * Extract dir and name from file path
 *
 * @param path - The full path
 * @return [dirPath, fileName]
 */
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('/')
	return [dirPath, fileName]
}

/**
 * Generate a translated summary of an array of nodes
 *
 * @param nodes - The nodes to summarize
 * @param hidden - The number of hidden nodes
 */
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

	const summary: string[] = []
	if (fileCount > 0 || folderCount === 0) {
		const fileSummary = n('files', '%n file', '%n files', fileCount)
		summary.push(fileSummary)
	}
	if (folderCount > 0) {
		const folderSummary = n('files', '%n folder', '%n folders', folderCount)
		summary.push(folderSummary)
	}
	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 summary.join(' · ')
}