-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
34a8875
commit 2883deb
Showing
12 changed files
with
160 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import type { Alphabet } from '../types/config'; | ||
import { extractElementSizes } from '../utils/extractElementSizes'; | ||
import { defaultIcons } from './defaultIcons'; | ||
|
||
const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; | ||
|
||
describe('defaultIcons()', () => { | ||
it('contains all 26 letters of the alphabet', () => { | ||
expect(Object.keys(defaultIcons).length).toBe(letters.length); | ||
|
||
for (let i = 0; i < letters.length; i++) { | ||
const letter = letters[i]; | ||
expect(defaultIcons[letter as Alphabet]).toBeDefined(); | ||
} | ||
}); | ||
|
||
it('each icon has width and height', () => { | ||
for (let i = 0; i < letters.length; i++) { | ||
const letter = letters[i]; | ||
const svg = defaultIcons[letter as Alphabet]; | ||
|
||
const { width, height } = extractElementSizes(svg); | ||
|
||
expect(width).toBeGreaterThan(0); | ||
expect(height).toBeGreaterThan(0); | ||
} | ||
}); | ||
}); |
4 changes: 3 additions & 1 deletion
4
src/constants/svgFlagsByLetter.ts → src/constants/defaultIcons.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,35 @@ | ||
export interface MarineCodeConfig { | ||
orientation?: Orientation; | ||
offset?: number; | ||
customIcons?: Partial<Record<Alphabet, string>>; | ||
} | ||
|
||
export type Orientation = 'horizontal' | 'vertical'; | ||
|
||
export type Alphabet = | ||
| 'A' | ||
| 'B' | ||
| 'C' | ||
| 'D' | ||
| 'E' | ||
| 'F' | ||
| 'G' | ||
| 'H' | ||
| 'I' | ||
| 'J' | ||
| 'K' | ||
| 'L' | ||
| 'M' | ||
| 'N' | ||
| 'O' | ||
| 'P' | ||
| 'Q' | ||
| 'R' | ||
| 'S' | ||
| 'T' | ||
| 'U' | ||
| 'V' | ||
| 'W' | ||
| 'X' | ||
| 'Y' | ||
| 'Z'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,21 @@ | ||
import { svgFlagsByLetter } from '../constants/svgFlagsByLetter'; | ||
import { defaultIcons } from '../constants/defaultIcons'; | ||
import type { Alphabet } from '../types/config'; | ||
import { getSvgArrayFromName } from './getSvgArrayFromName'; | ||
|
||
describe('getSvgArrayFromName()', () => { | ||
it('returns an array with SVG elements for a given name', () => { | ||
const result = getSvgArrayFromName(Object.keys(svgFlagsByLetter).join('').toLowerCase()); | ||
expect(result).toHaveLength(Object.keys(svgFlagsByLetter).length); | ||
const result = getSvgArrayFromName(Object.keys(defaultIcons).join('').toLowerCase(), defaultIcons); | ||
expect(result).toHaveLength(Object.keys(defaultIcons).length); | ||
result.forEach((svgElement, index) => { | ||
expect(svgElement).toBe(svgFlagsByLetter[Object.keys(svgFlagsByLetter)[index]]); | ||
expect(svgElement).toBe(defaultIcons[Object.keys(defaultIcons)[index] as Alphabet]); | ||
}); | ||
}); | ||
|
||
it('returns an empty array for an empty name', () => { | ||
expect(getSvgArrayFromName('')).toEqual([]); | ||
expect(getSvgArrayFromName('', defaultIcons)).toEqual([]); | ||
}); | ||
|
||
it('returns an empty array for a name with no SVG letters', () => { | ||
expect(getSvgArrayFromName('123!@$423423,123842391234')).toEqual([]); | ||
expect(getSvgArrayFromName('123!@$423423,123842391234', defaultIcons)).toEqual([]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,17 @@ | ||
import { svgFlagsByLetter } from '../constants/svgFlagsByLetter'; | ||
import type { Alphabet } from '../types/config'; | ||
|
||
export function getSvgArrayFromName(name: string): string[] { | ||
export function getSvgArrayFromName(name: string, icons: Record<Alphabet, string>): string[] { | ||
if (!name) return []; | ||
|
||
return name.toUpperCase().split('').filter(isSvgLetter).map(getSvgForLetter); | ||
return (name.toUpperCase().split('') as Alphabet[]) | ||
.filter((l) => isSvgLetter(l, icons)) | ||
.map((l) => getSvgForLetter(l, icons)); | ||
} | ||
|
||
function isSvgLetter(letter: string): boolean { | ||
return letter in svgFlagsByLetter; | ||
function isSvgLetter(letter: string, icons: Record<Alphabet, string>): boolean { | ||
return letter in icons; | ||
} | ||
|
||
function getSvgForLetter(letter: string): string { | ||
return svgFlagsByLetter[letter]; | ||
function getSvgForLetter(letter: Alphabet, icons: Record<Alphabet, string>): string { | ||
return icons[letter]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters