aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files/src/utils/fileUtils.ts
diff options
context:
space:
mode:
Diffstat (limited to 'apps/files/src/utils/fileUtils.ts')
-rw-r--r--apps/files/src/utils/fileUtils.ts47
1 files changed, 47 insertions, 0 deletions
diff --git a/apps/files/src/utils/fileUtils.ts b/apps/files/src/utils/fileUtils.ts
new file mode 100644
index 00000000000..f0b974be21d
--- /dev/null
+++ b/apps/files/src/utils/fileUtils.ts
@@ -0,0 +1,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(' ยท ')
+}