Skip to content

Commit

Permalink
Make helper function return Set instead of Array
Browse files Browse the repository at this point in the history
  • Loading branch information
neg4n committed Sep 6, 2021
1 parent 1ffc763 commit 722f7bd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
8 changes: 6 additions & 2 deletions packages/faviconize/src/faviconize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<IconType>,
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) => {
Expand Down
8 changes: 4 additions & 4 deletions packages/faviconize/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<IconType>): Set<IconType> {
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<IconType>)
}
}

Expand Down
21 changes: 11 additions & 10 deletions packages/faviconize/tests/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<IconType>)

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<IconType[]>(['icon'])
expect(outputTypes).toStrictEqual<Set<IconType>>(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<IconType[]>(['icon', 'apple-touch-icon'])
expect(outputTypes).toStrictEqual<Set<IconType>>(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<IconType[]>(['icon', 'apple-touch-icon'])
expect(outputTypes).toStrictEqual<Set<IconType>>(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<IconType[]>(['icon'])
expect(outputTypes).toStrictEqual<Set<IconType>>(new Set(['icon']))
})
})

Expand Down

0 comments on commit 722f7bd

Please sign in to comment.