Skip to content

Commit

Permalink
better argument parsing and checks
Browse files Browse the repository at this point in the history
  • Loading branch information
lunelson committed Dec 4, 2018
1 parent 4fbf520 commit 05c7bb8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/__tests__/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ test('throw type errors, when arg types are wrong', () => {
});
});

test('console.warn about scaling, when durations exceed 1', () => {
test('console.warn about scaling, when ratios exceed 1', () => {

// mocking console:
// https://stackoverflow.com/a/41224462/1204994 (see comments)
Expand Down
9 changes: 5 additions & 4 deletions src/__tests__/penners.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ const PennerSplitEases = {
'sineOut': SplitEase(0, { sin: true }),
};

test('match penner equivalents, to tenth decimal', () => {

describe('match penner equivalent to tenth decimal', () => {
Object.keys(PennerSplitEases).forEach(key => {
const t = Math.random();
expect(PennerSplitEases[key](t) - PennerEases[key](t)).toBeCloseTo(0, 10);
test(key, () => {
const t = Math.random();
expect(PennerSplitEases[key](t) - PennerEases[key](t)).toBeCloseTo(0, 10);
});
});
});
26 changes: 11 additions & 15 deletions src/split-ease.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,21 @@ function isPojo(value) {
}
}

export default function SplitEase(et1 = 0.5, et2 = Math.max(1 - et1, 0), opts = {}) {
export default function SplitEase(et1, et2, opts) {

// adjust re positional args
if (typeof et2 == 'object') { opts = et2; et2 = Math.max(1 - et1, 0); }
if (typeof et2 == 'object') { opts = et2; et2 = undefined; }
if (et1 != undefined && et1 < 0) throw new RangeError(`SplitEase: ease-in ratio must be > 0; received ${et1}`);
if (et2 != undefined && et2 < 0) throw new RangeError(`SplitEase: ease-out ratio must be > 0; received ${et2}`);
if (et1 != undefined && typeof et1 != 'number') throw new TypeError(`SplitEase: ease-in ratio must be a number; received ${et1}`);
if (et2 != undefined && typeof et2 != 'number') throw new TypeError(`SplitEase: ease-out ratio must be a number; received ${et2}`);
if (opts != undefined && !isPojo(opts)) throw new TypeError(`SplitEase: options must be a plain object; received ${opts}`);

// check lower ranges
if (et1 < 0) throw new RangeError(`SplitEase: ease-in ratio must be > 0; received ${et1}`);
if (et2 < 0) throw new RangeError(`SplitEase: ease-out ratio must be > 0; received ${et2}`);
et1 = (et1 == undefined) ? 0.5 : et1;
et2 = (et2 == undefined) ? Math.max(1 - et1, 0) : et2;
opts = (opts == undefined) ? { pow: 2 } : opts;

// check types
if (typeof et1 != 'number') throw new TypeError(`SplitEase: ease-in ratio must be a number; received ${et1}`);
if (typeof et2 != 'number') throw new TypeError(`SplitEase: ease-out ratio must be a number; received ${et2}`);
if (!isPojo(opts)) throw new TypeError('SplitEase: options argument must be a plain object');

// et1 = Math.max(et1, 0);
et2 = Math.max(et2, 0);
const eSum = et1 + et2;

// check upper ranges
if (et1 > 1) console.warn(`SplitEase: ease-in ratio > 1 [${et1}]; ease-in/-out ratios will both be scaled down`);
else if (et2 > 1) console.warn(`SplitEase: ease-out ratio > 1 [${et2}]; ease-in/-out ratios will both be scaled down`);
else if (eSum > 1) console.warn(`SplitEase: total easing ratio > 1 [${eSum}]; ease-in/-out ratios will both be scaled down`);
Expand All @@ -58,7 +54,7 @@ export default function SplitEase(et1 = 0.5, et2 = Math.max(1 - et1, 0), opts =
et1 *= eScale;
et2 *= eScale;

const { pow = 2, sin } = opts;
const { pow, sin } = opts;
const curve = sin ? cosine : power;
const p = sin ? Math.PI / 2 : pow;
const ev1 = et1 > 0 ? 1 / (p / et1 - (p - 1) * (et2 / et1 + 1)) : 0;
Expand Down

0 comments on commit 05c7bb8

Please sign in to comment.