Skip to content

Commit

Permalink
add cubicBezier transformer (#1079)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasoppermann authored Oct 30, 2024
1 parent de32449 commit a2b1b79
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/metal-cycles-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/primitives': patch
---

add cubicBezier transformer
22 changes: 22 additions & 0 deletions src/filters/isCubicBezier.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {getMockToken} from '../test-utilities/index.js'
import {isCubicBezier} from './isCubicBezier.js'

describe('Filter: isCubicBezier', () => {
it('returns true if $type property is `cubicBezier`', () => {
expect(isCubicBezier(getMockToken({$type: 'cubicBezier'}))).toStrictEqual(true)
})

it('returns false if $type property is not `cubicBezier`', () => {
expect(isCubicBezier(getMockToken({$type: 'pumpkin'}))).toStrictEqual(false)
})

it('returns false if $type property is missing', () => {
expect(isCubicBezier(getMockToken({alpha: 0.4}))).toStrictEqual(false)
})

it('returns false if $type property is falsy', () => {
expect(isCubicBezier(getMockToken({$type: false}))).toStrictEqual(false)
expect(isCubicBezier(getMockToken({$type: undefined}))).toStrictEqual(false)
expect(isCubicBezier(getMockToken({$type: null}))).toStrictEqual(false)
})
})
11 changes: 11 additions & 0 deletions src/filters/isCubicBezier.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type {TransformedToken} from 'style-dictionary/types'

/**
* @description Checks if token is of $type `cubicBezier`
* @param token [TransformedToken](https://github.com/amzn/style-dictionary/blob/main/types/TransformedToken.d.ts)
* @returns boolean
*/
export const isCubicBezier = (token: TransformedToken): boolean => {
const typeValue = token.$type ?? token.type
return typeValue === 'cubicBezier'
}
48 changes: 48 additions & 0 deletions src/transformers/cubicBezierToCss.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {getMockToken} from '../test-utilities/index.js'
import {cubicBezierToCss} from './cubicBezierToCss.js'

describe('Transformer: cubicBezierToCss', () => {
it('transforms `cubicBezier` token to css cubicBezier function', () => {
const input = getMockToken({
$value: [0, 0, 1, 1],
})

const expectedOutput = 'cubic-bezier(0,0,1,1)'
expect(cubicBezierToCss.transform(input, {}, {})).toStrictEqual(expectedOutput)
})

it('throws an error when required values are missing', () => {
// missing blur
expect(() =>
cubicBezierToCss.transform(
getMockToken({
$value: [0, 0, 1],
}),
{},
{},
),
).toThrowError()

// missing spread
expect(() =>
cubicBezierToCss.transform(
getMockToken({
$value: [0, 0, 1, 1, 1, 1],
}),
{},
{},
),
).toThrowError()

// missing offsets
expect(() =>
cubicBezierToCss.transform(
getMockToken({
$value: [0, 0, 1, '1'],
}),
{},
{},
),
).toThrowError()
})
})
26 changes: 26 additions & 0 deletions src/transformers/cubicBezierToCss.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type {PlatformConfig, Transform, TransformedToken} from 'style-dictionary/types'
import {isCubicBezier} from '../filters/isCubicBezier.js'

/**
* @description converts cubicBezeir tokens array value to a css cubic-bezier
* @type value transformer — [StyleDictionary.ValueTransform](https://github.com/amzn/style-dictionary/blob/main/types/Transform.d.ts)
* @matcher matches all tokens of $type `duration`
* @transformer returns a css cubic-bezier function
*/
export const cubicBezierToCss: Transform = {
name: 'cubicBezeir/css',
type: 'value',
transitive: true,
filter: isCubicBezier,
transform: (token: TransformedToken, _config: PlatformConfig) => {
const value = token.$value ?? token.value
// throw value of more or less than 4 items in array
if (value.length !== 4 || value.some((item: unknown) => typeof item !== 'number')) {
throw new Error(
`Invalid cubicBezier token ${token.path.join('.')}, must be an array with 4 numbers, but got this instead: ${JSON.stringify(value)}`,
)
}
// return value
return `cubic-bezier(${value.join(',')})`
},
}

0 comments on commit a2b1b79

Please sign in to comment.