|
@@ -12,7 +12,7 @@
|
|
|
*
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
* GNU Affero General Public License for more details.
|
|
|
*
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
@@ -28,15 +28,31 @@ import { translate as t, translatePlural as n } from '@nextcloud/l10n'
|
|
|
* Create an unique file name
|
|
|
* @param name The initial name to use
|
|
|
* @param otherNames Other names that are already used
|
|
|
- * @param suffix A function that takes an index an returns a suffix to add, defaults to '(index)'
|
|
|
+ * @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[], suffix = (n: number) => `(${n})`): string => {
|
|
|
+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 = extname(name)
|
|
|
- newName = `${basename(name, ext)} ${suffix(i++)}${ext}`
|
|
|
+ const ext = opts.ignoreFileExtension ? '' : extname(name)
|
|
|
+ const base = basename(name, ext)
|
|
|
+ newName = `${base} ${opts.suffix(i++)}${ext}`
|
|
|
}
|
|
|
return newName
|
|
|
}
|