iconify/tools

Want to import duplicates and import

Closed this issue ยท 2 comments

I want to disable the duplicates check during the import process and want to import the duplicate items but ignoreDuplicates: false seems has no effect.

tools.ImportDir('directory', {
    ignoreDuplicates: false,
}).then(collection => {
    //
}).catch(err => {
    //
});

How do I disable the ignoreDuplicates check and import everything including the duplicates? Any help would be highly appreciated ๐Ÿ˜€

That option is badly named, all it does is disables warning when duplicate items are found.

Each icon must have unique name. To make sure icons have unique names, you can use option keywordCallback to set custom names for icons:

const collection = await tools.ImportDir('dir', {
	keywordCallback: (key, file, options) => {
		let result = key
			.toLowerCase()
			.replace(/_/g, '-') // Replaces '_' with '_'
			.replace(/[^a-zA-Z0-9\-_:]/g, '') // Removes all invalid characters
			.replace(/--*/, '-'); // Replaces double '-' with single '-'
		return result;
	}
});

That's default function. You can create a temporary variable to track used keys and add suffixes like -2 to duplicate items:

let keywords = Object.create(null);
const collection = await tools.ImportDir('dir', {
	keywordCallback: (key, file, options) => {
		let result = key
			.toLowerCase()
			.replace(/_/g, '-') // Replaces '_' with '_'
			.replace(/[^a-zA-Z0-9\-_:]/g, '') // Removes all invalid characters
			.replace(/--*/, '-'); // Replaces double '-' with single '-'

		// Avoid duplicates
		if (keywords[result] !== void 0) {
			let count = 2;
			while (true) {
				let testKey = result + '-' + count;
				if (keywords[testKey] === void 0) {
					result = testKey;
					break;
				}
				count ++;
			}
		}

		keywords[result] = true;
		return result;
	}
});

Or you can use file argument to set custom keywords for specific files.

Yay! That did it ๐ŸŽ‰ Thanks, man. Really appreciate for the solution ๐Ÿบ