{"version":3,"file":"filename.mjs","sources":["../../lib/utils/filename.ts"],"sourcesContent":["/**\n * SPDX-FileCopyrightText: Nextcloud GmbH and Nextcloud contributors\n * SPDX-License-Identifier: AGPL-3.0-or-later or LGPL-3.0-or-later\n */\n\nimport { basename, extname } from '@nextcloud/paths'\n\ninterface UniqueNameOptions {\n\t/**\n\t * A function that takes an index and returns a suffix to add to the file name, defaults to '(index)'\n\t *\n\t * @param index The current index to add\n\t */\n\tsuffix?: (index: number) => string\n\t/**\n\t * Set to true to ignore the file extension when adding the suffix (when getting a unique directory name)\n\t */\n\tignoreFileExtension?: boolean\n}\n\n/**\n * Create an unique file name\n *\n * @param name The initial name to use\n * @param otherNames Other names that are already used\n * @param options Optional parameters for tuning the behavior\n * @return Either the initial name, if unique, or the name with the suffix so that the name is unique\n */\nexport function getUniqueName(\n\tname: string,\n\totherNames: string[],\n\toptions?: UniqueNameOptions,\n): string {\n\tconst opts = {\n\t\tsuffix: (n: number) => `(${n})`,\n\t\tignoreFileExtension: false,\n\t\t...options,\n\t}\n\n\tlet newName = name\n\tlet i = 1\n\twhile (otherNames.includes(newName)) {\n\t\tconst ext = opts.ignoreFileExtension ? '' : extname(name)\n\t\tconst base = basename(name, ext)\n\t\tnewName = `${base} ${opts.suffix(i++)}${ext}`\n\t}\n\treturn newName\n}\n"],"names":[],"mappings":";AA4BO,SAAS,cACf,MACA,YACA,SACS;AACT,QAAM,OAAO;AAAA,IACZ,QAAQ,CAAC,MAAc,IAAI,CAAC;AAAA,IAC5B,qBAAqB;AAAA,IACrB,GAAG;AAAA,EAAA;AAGJ,MAAI,UAAU;AACd,MAAI,IAAI;AACR,SAAO,WAAW,SAAS,OAAO,GAAG;AACpC,UAAM,MAAM,KAAK,sBAAsB,KAAK,QAAQ,IAAI;AACxD,UAAM,OAAO,SAAS,MAAM,GAAG;AAC/B,cAAU,GAAG,IAAI,IAAI,KAAK,OAAO,GAAG,CAAC,GAAG,GAAG;AAAA,EAC5C;AACA,SAAO;AACR;"}