From 1c5496bf65f35bf2d6512d426a3969e2a8d3c68b Mon Sep 17 00:00:00 2001 From: mg901 Date: Thu, 10 Jan 2019 15:23:55 +0300 Subject: [PATCH] refactor(modular-scale): made the function curry 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 --- src/calculators/modular-scale/index.js | 39 +++++++++++---------- src/calculators/modular-scale/index.spec.js | 6 ++-- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/src/calculators/modular-scale/index.js b/src/calculators/modular-scale/index.js index 103e193..eb89f57 100644 --- a/src/calculators/modular-scale/index.js +++ b/src/calculators/modular-scale/index.js @@ -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( @@ -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); } @@ -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 */ diff --git a/src/calculators/modular-scale/index.spec.js b/src/calculators/modular-scale/index.spec.js index f659819..b48aed6 100644 --- a/src/calculators/modular-scale/index.spec.js +++ b/src/calculators/modular-scale/index.spec.js @@ -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, + ); }); }); });