Skip to content

Commit

Permalink
Merge pull request #149 from brikou/fix_modularScale_helper
Browse files Browse the repository at this point in the history
fix(helpers): Fix modularScale computation, allow steps to be zero
  • Loading branch information
bhough committed Apr 16, 2017
2 parents 79a1353 + d4aff94 commit 496c384
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/helpers/modularScale.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type Ratio =
* }
*/
function modularScale(steps: number, base?: number|string = '1em', ratio?: Ratio = 'perfectFourth') {
if (!steps) {
if (typeof steps !== 'number') {
throw new Error('Please provide a number of steps to the modularScale helper.')
}
if (typeof ratio === 'string' && !ratioNames[ratio]) {
Expand All @@ -79,7 +79,7 @@ function modularScale(steps: number, base?: number|string = '1em', ratio?: Ratio
throw new Error(`Invalid value passed as base to modularScale, expected number or em string but got "${base}"`)
}

return `${realBase * realRatio * steps}em`
return `${realBase * (realRatio ** steps)}em`
}

export { ratioNames }
Expand Down
12 changes: 12 additions & 0 deletions src/helpers/test/__snapshots__/modularScale.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,15 @@ Object {
"font-size": "1.333em",
}
`;

exports[`modularScale should use perfect fourth and 1em base by default 2`] = `
Object {
"font-size": "1.776889em",
}
`;

exports[`modularScale should use perfect fourth and 1em base by default 3`] = `
Object {
"font-size": "1em",
}
`;
2 changes: 2 additions & 0 deletions src/helpers/test/modularScale.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ describe('modularScale', () => {

it('should use perfect fourth and 1em base by default', () => {
expect({ 'font-size': modularScale(1) }).toMatchSnapshot()
expect({ 'font-size': modularScale(2) }).toMatchSnapshot()
expect({ 'font-size': modularScale(0) }).toMatchSnapshot()
})

it('should allow adjusting the base', () => {
Expand Down

0 comments on commit 496c384

Please sign in to comment.