Skip to content
This repository has been archived by the owner on Feb 17, 2021. It is now read-only.

Commit

Permalink
refactor(modular-scale): made the function curry
Browse files Browse the repository at this point in the history
made the function curry. instead of root the function accepts an object of breakpoint type

BREAKING CHANGE: instead of root the function accepts an object of breakpoint type
  • Loading branch information
mg901 committed Jan 10, 2019
1 parent 49d1760 commit 1c5496b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
39 changes: 21 additions & 18 deletions src/calculators/modular-scale/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// https://www.modularscale.com/
// from https://github.com/modularscale/modularscale.com/blob/master/source/javascripts/_ms.js.erb#L27-L52

import * as R from 'ramda';
import type { Breakpoint } from '../../models';

type CalcStartPosition = (number, number[]) => number;
const calcStartPosition: CalcStartPosition = (step, base) =>
Math.round(
Expand All @@ -14,9 +17,24 @@ const calcFontSize: CalcFontSize = (step, base, ratio) =>
Math.pow(ratio, Math.floor(step / base.length));

/* eslint-disable no-param-reassign, no-plusplus */
const normalizeBases = (base, baseHigh, ratio) => {
const cloneBase = [...base];
for (let i = 1; i < cloneBase.length; i++) {
// shift up if value too low
while (cloneBase[i] / 1 < cloneBase[0] / 1) {
cloneBase[i] *= Math.pow(ratio, 1);
}
// Shift down if too high
while (cloneBase[i] / 1 >= baseHigh / 1) {
cloneBase[i] *= Math.pow(ratio, -1);
}
}
return cloneBase.sort();
};
/* eslint-enable */

type ModularScale = (number[], number, number) => number;
export const modularScale: ModularScale = (base, ratio, step) => {
type ModularScale = (number) => (Breakpoint) => number;
export const modularScale: ModularScale = (step) => ({ base, ratio }) => {
if (base.length === 1) {
return Math.pow(ratio, step) * parseFloat(base);
}
Expand All @@ -25,21 +43,6 @@ export const modularScale: ModularScale = (base, ratio, step) => {
const fontSize = calcFontSize(step, base, ratio);
const baseHigh = Math.pow(ratio, 1) * base[0];

// Normalize bases
// Find the upper bounds for base values

for (let i = 1; i < base.length; i++) {
// shift up if value too low
while (base[i] / 1 < base[0] / 1) {
base[i] *= Math.pow(ratio, 1);
}
// Shift down if too high
while (base[i] / 1 >= baseHigh / 1) {
base[i] *= Math.pow(ratio, -1);
}
}

// Return
return fontSize * base.sort()[startPosition];
return fontSize * normalizeBases(base, baseHigh, ratio)[startPosition];
};
/* eslint-enable */
6 changes: 4 additions & 2 deletions src/calculators/modular-scale/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { modularScale } from '.';
describe('calculators', () => {
describe('calcFontSize', () => {
it('calculated `font-size` if base is array of numbers', () => {
expect(modularScale([16, 33, 15], 1.5, 7)).toEqual(49.5);
expect(modularScale(7)({ base: [16, 33, 15], ratio: 1.5 })).toEqual(49.5);
});

it('calculated `font-size` if base is array of numbers', () => {
expect(modularScale([16], 1.333, 6)).toEqual(89.76372759879813);
expect(modularScale(6)({ base: [16], ratio: 1.333 })).toEqual(
89.76372759879813,
);
});
});
});

0 comments on commit 1c5496b

Please sign in to comment.