From 722f7bd8d186e370a4dfd3c4f657dc50f089fe71 Mon Sep 17 00:00:00 2001 From: Igor Klepacki Date: Mon, 6 Sep 2021 19:21:00 +0200 Subject: [PATCH] Make helper function return Set instead of Array --- packages/faviconize/src/faviconize.ts | 8 ++++++-- packages/faviconize/src/helpers.ts | 8 ++++---- packages/faviconize/tests/helpers.test.ts | 21 +++++++++++---------- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/packages/faviconize/src/faviconize.ts b/packages/faviconize/src/faviconize.ts index bc71d28..43fdc5a 100644 --- a/packages/faviconize/src/faviconize.ts +++ b/packages/faviconize/src/faviconize.ts @@ -11,13 +11,17 @@ import { IconType } from './types' * @param {IconType | IconType[]} outputTypes Icon types to be generated. Can be a single type or an array of types. null means all types. * @param {string} outputDirectoryPath Directory where to save icons. If not specified it will be `icons/` */ -export async function faviconize(imageInput: string | Buffer, outputTypes?: IconType | IconType[], outputDirectoryPath?: string) { +export async function faviconize( + imageInput: string | Buffer, + outputTypes?: IconType | Array, + outputDirectoryPath?: string, +) { const resolvedImageInput = Buffer.isBuffer(imageInput) ? imageInput : await resolveAndCheckInputFilePath(imageInput) const normalizedOutputTypes = normalizeOutputTypes(outputTypes) const resolvedOutputPath = await resolveAndCreateOrUseOutputPath(outputDirectoryPath) for (const [type, edges] of Object.entries(iconTypesAndEdgesMap)) { - if (normalizedOutputTypes.includes(type as IconType)) { + if (normalizedOutputTypes.has(type as IconType)) { try { await Promise.all( edges.map((edge) => { diff --git a/packages/faviconize/src/helpers.ts b/packages/faviconize/src/helpers.ts index 7a49698..ec5bdf0 100644 --- a/packages/faviconize/src/helpers.ts +++ b/packages/faviconize/src/helpers.ts @@ -4,18 +4,18 @@ import fs from 'fs/promises' import { defaultOutputDirectory, iconTypesAndEdgesMap } from './constants' import { IconType, InputFileError } from './types' -export function normalizeOutputTypes(outputTypes: IconType | IconType[]): IconType[] { +export function normalizeOutputTypes(outputTypes: IconType | Array): Set { if (outputTypes) { if (outputTypes instanceof Array) { if (outputTypes.length === 0) { throw new Error('Output types must be an array of at least one element') } - return outputTypes.filter((type, index, original) => original.indexOf(type) === index) + return new Set(outputTypes) } else { - return [outputTypes] + return new Set([outputTypes]) } } else { - return Object.keys(iconTypesAndEdgesMap) as IconType[] + return new Set(Object.keys(iconTypesAndEdgesMap) as Array) } } diff --git a/packages/faviconize/tests/helpers.test.ts b/packages/faviconize/tests/helpers.test.ts index 31f8eb1..5236aa0 100644 --- a/packages/faviconize/tests/helpers.test.ts +++ b/packages/faviconize/tests/helpers.test.ts @@ -13,35 +13,36 @@ describe(normalizeOutputTypes, () => { expect(() => normalizeOutputTypes([])).toThrow() }) - it('should return an default icon output types if no output types or null are provided', () => { + it('should return a set with default icon output types if no output types or null is provided', () => { const outputTypes = normalizeOutputTypes(null) - const defaultIconTypes = Object.keys(iconTypesAndEdgesMap) as IconType[] + const defaultIconTypes = new Set(Object.keys(iconTypesAndEdgesMap) as Array) expect(outputTypes).toStrictEqual(defaultIconTypes) }) - it('should return an array with a single icon output type', () => { + it('should return a set with a single icon output type', () => { const outputTypes = normalizeOutputTypes(['icon']) - expect(outputTypes).toStrictEqual(['icon']) + expect(outputTypes).toStrictEqual>(new Set(['icon'])) }) - it('should return an array with multiple icon output types', () => { + it('should return a set with multiple icon output types', () => { const outputTypes = normalizeOutputTypes(['icon', 'apple-touch-icon']) - expect(outputTypes).toStrictEqual(['icon', 'apple-touch-icon']) + expect(outputTypes).toStrictEqual>(new Set(['icon', 'apple-touch-icon'])) }) - it('should return an array with no duplicates if there are any provided', () => { + // Do we need this? + it('should return a set with no duplicates if there are any provided', () => { const outputTypes = normalizeOutputTypes(['icon', 'apple-touch-icon', 'apple-touch-icon']) - expect(outputTypes).toStrictEqual(['icon', 'apple-touch-icon']) + expect(outputTypes).toStrictEqual>(new Set(['icon', 'apple-touch-icon'])) }) - it('should return an array with one item if single icon type is provided', () => { + it('should return a set with one item if single icon type is provided', () => { const outputTypes = normalizeOutputTypes('icon') - expect(outputTypes).toStrictEqual(['icon']) + expect(outputTypes).toStrictEqual>(new Set(['icon'])) }) })