-
Notifications
You must be signed in to change notification settings - Fork 45
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
0a3384e
commit 9308e89
Showing
4 changed files
with
98 additions
and
7 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,56 @@ | ||
import {getMockToken} from '~/src/test-utilities' | ||
import {durationToCss} from './durationToCss' | ||
|
||
describe('Transformer: durationToCss', () => { | ||
it('transforms `duration` token to css duration', () => { | ||
const input = getMockToken({ | ||
value: '100ms', | ||
}) | ||
|
||
const expectedOutput = '100ms' | ||
expect(durationToCss.transformer(input, {})).toStrictEqual(expectedOutput) | ||
}) | ||
|
||
it('transforms `ms` to `s`', () => { | ||
const input = getMockToken({ | ||
value: '1252ms', | ||
}) | ||
|
||
const expectedOutput = '1.252s' | ||
expect(durationToCss.transformer(input, {})).toStrictEqual(expectedOutput) | ||
}) | ||
|
||
it('throws an error when unit is missing or invalid', () => { | ||
// missing unit | ||
expect(() => | ||
durationToCss.transformer( | ||
getMockToken({ | ||
value: '1000', | ||
}), | ||
{}, | ||
), | ||
).toThrowError() | ||
|
||
// only ms unit is supported | ||
expect(() => | ||
durationToCss.transformer( | ||
getMockToken({ | ||
value: '1s', | ||
}), | ||
{}, | ||
), | ||
).toThrowError() | ||
}) | ||
|
||
it('throws an error when value is number', () => { | ||
// missing unit | ||
expect(() => | ||
durationToCss.transformer( | ||
getMockToken({ | ||
value: 1000, | ||
}), | ||
{}, | ||
), | ||
).toThrowError() | ||
}) | ||
}) |
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 StyleDictionary from 'style-dictionary' | ||
|
||
/** | ||
* @description converts duration tokens string value to number with `ms` unit | ||
* @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 duration | ||
*/ | ||
export const durationToCss: StyleDictionary.Transform = { | ||
type: `value`, | ||
transitive: true, | ||
matcher: (token: StyleDictionary.TransformedToken) => token.type === `duration`, | ||
transformer: (token: StyleDictionary.TransformedToken, _options?: StyleDictionary.Platform) => { | ||
// throw an error if token value is not a string or does not end with `ms` | ||
if (typeof token.value !== `string` || !token.value.endsWith(`ms`)) { | ||
throw new Error(`duration token value must be a string with an "ms" unit`) | ||
} | ||
// get value | ||
let value = parseInt(token.value.replace('ms', '')) | ||
let unit = `ms` | ||
if (value >= 1000) { | ||
value = value / 1000 | ||
unit = `s` | ||
} | ||
// return value | ||
return `${value}${unit}` | ||
}, | ||
} |
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