Skip to content

Commit 181a18e

Browse files
convert array of font families to string for figma (#901)
1 parent 597eb43 commit 181a18e

File tree

5 files changed

+49
-4
lines changed

5 files changed

+49
-4
lines changed

src/transformers/fontFamilyToCss.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import type StyleDictionary from 'style-dictionary'
22
import {isFontFamily} from '../filters'
33
import {getTokenValue} from './utilities/getTokenValue'
4-
5-
const hasSpaceInName = (string: string) => /\s/g.test(string)
4+
import {hasSpaceInString} from './utilities/hasSpaceInString'
65
/**
76
* takes a value and returns it if its a string or concats strings in an array quoting strings with spaces
87
* @param value
@@ -18,7 +17,7 @@ export const parseFontFamily = (value: unknown): string => {
1817
if (typeof string !== 'string') {
1918
throw new Error(`Invalid value in array ${string}, must be a string`)
2019
}
21-
return hasSpaceInName(string) ? `'${string}'` : string
20+
return hasSpaceInString(string) ? `'${string}'` : string
2221
})
2322
.join(', ')
2423
}

src/transformers/fontFamilyToFigma.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe('Transformer: fontFamilyToFigma', () => {
1515
expect(input.map(item => fontFamilyToFigma.transformer(item, {}))).toStrictEqual(expectedOutput)
1616
})
1717

18-
it.skip('transforms fontFamily array', () => {
18+
it('transforms fontFamily array', () => {
1919
const input = [
2020
getMockToken({
2121
value: ['Roboto', 'Noto Sans'],

src/transformers/fontFamilyToFigma.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type StyleDictionary from 'style-dictionary'
22
import {isFontFamily} from '../filters'
3+
import {hasSpaceInString} from './utilities/hasSpaceInString'
34
/**
45
* takes a value and returns it if its a string or concats strings in an array quoting strings with spaces
56
* @param value
@@ -17,6 +18,17 @@ export const parseFontFamily = (
1718
if (typeof token.value === 'string') {
1819
return token.value
1920
}
21+
// return stringified array
22+
if (Array.isArray(token.value)) {
23+
return token.value
24+
.map((string: string) => {
25+
if (typeof string !== 'string') {
26+
throw new Error(`Invalid value in array ${string}, must be a string`)
27+
}
28+
return hasSpaceInString(string) ? `'${string}'` : string
29+
})
30+
.join(', ')
31+
}
2032
// invalid value
2133
throw new Error(`Invalid value ${token.value}, should be a string or array of strings`)
2234
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import {hasSpaceInString} from './hasSpaceInString'
2+
3+
describe('Utilities: hasSpaceInString', () => {
4+
it('returns true if string has space', () => {
5+
expect(hasSpaceInString('string with space')).toBe(true)
6+
})
7+
8+
it('returns false if string has no space', () => {
9+
expect(hasSpaceInString('string-without-space')).toBe(false)
10+
})
11+
12+
it('throws a typeError if invalid input is used', () => {
13+
expect(() => {
14+
// @ts-expect-error due to testing wrong input
15+
hasSpaceInString(100)
16+
}).toThrow(TypeError)
17+
18+
expect(() => {
19+
// @ts-expect-error due to testing wrong input
20+
hasSpaceInString(['string'])
21+
}).toThrow(TypeError)
22+
23+
expect(() => {
24+
// @ts-expect-error due to testing wrong input
25+
hasSpaceInString({value: 'string'})
26+
}).toThrow(TypeError)
27+
})
28+
})
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const hasSpaceInString = (string: string): boolean => {
2+
if (typeof string !== 'string') {
3+
throw new TypeError(`Invalid input. Input must be a string, ${typeof string} provided`)
4+
}
5+
return /\s/g.test(string)
6+
}

0 commit comments

Comments
 (0)