Skip to content

Commit

Permalink
refactor: Better handling of defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
Undistraction committed Jul 29, 2024
1 parent 2880487 commit f2a7690
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 33 deletions.
38 changes: 27 additions & 11 deletions src/utils/getGrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const processSteps = (steps) =>
}
})

const insertGutters = (steps, gutter = 0) => {
const insertGutters = (steps, gutter) => {
const lastStepIndex = steps.length - 1
const hasGutter = gutter > 0
return steps.reduce((acc, step, idx) => {
Expand Down Expand Up @@ -95,18 +95,33 @@ const stepIsNotGutter = (step) => !step.isGutter
// -----------------------------------------------------------------------------

const getGrid = (boundingCurves, grid) => {
// Don't destructure arg so we can pass it around as is
const gridWithDefaults = {
...{
gutter: 0,
interpolationStrategy: INTERPOLATION_STRATEGY_ID.EVEN,
precision: 20,
lineStrategy: LINE_STRATEGY_ID.STRAIGHT_LINES,
},
...grid,
}
console.log('BOUNDS', boundingCurves)
console.log('GRID', gridWithDefaults)

validateBoundingCurves(boundingCurves)
validateGrid(grid)
validateGrid(gridWithDefaults)

const { gutter } = grid
const { gutter } = gridWithDefaults

const [columns, rows] = [grid.columns, grid.rows].map(prepareSteps(gutter))
const [columns, rows] = [gridWithDefaults.columns, gridWithDefaults.rows].map(
prepareSteps(gutter)
)

// Choose the function to use for interpolating the location of a point on a
// curve.
const interpolatePointOnCurve = getInterpolationStrategy(grid)
const interpolatePointOnCurve = getInterpolationStrategy(gridWithDefaults)

const [getLineOnXAxis, getLineOnYAxis] = getLineStrategy(grid)
const [getLineOnXAxis, getLineOnYAxis] = getLineStrategy(gridWithDefaults)

const getPoint = memoize((ratioX, ratioY) => {
validateGetPointArguments(ratioX, ratioY)
Expand Down Expand Up @@ -153,13 +168,14 @@ const getGrid = (boundingCurves, grid) => {

const { xAxis, yAxis } = getLines()

const g = grid.gutter > 0 ? 2 : 1
// If there is a gutter, we need to skip over the gutter space
const gutterMultiplier = gutter > 0 ? 2 : 1

return {
top: xAxis[y * g][x],
bottom: xAxis[y * g + 1][x],
left: yAxis[x * g][y],
right: yAxis[x * g + 1][y],
top: xAxis[y * gutterMultiplier][x],
bottom: xAxis[y * gutterMultiplier + 1][x],
left: yAxis[x * gutterMultiplier][y],
right: yAxis[x * gutterMultiplier + 1][y],
}
})

Expand Down
8 changes: 1 addition & 7 deletions src/utils/interpolate/even.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ import { getDistanceBetweenPoints, roundTo10 } from '../math'
import { validateT } from '../validation'
import { interpolatePointOnCurveLinear } from './linear'

// -----------------------------------------------------------------------------
// Const
// -----------------------------------------------------------------------------

const DEFAULT_PRECISION = 20

// -----------------------------------------------------------------------------
// Utils
// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -64,7 +58,7 @@ export const interpolatePointOnCurveEvenlySpaced =
(
// Get an approximation using an arbitrary number of points. Increase for
// more accuracy at cost of performance
{ precision = DEFAULT_PRECISION } = {}
{ precision } = {}
) =>
(t, curve) => {
// Round the ratio to 10 decimal places to avoid rounding issues where the
Expand Down
10 changes: 5 additions & 5 deletions src/utils/interpolate/linear.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ const lerpPoint = (point1, point2, t) => {
return { x: lerp(point1.x, point2.x, t), y: lerp(point1.y, point2.y, t) }
}

// Alternativly: Bernstein polynomials
// Alternativly we could use Bernstein polynomials for interpolation
// return (
// startPoint[coordinate] * (-tCubed + 3 * tSquared - 3 * t + 1) +
// controlPoint1[coordinate] * (3 * tCubed - 6 * tSquared + 3 * t) +
// controlPoint2[coordinate] * (-3 * tCubed + 3 * tSquared) +
// endPoint[coordinate] * tCubed
// startPoint[coordinateName] * (-tCubed + 3 * tSquared - 3 * t + 1) +
// controlPoint1[coordinateName] * (3 * tCubed - 6 * tSquared + 3 * t) +
// controlPoint2[coordinateName] * (-3 * tCubed + 3 * tSquared) +
// endPoint[coordinateName] * tCubed
// )
const interpolate = (
t,
Expand Down
4 changes: 0 additions & 4 deletions src/utils/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,6 @@ export const validateBoundingCurves = (boundingCurves) => {
}

export const validateGrid = (grid) => {
if (isNil(grid)) {
throw new Error('You must supply a grid(Object)')
}

const { rows, columns, interpolationStrategy, lineStrategy, precision } = grid

if (isNil(columns)) {
Expand Down
6 changes: 0 additions & 6 deletions tests/getGrid.unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,6 @@ describe(`getGrid`, () => {
})

describe(`grid`, () => {
it(`throws if no grid is supplied`, () => {
expect(() => {
getGrid(boundsValid)
}).toThrow('You must supply a grid(Object)')
})

describe(`columns and rows`, () => {
it(`throws if no columns are supplied`, () => {
expect(() => {
Expand Down

0 comments on commit f2a7690

Please sign in to comment.