From e51164f922e69f2f77aa66d681d8c69258636e84 Mon Sep 17 00:00:00 2001 From: Daniel Jun Suguimoto Date: Thu, 20 Aug 2020 19:06:26 -0300 Subject: [PATCH] feat: Adding max and min functions --- src/index.js | 16 +++++++++++ test/unit/timecode.static.js | 52 ++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/src/index.js b/src/index.js index 14ab495..a47fbc5 100644 --- a/src/index.js +++ b/src/index.js @@ -189,4 +189,20 @@ SMPTE.isFramerateSupported = function (framerate) { return SMPTE.supportedFrameRates.includes(framerate); }; +/** + * Gets the maximum timecode + * @param {Array.} args List of timecode objects + */ +SMPTE.max = function (...args) { + return args.reduce((max, timecode) => timecode.frameCount > max.frameCount ? timecode : max); +}; + +/** + * Gets the minimum timecode + * @param {Array.} args List of timecode objects + */ +SMPTE.min = function (...args) { + return args.reduce((min, timecode) => timecode.frameCount < min.frameCount ? timecode : min); +}; + export default SMPTE; diff --git a/test/unit/timecode.static.js b/test/unit/timecode.static.js index 3f9215c..9ac2016 100644 --- a/test/unit/timecode.static.js +++ b/test/unit/timecode.static.js @@ -258,4 +258,56 @@ describe('SMPTE', function () { .to.equal(new SMPTE(timecode, FrameRate.FR_60).frameCount); }); }); + + describe('.max()', function () { + it('should properly return the maximum timecode', function () { + const timecodeList = [ + new SMPTE('00:00:03:00'), + new SMPTE('01:20:01:15'), + new SMPTE('00:00:05:10'), + new SMPTE('00:01:12:13'), + new SMPTE('00:00:00:10'), + ]; + + expect(SMPTE.max(...timecodeList).toString()).to.equal('01:20:01:15'); + }); + + it('should properly return the maximum timecode (multiple framerate)', function () { + const timecodeList = [ + new SMPTE('00:00:03:00', FrameRate.FR_30), + new SMPTE('01:20:01:15', FrameRate.FR_59_94), + new SMPTE('00:00:05:10', FrameRate.FR_25), + new SMPTE('00:01:12;13', FrameRate.FR_29_97, true), + new SMPTE('00:00:00:10', FrameRate.FR_24), + ]; + + expect(SMPTE.max(...timecodeList).toString()).to.equal('01:20:01:15'); + }); + }); + + describe('.min()', function () { + it('should properly return the minimum timecode', function () { + const timecodeList = [ + new SMPTE('00:00:03:00'), + new SMPTE('01:20:01:15'), + new SMPTE('00:00:05:10'), + new SMPTE('00:01:12:13'), + new SMPTE('00:00:00:10'), + ]; + + expect(SMPTE.min(...timecodeList).toString()).to.equal('00:00:00:10'); + }); + + it('should properly return the minimum timecode (multiple framerate)', function () { + const timecodeList = [ + new SMPTE('00:00:03:00', FrameRate.FR_30), + new SMPTE('01:20:01:15', FrameRate.FR_59_94), + new SMPTE('00:00:05:10', FrameRate.FR_25), + new SMPTE('00:01:12;13', FrameRate.FR_29_97, true), + new SMPTE('00:00:00:10', FrameRate.FR_24), + ]; + + expect(SMPTE.min(...timecodeList).toString()).to.equal('00:00:00:10'); + }); + }); });