generated from wayfair-incubator/oss-template
-
Notifications
You must be signed in to change notification settings - Fork 17
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
Showing
6 changed files
with
119 additions
and
6 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
packages/rainbow-sprinkles/src/__tests__/createNormalizeValueFn.test.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { createNormalizeValueFn } from '../createNormalizeValueFn'; | ||
|
||
const responsiveArray = ['mobile', 'tablet', 'desktop']; | ||
|
||
describe('with responsive array', () => { | ||
const config = { | ||
config: { | ||
display: {}, | ||
}, | ||
responsiveArray, | ||
}; | ||
|
||
const fn = createNormalizeValueFn(config as any); | ||
|
||
it('returns conditional object given responsive array', () => { | ||
expect(fn('display', ['block', 'flex', 'grid'])).toMatchObject({ | ||
mobile: 'block', | ||
tablet: 'flex', | ||
desktop: 'grid', | ||
}); | ||
}); | ||
|
||
it('fills in missing values with last value', () => { | ||
expect(fn('display', ['block', 'flex'])).toMatchObject({ | ||
mobile: 'block', | ||
tablet: 'flex', | ||
desktop: 'flex', | ||
}); | ||
|
||
expect(fn('display', ['block', null, 'flex'])).toMatchObject({ | ||
mobile: 'block', | ||
tablet: 'block', | ||
desktop: 'flex', | ||
}); | ||
}); | ||
}); | ||
|
||
describe('without responsive array', () => { | ||
const config = { | ||
config: { | ||
display: {}, | ||
}, | ||
}; | ||
|
||
const fn = createNormalizeValueFn(config as any); | ||
|
||
it('returns original value given no responsive array', () => { | ||
expect(fn('display', ['block', 'flex', 'grid'])).toMatchObject([ | ||
'block', | ||
'flex', | ||
'grid', | ||
]); | ||
}); | ||
}); |
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,36 @@ | ||
import { DefinePropertiesReturn } from './types'; | ||
|
||
export function createNormalizeValueFn< | ||
Configs extends ReadonlyArray<DefinePropertiesReturn>, | ||
>(...configs: Configs) { | ||
const normalizeValueFn = (property: string, value: any) => { | ||
if (Array.isArray(value)) { | ||
const config = configs.find((c) => property in c.config); | ||
if (!config || !config.responsiveArray) { | ||
return value; | ||
} | ||
|
||
let lastValue: typeof value; | ||
return config.responsiveArray.reduce( | ||
(acc, key, index) => { | ||
const currentValue = value[index]; | ||
|
||
if (currentValue !== undefined && currentValue !== null) { | ||
lastValue = currentValue; | ||
} | ||
|
||
acc[key] = lastValue; | ||
|
||
return acc; | ||
}, | ||
{} as { | ||
[key in typeof config.responsiveArray[number]]: typeof value[number]; | ||
}, | ||
); | ||
} | ||
|
||
return value; | ||
}; | ||
|
||
return normalizeValueFn; | ||
} |
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