react-timer-and-stopwatch
Advanced tools
Comparing version 0.5.2 to 0.6.0
@@ -1,2 +0,2 @@ | ||
import{useRef as t,useEffect as i,useReducer as s}from"react";function e(){return e=Object.assign||function(t){for(var i=1;i<arguments.length;i++){var s=arguments[i];for(var e in s)Object.prototype.hasOwnProperty.call(s,e)&&(t[e]=s[e])}return t},e.apply(this,arguments)}var n,r,a;function o(t,i){switch(i.type){case"pause":return e({},t,{paused:!0});case"resume":return e({},t,{paused:!1});case"togglePause":return e({},t,{paused:!t.paused});case"updateTimer":{const s=i.payload;return e({},t,{timerText:s.getTimerText(),timerDisplayStrings:s.getTimerDisplayStrings(),timeElapsed:s.getTimeElapsed()})}case"timerIsFinished":{const s=i.payload,n=s.timerBase.directionOfTime===r.Backward?0:s.timerBase.timestamps.end;return e({},t,{timerIsFinished:!0,pastFinish:s.continueAfterFinish,timerText:s.getTimerText(n),timerDisplayStrings:s.getTimerDisplayStrings(n),timeElapsed:s.timerBase.timestamps.end})}case"resetTimer":return e({},i.payload);case"timerIsPastFinish":return e({},t,{pastFinish:!0});default:return t}}!function(t){t[t.Duration=0]="Duration",t[t.UnixTimestamp=1]="UnixTimestamp",t[t.Stopwatch=2]="Stopwatch"}(n||(n={})),function(t){t[t.Forward=0]="Forward",t[t.Backward=1]="Backward"}(r||(r={})),function(t){t[t.OnProgress=0]="OnProgress",t[t.OnTimeLeft=1]="OnTimeLeft"}(a||(a={}));class l{constructor(t){this.milliseconds="object"==typeof t?t.milliseconds:t,this.seconds="object"==typeof t?t.seconds:0,this.minutes="object"==typeof t?t.minutes:0,this.hours="object"==typeof t?t.hours:0,this.days="object"==typeof t?t.days:0}toMilliseconds(){let t=0;return this.days&&(t=24*this.days),this.hours&&(t+=this.hours),t&&(t*=60),this.minutes&&(t+=this.minutes),t&&(t*=60),this.seconds&&(t+=this.seconds),t&&(t*=1e3),this.milliseconds&&(t+=this.milliseconds),t}}class m{constructor(t,i,s,e,n){var r;if(!t.create.stopwatch&&!t.create.timerWithDuration&&!t.create.timerWithUnixTimestamp)throw new Error("useTimer requires one of options.create.stopwatch, options.create.timerWithDuration, or options.create.timerWithUnixTimestamp. None provided.");this.creationType=s,this.display=e,this.timerBase=i,this.callbacks=n,this.continueAfterFinish=i.continueAfterFinish,this.intervalRate=null!=(r=t.intervalRate)?r:1e3,this.timerIsFinished=this.timerBase.isFinished(),this.pastFinish=this.timerBase.IsPastFinish()}isFinished(){return this.timerBase.isFinished()}isPastFinish(){const t=this.timerBase.IsPastFinish();return this.pastFinish=t,t}addTime(t){const i=new l(t).toMilliseconds();this.timerBase.addTime(i)}subtractTime(t){const i=new l(t).toMilliseconds();this.timerBase.subtractTime(i)}advanceTimestamps(){this.timerBase.advanceTimestamps()}getTimerText(t){return this.display.createTimerText(void 0===t?this.timerBase.millisecondsToDisplay():t,this.pastFinish)}getTimeElapsed(){return this.timerBase.timeElapsed}getTimerDisplayNumbers(t){return this.display.createDisplayNumbers(void 0===t?this.timerBase.millisecondsToDisplay():t)}getTimerDisplayStrings(t){return this.display.createDisplayStrings(void 0===t?this.timerBase.millisecondsToDisplay():t)}fireCallbacks(){var t,i;null==(t=(i=this.callbacks).onTick)||t.call(i),this.callbacks.onProgress(this.timerBase.timeElapsed),this.creationType!==n.Stopwatch&&this.callbacks.onTimeLeft(this.timerBase.timeLeft)}}class c{constructor(t){var i,s,e,n,a;this.intervalRate=null!=(i=t.intervalRate)?i:1e3,this.continueAfterFinish=null!=(s=null==(e=t.create.timerWithUnixTimestamp)?void 0:e.continueAfterFinish)&&s,this.directionOfTime=r.Backward,this.unixTimeMilliseconds=null!=(n=null==(a=t.create.timerWithUnixTimestamp)?void 0:a.unixTimestampMilliseconds)?n:Date.now(),this.millisecondsLeft=this.unixTimeMilliseconds-Date.now(),this.timestamps={start:0,end:this.millisecondsLeft}}get timeElapsed(){return this.timestamps.start}get timeLeft(){return this.timestamps.end-this.timestamps.start}millisecondsToDisplay(){return this.timestamps.end-this.timestamps.start}advanceTimestamps(){this.timestamps.start+=this.intervalRate}isFinished(){return this.timestamps.start>=this.timestamps.end}IsPastFinish(){return this.timestamps.start>=this.timestamps.end&&this.continueAfterFinish}addTime(t){}subtractTime(t){}}class h{constructor(t){var i,s,e,n;this.IsPastFinish=()=>!1,this.intervalRate=null!=(i=t.intervalRate)?i:1e3,this.continueAfterFinish=!1;const a=new l(null!=(s=null==(e=t.create.timerWithDuration)?void 0:e.time)?s:0).toMilliseconds();this.directionOfTime=null!=(n=t.create.timerWithDuration)&&n.directionOfTimeForward?r.Forward:r.Backward,this.timestamps={start:0,end:0+a}}get timeElapsed(){return this.timestamps.start}get timeLeft(){return this.timestamps.end-this.timestamps.start}millisecondsToDisplay(){return this.directionOfTime===r.Forward?this.timestamps.start:this.timestamps.end-this.timestamps.start}advanceTimestamps(){this.timestamps.start+=this.intervalRate}isFinished(){return this.timestamps.start>=this.timestamps.end}addTime(t){const i=new l(t).toMilliseconds();this.timestamps.end+=i}subtractTime(t){const i=new l(t).toMilliseconds(),s=this.timestamps.start+i;this.timestamps.start=s>this.timestamps.end?this.timestamps.end:s}}class u{constructor(t){var i,s,e;this.timeLeft=0,this.isFinished=()=>!1,this.IsPastFinish=()=>!1,this.intervalRate=null!=(i=t.intervalRate)?i:1e3,this.continueAfterFinish=!1,this.directionOfTime=r.Forward,this.timestamps={start:null!=(s=null==(e=t.create.stopwatch)?void 0:e.startAtMilliseconds)?s:0,end:0}}get timeElapsed(){return this.timestamps.start}millisecondsToDisplay(){return this.timestamps.start}advanceTimestamps(){this.timestamps.start+=this.intervalRate}addTime(t){const i=new l(t).toMilliseconds();this.timestamps.start+=i}subtractTime(t){const i=new l(t).toMilliseconds();this.timestamps.start-=i,this.timestamps.start<0&&(this.timestamps.start=0)}}class d{constructor(t,i){var s,e,n,r,o,l;this.onTick=null!=(s=null==(e=t.callbacks)?void 0:e.onTick)?s:void 0,this.onFinish=null!=(n=null==(r=t.callbacks)?void 0:r.onFinish)?n:void 0,this.onProgressControllers=null!=(o=t.callbacks)&&o.onProgress?this.CallbackOptionsToControllers(t.callbacks.onProgress,a.OnProgress):[],this.onTimeLeftControllers=null!=(l=t.callbacks)&&l.onTimeLeft?this.CallbackOptionsToControllers(t.callbacks.onTimeLeft,a.OnTimeLeft,i):[]}CallbackOptionToController(t){return{milliseconds:new l(t.time).toMilliseconds(),callback:t.callback,fired:!1}}CallbackOptionsToControllers(t,i,s){return t.filter(t=>{if("function"==typeof t.callback){const e=new l(t.time).toMilliseconds();if(i===a.OnProgress&&e>0)return!0;if(i===a.OnTimeLeft&&void 0!==s)return s>e}return!1}).map(t=>this.CallbackOptionToController(t))}onProgress(t){if(this.onProgressControllers.length>0){let i=!1;this.onProgressControllers.forEach(s=>{if(t>=s.milliseconds)return s.callback(),s.fired=!0,void(i=!0)}),i&&(this.onProgressControllers=this.onProgressControllers.filter(t=>!t.fired))}}onTimeLeft(t){if(this.onTimeLeftControllers.length>0){let i=!1;this.onTimeLeftControllers.forEach(s=>{if(t<=s.milliseconds)return s.callback(),s.fired=!0,void(i=!0)}),i&&(this.onTimeLeftControllers=this.onTimeLeftControllers.filter(t=>!t.fired))}}}class p{constructor(t){var i;this.textOutputWithWords=!!t.textOutputWithWords,this.includeMilliseconds=null!=(i=t.includeMilliseconds)&&i}getRelevantDisplayUnits(t){return{days:t>=864e5,hours:t>=36e5,minutes:t>=6e4,seconds:t>=1e3,milliseconds:!0}}createDisplayNumbers(t){const i=Math.abs(t),s=i%1e3,e=i/1e3,n=Math.floor(e),r=e/60,a=Math.floor(r),o=r/60,l=Math.floor(o),m=Math.floor(o/24);return{milliseconds:s,seconds:this.totalOrRemainderTime(n,60,"seconds"),minutes:this.totalOrRemainderTime(a,60,"minutes"),hours:this.totalOrRemainderTime(l,24,"hours"),days:m}}createDisplayStrings(t){const i=this.createDisplayNumbers(t);return this.displayNumbersToDisplayStrings(i)}displayNumberToString(t,i){return t=Math.abs(t),i?t<100?t<10?`00${t}`:`0${t}`:String(t):this.textOutputWithWords?String(t):t<10?`0${t}`:String(t)}displayNumbersToDisplayStrings(t){return{seconds:this.displayNumberToString(t.seconds),minutes:this.displayNumberToString(t.minutes),hours:this.displayNumberToString(t.hours),days:this.displayNumberToString(t.days),milliseconds:this.displayNumberToString(t.milliseconds,!0)}}createTimerText(t,i){t=Math.abs(t);const s=this.createDisplayNumbers(t),e=this.displayNumbersToDisplayStrings(s),n=this.getRelevantDisplayUnits(t);return this.textOutputWithWords?this.createTextOutputWithWords(e,n,i):this.createTextOutputWithoutWords(e,n,i)}createTextOutputWithoutWords(t,i,s){return`${s?"-":""}${i.days?t.days+":":""}${t.hours}:${t.minutes}:${t.seconds}${this.includeMilliseconds?`:${t.milliseconds}`:""}`}createTextOutputWithWords(t,i,s){const e=s?" ago":"";let n="";const r=1===Number(t.days)?"":"s";i.days&&(n=`${t.days} day${r}, `);let a="";const o=1===Number(t.hours)?"":"s";i.hours&&(a=`${t.hours} hour${o}, `);let l="";const m=1===Number(t.minutes)?"":"s";i.minutes&&(l=`${t.minutes} minute${m}, `);let c="";const h=1===Number(t.seconds)?"":"s";i.seconds&&(c=`${t.seconds} second${h}`),i.seconds||this.includeMilliseconds||(c="0 seconds");let u="";return this.includeMilliseconds&&(u=i.seconds?`, ${t.milliseconds} milliseconds`:`${t.milliseconds} milliseconds`),`${n}${a}${l}${c}${u}${e}`}totalOrRemainderTime(t,i,s){if("days"!==s){if("seconds"===s&&t>60)return t%60?t%60:0;if("minutes"===s&&t>60)return t%60?t%60:0;if("hours"===s&&t>24)return t%24?t%24:0}return t===i?0:0===t?t:t%i?t%i:i}}function T(t){const i=function(t){var i;return t.create.timerWithDuration?n.Duration:null!=(i=t.create.timerWithUnixTimestamp)&&i.unixTimestampMilliseconds?n.UnixTimestamp:n.Stopwatch}(t),s=function(t,i){return t===n.Duration?new h(i):t===n.UnixTimestamp?new c(i):new u(i)}(i,t),e=new p(t),r=new d(t,s.timeLeft);return new m(t,s,i,e,r)}function f(t,i){return{paused:!1===t.autoplay&&i.creationType!==n.UnixTimestamp,timerIsFinished:i.timerIsFinished,timerText:i.getTimerText(),timerDisplayStrings:i.getTimerDisplayStrings(),timeElapsed:i.getTimeElapsed(),pastFinish:!1}}function y(t,i){i({type:"updateTimer",payload:t})}function g(t,i){null==t.callbacks.onFinish||t.callbacks.onFinish(),t.timerIsFinished=!0,t.pastFinish=t.continueAfterFinish,i({type:"timerIsFinished",payload:t})}function F(e){const r=t(null),a=function(t,i){return null===t.current&&(t.current=T(i)),t.current}(r,e),[l,m]=s(o,null,()=>f(e,a));return function(s,e){const n=t(function(){});i(()=>{n.current=s},[s]),i(()=>{if(null!==e){let t=setInterval(function(){n.current()},e);return()=>clearInterval(t)}},[e])}(function(){a.isFinished()&&!a.isPastFinish()||(a.advanceTimestamps(),a.fireCallbacks()),l.timerIsFinished||!a.isFinished()?y(a,m):g(a,m)},function(t,i){return t.timerBase.continueAfterFinish&&t.creationType===n.UnixTimestamp?!i.paused:!i.paused&&!i.timerIsFinished}(a,l)?a.intervalRate:null),{timerText:l.timerText,timerDisplayStrings:l.timerDisplayStrings,timeElapsed:l.timeElapsed,timerIsPaused:l.paused,timerIsFinished:l.timerIsFinished,pastFinish:l.pastFinish,pauseTimer:function(){a.creationType!==n.UnixTimestamp&&(!l.paused&&!l.timerIsFinished||a.pastFinish)&&m({type:"pause"})},resumeTimer:function(){a.creationType!==n.UnixTimestamp&&(l.paused&&!l.timerIsFinished||a.pastFinish)&&m({type:"resume"})},togglePause:function(){a.creationType!==n.UnixTimestamp&&(l.timerIsFinished&&!a.pastFinish||m({type:"togglePause"}))},resetTimer:function(){a.creationType!==n.UnixTimestamp&&(r.current=T(e),m({type:"resetTimer",payload:f(e,r.current)}))},addTime:function(t){a.creationType!==n.UnixTimestamp&&(l.timerIsFinished||(a.addTime(t),a.isFinished()?g(a,m):y(a,m)))},subtractTime:function(t){a.creationType!==n.UnixTimestamp&&(l.timerIsFinished||(a.subtractTime(t),a.isFinished()?g(a,m):y(a,m)))}}}export{F as useTimer}; | ||
import{useRef as t,useEffect as i,useReducer as s}from"react";function e(){return e=Object.assign||function(t){for(var i=1;i<arguments.length;i++){var s=arguments[i];for(var e in s)Object.prototype.hasOwnProperty.call(s,e)&&(t[e]=s[e])}return t},e.apply(this,arguments)}var n,r,a;function o(t,i){switch(i.type){case"pause":return e({},t,{paused:!0});case"resume":return e({},t,{paused:!1});case"togglePause":return e({},t,{paused:!t.paused});case"updateTimer":{const s=i.payload;return e({},t,{timerText:s.getTimerText(),timerDisplayStrings:s.getTimerDisplayStrings(),timeElapsed:s.getTimeElapsed()})}case"timerIsFinished":{const s=i.payload,n=s.timerBase.directionOfTime===r.Backward?0:s.timerBase.timestamps.end;return e({},t,{timerIsFinished:!0,pastFinish:s.continueAfterFinish,timerText:s.getTimerText(n),timerDisplayStrings:s.getTimerDisplayStrings(n),timeElapsed:s.timerBase.timestamps.end})}case"resetTimer":return e({},i.payload);case"timerIsPastFinish":return e({},t,{pastFinish:!0});default:return t}}!function(t){t[t.Duration=0]="Duration",t[t.UnixTimestamp=1]="UnixTimestamp",t[t.Stopwatch=2]="Stopwatch"}(n||(n={})),function(t){t[t.Forward=0]="Forward",t[t.Backward=1]="Backward"}(r||(r={})),function(t){t[t.OnProgress=0]="OnProgress",t[t.OnTimeLeft=1]="OnTimeLeft"}(a||(a={}));class l{constructor(t){this.milliseconds="object"==typeof t?t.milliseconds:t,this.seconds="object"==typeof t?t.seconds:0,this.minutes="object"==typeof t?t.minutes:0,this.hours="object"==typeof t?t.hours:0,this.days="object"==typeof t?t.days:0}toMilliseconds(){let t=0;return this.days&&(t=24*this.days),this.hours&&(t+=this.hours),t&&(t*=60),this.minutes&&(t+=this.minutes),t&&(t*=60),this.seconds&&(t+=this.seconds),t&&(t*=1e3),this.milliseconds&&(t+=this.milliseconds),t}}class m{constructor(t,i,s,e,n){var r;if(!t.create.stopwatch&&!t.create.timerWithDuration&&!t.create.timerWithUnixTimestamp)throw new Error("useTimer requires one of options.create.stopwatch, options.create.timerWithDuration, or options.create.timerWithUnixTimestamp. None provided.");this.creationType=s,this.display=e,this.timerBase=i,this.callbacks=n,this.continueAfterFinish=i.continueAfterFinish,this.intervalRate=null!=(r=t.intervalRate)?r:1e3,this.timerIsFinished=this.timerBase.isFinished(),this.pastFinish=this.timerBase.IsPastFinish()}isFinished(){return this.timerBase.isFinished()}isPastFinish(){const t=this.timerBase.IsPastFinish();return this.pastFinish=t,t}addTime(t){const i=new l(t).toMilliseconds();this.timerBase.addTime(i)}subtractTime(t){const i=new l(t).toMilliseconds();this.timerBase.subtractTime(i)}advanceTimestamps(){this.timerBase.advanceTimestamps()}getTimerText(t){return this.display.createTimerText(void 0===t?this.timerBase.millisecondsToDisplay():t,this.pastFinish)}getTimeElapsed(){return this.timerBase.timeElapsed}getTimerDisplayNumbers(t){return this.display.createDisplayNumbers(void 0===t?this.timerBase.millisecondsToDisplay():t)}getTimerDisplayStrings(t){return this.display.createDisplayStrings(void 0===t?this.timerBase.millisecondsToDisplay():t)}fireCallbacks(){var t,i;null==(t=(i=this.callbacks).onTick)||t.call(i),this.callbacks.onProgress(this.timerBase.timeElapsed),this.creationType!==n.Stopwatch&&this.callbacks.onTimeLeft(this.timerBase.timeLeft)}}class c{constructor(t){var i,s,e,n,a;this.intervalRate=null!=(i=t.intervalRate)?i:1e3,this.continueAfterFinish=null!=(s=null==(e=t.create.timerWithUnixTimestamp)?void 0:e.continueAfterFinish)&&s,this.directionOfTime=r.Backward,this.unixTimeMilliseconds=null!=(n=null==(a=t.create.timerWithUnixTimestamp)?void 0:a.unixTimestampMilliseconds)?n:Date.now(),this.millisecondsLeft=this.unixTimeMilliseconds-Date.now(),this.timestamps={start:0,end:this.millisecondsLeft}}get timeElapsed(){return this.timestamps.start}get timeLeft(){return this.timestamps.end-this.timestamps.start}millisecondsToDisplay(){return this.timestamps.end-this.timestamps.start}advanceTimestamps(){this.timestamps.start+=this.intervalRate}isFinished(){return this.timestamps.start>=this.timestamps.end}IsPastFinish(){return this.timestamps.start>=this.timestamps.end&&this.continueAfterFinish}addTime(t){}subtractTime(t){}}class h{constructor(t){var i,s,e,n;this.IsPastFinish=()=>!1,this.intervalRate=null!=(i=t.intervalRate)?i:1e3,this.continueAfterFinish=!1;const a=new l(null!=(s=null==(e=t.create.timerWithDuration)?void 0:e.time)?s:0).toMilliseconds();this.directionOfTime=null!=(n=t.create.timerWithDuration)&&n.directionOfTimeForward?r.Forward:r.Backward,this.timestamps={start:0,end:0+a}}get timeElapsed(){return this.timestamps.start}get timeLeft(){return this.timestamps.end-this.timestamps.start}millisecondsToDisplay(){return this.directionOfTime===r.Forward?this.timestamps.start:this.timestamps.end-this.timestamps.start}advanceTimestamps(){this.timestamps.start+=this.intervalRate}isFinished(){return this.timestamps.start>=this.timestamps.end}addTime(t){const i=new l(t).toMilliseconds();this.timestamps.end+=i}subtractTime(t){const i=new l(t).toMilliseconds(),s=this.timestamps.start+i;this.timestamps.start=s>this.timestamps.end?this.timestamps.end:s}}class u{constructor(t){var i,s,e;this.timeLeft=0,this.isFinished=()=>!1,this.IsPastFinish=()=>!1,this.intervalRate=null!=(i=t.intervalRate)?i:1e3,this.continueAfterFinish=!1,this.directionOfTime=r.Forward,this.timestamps={start:null!=(s=null==(e=t.create.stopwatch)?void 0:e.startAtMilliseconds)?s:0,end:0}}get timeElapsed(){return this.timestamps.start}millisecondsToDisplay(){return this.timestamps.start}advanceTimestamps(){this.timestamps.start+=this.intervalRate}addTime(t){const i=new l(t).toMilliseconds();this.timestamps.start+=i}subtractTime(t){const i=new l(t).toMilliseconds();this.timestamps.start-=i,this.timestamps.start<0&&(this.timestamps.start=0)}}class d{constructor(t,i){var s,e,n,r,o,l;this.onTick=null!=(s=null==(e=t.callbacks)?void 0:e.onTick)?s:void 0,this.onFinish=null!=(n=null==(r=t.callbacks)?void 0:r.onFinish)?n:void 0,this.onProgressControllers=null!=(o=t.callbacks)&&o.onProgress?this.CallbackOptionsToControllers(t.callbacks.onProgress,a.OnProgress):[],this.onTimeLeftControllers=null!=(l=t.callbacks)&&l.onTimeLeft?this.CallbackOptionsToControllers(t.callbacks.onTimeLeft,a.OnTimeLeft,i):[]}CallbackOptionToController(t){return{milliseconds:new l(t.time).toMilliseconds(),callback:t.callback,fired:!1}}CallbackOptionsToControllers(t,i,s){return t.filter(t=>{if("function"==typeof t.callback){const e=new l(t.time).toMilliseconds();if(i===a.OnProgress&&e>0)return!0;if(i===a.OnTimeLeft&&void 0!==s)return s>e}return!1}).map(t=>this.CallbackOptionToController(t))}onProgress(t){if(this.onProgressControllers.length>0){let i=!1;this.onProgressControllers.forEach(s=>{if(t>=s.milliseconds)return s.callback(),s.fired=!0,void(i=!0)}),i&&(this.onProgressControllers=this.onProgressControllers.filter(t=>!t.fired))}}onTimeLeft(t){if(this.onTimeLeftControllers.length>0){let i=!1;this.onTimeLeftControllers.forEach(s=>{if(t<=s.milliseconds)return s.callback(),s.fired=!0,void(i=!0)}),i&&(this.onTimeLeftControllers=this.onTimeLeftControllers.filter(t=>!t.fired))}}}class p{constructor(t){var i;this.textOutputWithWords=!!t.textOutputWithWords,this.includeMilliseconds=null!=(i=t.includeMilliseconds)&&i}getRelevantDisplayUnits(t){return{days:t>=864e5,hours:t>=36e5,minutes:t>=6e4,seconds:t>=1e3,milliseconds:!0}}createDisplayNumbers(t){const i=Math.abs(t),s=i%1e3,e=i/1e3,n=Math.floor(e),r=e/60,a=Math.floor(r),o=r/60,l=Math.floor(o),m=Math.floor(o/24);return{milliseconds:s,seconds:this.totalOrRemainderTime(n,60,"seconds"),minutes:this.totalOrRemainderTime(a,60,"minutes"),hours:this.totalOrRemainderTime(l,24,"hours"),days:m}}createDisplayStrings(t){const i=this.createDisplayNumbers(t);return this.displayNumbersToDisplayStrings(i)}displayNumberToString(t,i){return t=Math.abs(t),i?t<100?t<10?`00${t}`:`0${t}`:String(t):this.textOutputWithWords?String(t):t<10?`0${t}`:String(t)}displayNumbersToDisplayStrings(t){return{seconds:this.displayNumberToString(t.seconds),minutes:this.displayNumberToString(t.minutes),hours:this.displayNumberToString(t.hours),days:this.displayNumberToString(t.days),milliseconds:this.displayNumberToString(t.milliseconds,!0)}}createTimerText(t,i){t=Math.abs(t);const s=this.createDisplayNumbers(t),e=this.displayNumbersToDisplayStrings(s),n=this.getRelevantDisplayUnits(t);return this.textOutputWithWords?this.createTextOutputWithWords(e,n,i):this.createTextOutputWithoutWords(e,n,i)}createTextOutputWithoutWords(t,i,s){return`${s?"-":""}${i.days?t.days+":":""}${t.hours}:${t.minutes}:${t.seconds}${this.includeMilliseconds?`:${t.milliseconds}`:""}`}createTextOutputWithWords(t,i,s){const e=s?" ago":"";let n="";const r=1===Number(t.days)?"":"s";i.days&&(n=`${t.days} day${r}, `);let a="";const o=1===Number(t.hours)?"":"s";i.hours&&(a=`${t.hours} hour${o}, `);let l="";const m=1===Number(t.minutes)?"":"s";i.minutes&&(l=`${t.minutes} minute${m}, `);let c="";const h=1===Number(t.seconds)?"":"s";i.seconds&&(c=`${t.seconds} second${h}`),i.seconds||this.includeMilliseconds||(c="0 seconds");let u="";return this.includeMilliseconds&&(u=i.seconds?`, ${t.milliseconds} milliseconds`:`${t.milliseconds} milliseconds`),`${n}${a}${l}${c}${u}${e}`}totalOrRemainderTime(t,i,s){if("days"!==s){if("seconds"===s&&t>60)return t%60?t%60:0;if("minutes"===s&&t>60)return t%60?t%60:0;if("hours"===s&&t>24)return t%24?t%24:0}return t===i?0:0===t?t:t%i?t%i:i}}function T(t){const i=function(t){var i;return t.create.timerWithDuration?n.Duration:null!=(i=t.create.timerWithUnixTimestamp)&&i.unixTimestampMilliseconds?n.UnixTimestamp:n.Stopwatch}(t),s=function(t,i){return t===n.Duration?new h(i):t===n.UnixTimestamp?new c(i):new u(i)}(i,t),e=new p(t),r=new d(t,s.timeLeft);return new m(t,s,i,e,r)}function f(t,i){return{paused:!1===t.autoplay&&i.creationType!==n.UnixTimestamp,timerIsFinished:i.timerIsFinished,timerText:i.getTimerText(),timerDisplayStrings:i.getTimerDisplayStrings(),timeElapsed:i.getTimeElapsed(),pastFinish:!1}}function y(t,i){i({type:"updateTimer",payload:t})}function g(t,i){null==t.callbacks.onFinish||t.callbacks.onFinish(),t.timerIsFinished=!0,t.pastFinish=t.continueAfterFinish,i({type:"timerIsFinished",payload:t})}function F(r){const a=t(null),l=function(t,i){return null===t.current&&(t.current=T(i)),t.current}(a,r),[m,c]=s(o,null,()=>f(r,l));return function(s,e){const n=t(function(){});i(()=>{n.current=s},[s]),i(()=>{if(null!==e){let t=setInterval(function(){n.current()},e);return()=>clearInterval(t)}},[e])}(function(){l.isFinished()&&!l.isPastFinish()||(l.advanceTimestamps(),l.fireCallbacks()),m.timerIsFinished||!l.isFinished()?y(l,c):g(l,c)},function(t,i){return t.timerBase.continueAfterFinish&&t.creationType===n.UnixTimestamp?!i.paused:!i.paused&&!i.timerIsFinished}(l,m)?l.intervalRate:null),{timerText:m.timerText,timerDisplayStrings:m.timerDisplayStrings,timeElapsed:m.timeElapsed,timerIsPaused:m.paused,timerIsFinished:m.timerIsFinished,pastFinish:m.pastFinish,pauseTimer:function(){l.creationType!==n.UnixTimestamp&&(!m.paused&&!m.timerIsFinished||l.pastFinish)&&c({type:"pause"})},resumeTimer:function(){l.creationType!==n.UnixTimestamp&&(m.paused&&!m.timerIsFinished||l.pastFinish)&&c({type:"resume"})},togglePause:function(){l.creationType!==n.UnixTimestamp&&(m.timerIsFinished&&!l.pastFinish||c({type:"togglePause"}))},resetTimer:function(t,i){if(l.creationType===n.UnixTimestamp)return;const s=function(t,i,s){return i?s&&i.create?e({create:{}},i):e({},t,i):t}(r,t,i);a.current=T(s),c({type:"resetTimer",payload:f(s,a.current)})},addTime:function(t){l.creationType!==n.UnixTimestamp&&(m.timerIsFinished||(l.addTime(t),l.isFinished()?g(l,c):y(l,c)))},subtractTime:function(t){l.creationType!==n.UnixTimestamp&&(m.timerIsFinished||(l.subtractTime(t),l.isFinished()?g(l,c):y(l,c)))}}}export{F as useTimer}; | ||
//# sourceMappingURL=index.modern.js.map |
@@ -1,2 +0,2 @@ | ||
import{useRef as t,useEffect as i,useReducer as e}from"react";function s(t,i){for(var e=0;e<i.length;e++){var s=i[e];s.enumerable=s.enumerable||!1,s.configurable=!0,"value"in s&&(s.writable=!0),Object.defineProperty(t,s.key,s)}}function n(t,i,e){return i&&s(t.prototype,i),e&&s(t,e),t}function r(){return r=Object.assign||function(t){for(var i=1;i<arguments.length;i++){var e=arguments[i];for(var s in e)Object.prototype.hasOwnProperty.call(e,s)&&(t[s]=e[s])}return t},r.apply(this,arguments)}var a,o,u;function l(t,i){switch(i.type){case"pause":return r({},t,{paused:!0});case"resume":return r({},t,{paused:!1});case"togglePause":return r({},t,{paused:!t.paused});case"updateTimer":var e=i.payload;return r({},t,{timerText:e.getTimerText(),timerDisplayStrings:e.getTimerDisplayStrings(),timeElapsed:e.getTimeElapsed()});case"timerIsFinished":var s=i.payload,n=s.timerBase.directionOfTime===o.Backward?0:s.timerBase.timestamps.end;return r({},t,{timerIsFinished:!0,pastFinish:s.continueAfterFinish,timerText:s.getTimerText(n),timerDisplayStrings:s.getTimerDisplayStrings(n),timeElapsed:s.timerBase.timestamps.end});case"resetTimer":return r({},i.payload);case"timerIsPastFinish":return r({},t,{pastFinish:!0});default:return t}}!function(t){t[t.Duration=0]="Duration",t[t.UnixTimestamp=1]="UnixTimestamp",t[t.Stopwatch=2]="Stopwatch"}(a||(a={})),function(t){t[t.Forward=0]="Forward",t[t.Backward=1]="Backward"}(o||(o={})),function(t){t[t.OnProgress=0]="OnProgress",t[t.OnTimeLeft=1]="OnTimeLeft"}(u||(u={}));var m=/*#__PURE__*/function(){function t(t){this.milliseconds="object"==typeof t?t.milliseconds:t,this.seconds="object"==typeof t?t.seconds:0,this.minutes="object"==typeof t?t.minutes:0,this.hours="object"==typeof t?t.hours:0,this.days="object"==typeof t?t.days:0}return t.prototype.toMilliseconds=function(){var t=0;return this.days&&(t=24*this.days),this.hours&&(t+=this.hours),t&&(t*=60),this.minutes&&(t+=this.minutes),t&&(t*=60),this.seconds&&(t+=this.seconds),t&&(t*=1e3),this.milliseconds&&(t+=this.milliseconds),t},t}(),c=/*#__PURE__*/function(){function t(t,i,e,s,n){var r;if(!t.create.stopwatch&&!t.create.timerWithDuration&&!t.create.timerWithUnixTimestamp)throw new Error("useTimer requires one of options.create.stopwatch, options.create.timerWithDuration, or options.create.timerWithUnixTimestamp. None provided.");this.creationType=e,this.display=s,this.timerBase=i,this.callbacks=n,this.continueAfterFinish=i.continueAfterFinish,this.intervalRate=null!=(r=t.intervalRate)?r:1e3,this.timerIsFinished=this.timerBase.isFinished(),this.pastFinish=this.timerBase.IsPastFinish()}var i=t.prototype;return i.isFinished=function(){return this.timerBase.isFinished()},i.isPastFinish=function(){var t=this.timerBase.IsPastFinish();return this.pastFinish=t,t},i.addTime=function(t){var i=new m(t).toMilliseconds();this.timerBase.addTime(i)},i.subtractTime=function(t){var i=new m(t).toMilliseconds();this.timerBase.subtractTime(i)},i.advanceTimestamps=function(){this.timerBase.advanceTimestamps()},i.getTimerText=function(t){return this.display.createTimerText(void 0===t?this.timerBase.millisecondsToDisplay():t,this.pastFinish)},i.getTimeElapsed=function(){return this.timerBase.timeElapsed},i.getTimerDisplayNumbers=function(t){return this.display.createDisplayNumbers(void 0===t?this.timerBase.millisecondsToDisplay():t)},i.getTimerDisplayStrings=function(t){return this.display.createDisplayStrings(void 0===t?this.timerBase.millisecondsToDisplay():t)},i.fireCallbacks=function(){var t,i;null==(t=(i=this.callbacks).onTick)||t.call(i),this.callbacks.onProgress(this.timerBase.timeElapsed),this.creationType!==a.Stopwatch&&this.callbacks.onTimeLeft(this.timerBase.timeLeft)},t}(),h=/*#__PURE__*/function(){function t(t){var i,e,s,n,r;this.intervalRate=null!=(i=t.intervalRate)?i:1e3,this.continueAfterFinish=null!=(e=null==(s=t.create.timerWithUnixTimestamp)?void 0:s.continueAfterFinish)&&e,this.directionOfTime=o.Backward,this.unixTimeMilliseconds=null!=(n=null==(r=t.create.timerWithUnixTimestamp)?void 0:r.unixTimestampMilliseconds)?n:Date.now(),this.millisecondsLeft=this.unixTimeMilliseconds-Date.now(),this.timestamps={start:0,end:this.millisecondsLeft}}var i=t.prototype;return i.millisecondsToDisplay=function(){return this.timestamps.end-this.timestamps.start},i.advanceTimestamps=function(){this.timestamps.start+=this.intervalRate},i.isFinished=function(){return this.timestamps.start>=this.timestamps.end},i.IsPastFinish=function(){return this.timestamps.start>=this.timestamps.end&&this.continueAfterFinish},i.addTime=function(t){},i.subtractTime=function(t){},n(t,[{key:"timeElapsed",get:function(){return this.timestamps.start}},{key:"timeLeft",get:function(){return this.timestamps.end-this.timestamps.start}}]),t}(),d=/*#__PURE__*/function(){function t(t){var i,e,s,n;this.IsPastFinish=function(){return!1},this.intervalRate=null!=(i=t.intervalRate)?i:1e3,this.continueAfterFinish=!1;var r=new m(null!=(e=null==(s=t.create.timerWithDuration)?void 0:s.time)?e:0).toMilliseconds();this.directionOfTime=null!=(n=t.create.timerWithDuration)&&n.directionOfTimeForward?o.Forward:o.Backward,this.timestamps={start:0,end:0+r}}var i=t.prototype;return i.millisecondsToDisplay=function(){return this.directionOfTime===o.Forward?this.timestamps.start:this.timestamps.end-this.timestamps.start},i.advanceTimestamps=function(){this.timestamps.start+=this.intervalRate},i.isFinished=function(){return this.timestamps.start>=this.timestamps.end},i.addTime=function(t){var i=new m(t).toMilliseconds();this.timestamps.end+=i},i.subtractTime=function(t){var i=new m(t).toMilliseconds(),e=this.timestamps.start+i;this.timestamps.start=e>this.timestamps.end?this.timestamps.end:e},n(t,[{key:"timeElapsed",get:function(){return this.timestamps.start}},{key:"timeLeft",get:function(){return this.timestamps.end-this.timestamps.start}}]),t}(),p=/*#__PURE__*/function(){function t(t){var i,e,s;this.timeLeft=0,this.isFinished=function(){return!1},this.IsPastFinish=function(){return!1},this.intervalRate=null!=(i=t.intervalRate)?i:1e3,this.continueAfterFinish=!1,this.directionOfTime=o.Forward,this.timestamps={start:null!=(e=null==(s=t.create.stopwatch)?void 0:s.startAtMilliseconds)?e:0,end:0}}var i=t.prototype;return i.millisecondsToDisplay=function(){return this.timestamps.start},i.advanceTimestamps=function(){this.timestamps.start+=this.intervalRate},i.addTime=function(t){var i=new m(t).toMilliseconds();this.timestamps.start+=i},i.subtractTime=function(t){var i=new m(t).toMilliseconds();this.timestamps.start-=i,this.timestamps.start<0&&(this.timestamps.start=0)},n(t,[{key:"timeElapsed",get:function(){return this.timestamps.start}}]),t}(),f=/*#__PURE__*/function(){function t(t,i){var e,s,n,r,a,o;this.onTick=null!=(e=null==(s=t.callbacks)?void 0:s.onTick)?e:void 0,this.onFinish=null!=(n=null==(r=t.callbacks)?void 0:r.onFinish)?n:void 0,this.onProgressControllers=null!=(a=t.callbacks)&&a.onProgress?this.CallbackOptionsToControllers(t.callbacks.onProgress,u.OnProgress):[],this.onTimeLeftControllers=null!=(o=t.callbacks)&&o.onTimeLeft?this.CallbackOptionsToControllers(t.callbacks.onTimeLeft,u.OnTimeLeft,i):[]}var i=t.prototype;return i.CallbackOptionToController=function(t){return{milliseconds:new m(t.time).toMilliseconds(),callback:t.callback,fired:!1}},i.CallbackOptionsToControllers=function(t,i,e){var s=this;return t.filter(function(t){if("function"==typeof t.callback){var s=new m(t.time).toMilliseconds();if(i===u.OnProgress&&s>0)return!0;if(i===u.OnTimeLeft&&void 0!==e)return e>s}return!1}).map(function(t){return s.CallbackOptionToController(t)})},i.onProgress=function(t){if(this.onProgressControllers.length>0){var i=!1;this.onProgressControllers.forEach(function(e){if(t>=e.milliseconds)return e.callback(),e.fired=!0,void(i=!0)}),i&&(this.onProgressControllers=this.onProgressControllers.filter(function(t){return!t.fired}))}},i.onTimeLeft=function(t){if(this.onTimeLeftControllers.length>0){var i=!1;this.onTimeLeftControllers.forEach(function(e){if(t<=e.milliseconds)return e.callback(),e.fired=!0,void(i=!0)}),i&&(this.onTimeLeftControllers=this.onTimeLeftControllers.filter(function(t){return!t.fired}))}},t}(),T=/*#__PURE__*/function(){function t(t){var i;this.textOutputWithWords=!!t.textOutputWithWords,this.includeMilliseconds=null!=(i=t.includeMilliseconds)&&i}var i=t.prototype;return i.getRelevantDisplayUnits=function(t){return{days:t>=864e5,hours:t>=36e5,minutes:t>=6e4,seconds:t>=1e3,milliseconds:!0}},i.createDisplayNumbers=function(t){var i=Math.abs(t),e=i%1e3,s=i/1e3,n=Math.floor(s),r=s/60,a=Math.floor(r),o=r/60,u=Math.floor(o),l=Math.floor(o/24);return{milliseconds:e,seconds:this.totalOrRemainderTime(n,60,"seconds"),minutes:this.totalOrRemainderTime(a,60,"minutes"),hours:this.totalOrRemainderTime(u,24,"hours"),days:l}},i.createDisplayStrings=function(t){var i=this.createDisplayNumbers(t);return this.displayNumbersToDisplayStrings(i)},i.displayNumberToString=function(t,i){return t=Math.abs(t),i?t<100?t<10?"00"+t:"0"+t:String(t):this.textOutputWithWords?String(t):t<10?"0"+t:String(t)},i.displayNumbersToDisplayStrings=function(t){return{seconds:this.displayNumberToString(t.seconds),minutes:this.displayNumberToString(t.minutes),hours:this.displayNumberToString(t.hours),days:this.displayNumberToString(t.days),milliseconds:this.displayNumberToString(t.milliseconds,!0)}},i.createTimerText=function(t,i){t=Math.abs(t);var e=this.createDisplayNumbers(t),s=this.displayNumbersToDisplayStrings(e),n=this.getRelevantDisplayUnits(t);return this.textOutputWithWords?this.createTextOutputWithWords(s,n,i):this.createTextOutputWithoutWords(s,n,i)},i.createTextOutputWithoutWords=function(t,i,e){return(e?"-":"")+(i.days?t.days+":":"")+t.hours+":"+t.minutes+":"+t.seconds+(this.includeMilliseconds?":"+t.milliseconds:"")},i.createTextOutputWithWords=function(t,i,e){var s=e?" ago":"",n="",r=1===Number(t.days)?"":"s";i.days&&(n=t.days+" day"+r+", ");var a="",o=1===Number(t.hours)?"":"s";i.hours&&(a=t.hours+" hour"+o+", ");var u="",l=1===Number(t.minutes)?"":"s";i.minutes&&(u=t.minutes+" minute"+l+", ");var m="",c=1===Number(t.seconds)?"":"s";i.seconds&&(m=t.seconds+" second"+c),i.seconds||this.includeMilliseconds||(m="0 seconds");var h="";return this.includeMilliseconds&&(h=i.seconds?", "+t.milliseconds+" milliseconds":t.milliseconds+" milliseconds"),""+n+a+u+m+h+s},i.totalOrRemainderTime=function(t,i,e){if("days"!==e){if("seconds"===e&&t>60)return t%60?t%60:0;if("minutes"===e&&t>60)return t%60?t%60:0;if("hours"===e&&t>24)return t%24?t%24:0}return t===i?0:0===t?t:t%i?t%i:i},t}();function y(t){var i=function(t){var i;return t.create.timerWithDuration?a.Duration:null!=(i=t.create.timerWithUnixTimestamp)&&i.unixTimestampMilliseconds?a.UnixTimestamp:a.Stopwatch}(t),e=function(t,i){return t===a.Duration?new d(i):t===a.UnixTimestamp?new h(i):new p(i)}(i,t),s=new T(t),n=new f(t,e.timeLeft);return new c(t,e,i,s,n)}function v(t,i){return{paused:!1===t.autoplay&&i.creationType!==a.UnixTimestamp,timerIsFinished:i.timerIsFinished,timerText:i.getTimerText(),timerDisplayStrings:i.getTimerDisplayStrings(),timeElapsed:i.getTimeElapsed(),pastFinish:!1}}function g(t,i){i({type:"updateTimer",payload:t})}function F(t,i){null==t.callbacks.onFinish||t.callbacks.onFinish(),t.timerIsFinished=!0,t.pastFinish=t.continueAfterFinish,i({type:"timerIsFinished",payload:t})}function b(s){var n,r,o,u=t(null),m=function(t,i){return null===t.current&&(t.current=y(i)),t.current}(u,s),c=e(l,null,function(){return v(s,m)}),h=c[0],d=c[1];return n=function(){m.isFinished()&&!m.isPastFinish()||(m.advanceTimestamps(),m.fireCallbacks()),h.timerIsFinished||!m.isFinished()?g(m,d):F(m,d)},r=function(t,i){return t.timerBase.continueAfterFinish&&t.creationType===a.UnixTimestamp?!i.paused:!i.paused&&!i.timerIsFinished}(m,h)?m.intervalRate:null,o=t(function(){}),i(function(){o.current=n},[n]),i(function(){if(null!==r){var t=setInterval(function(){o.current()},r);return function(){return clearInterval(t)}}},[r]),{timerText:h.timerText,timerDisplayStrings:h.timerDisplayStrings,timeElapsed:h.timeElapsed,timerIsPaused:h.paused,timerIsFinished:h.timerIsFinished,pastFinish:h.pastFinish,pauseTimer:function(){m.creationType!==a.UnixTimestamp&&(!h.paused&&!h.timerIsFinished||m.pastFinish)&&d({type:"pause"})},resumeTimer:function(){m.creationType!==a.UnixTimestamp&&(h.paused&&!h.timerIsFinished||m.pastFinish)&&d({type:"resume"})},togglePause:function(){m.creationType!==a.UnixTimestamp&&(h.timerIsFinished&&!m.pastFinish||d({type:"togglePause"}))},resetTimer:function(){m.creationType!==a.UnixTimestamp&&(u.current=y(s),d({type:"resetTimer",payload:v(s,u.current)}))},addTime:function(t){m.creationType!==a.UnixTimestamp&&(h.timerIsFinished||(m.addTime(t),m.isFinished()?F(m,d):g(m,d)))},subtractTime:function(t){m.creationType!==a.UnixTimestamp&&(h.timerIsFinished||(m.subtractTime(t),m.isFinished()?F(m,d):g(m,d)))}}}export{b as useTimer}; | ||
import{useRef as t,useEffect as i,useReducer as e}from"react";function s(t,i){for(var e=0;e<i.length;e++){var s=i[e];s.enumerable=s.enumerable||!1,s.configurable=!0,"value"in s&&(s.writable=!0),Object.defineProperty(t,s.key,s)}}function n(t,i,e){return i&&s(t.prototype,i),e&&s(t,e),t}function r(){return r=Object.assign||function(t){for(var i=1;i<arguments.length;i++){var e=arguments[i];for(var s in e)Object.prototype.hasOwnProperty.call(e,s)&&(t[s]=e[s])}return t},r.apply(this,arguments)}var a,o,u;function l(t,i){switch(i.type){case"pause":return r({},t,{paused:!0});case"resume":return r({},t,{paused:!1});case"togglePause":return r({},t,{paused:!t.paused});case"updateTimer":var e=i.payload;return r({},t,{timerText:e.getTimerText(),timerDisplayStrings:e.getTimerDisplayStrings(),timeElapsed:e.getTimeElapsed()});case"timerIsFinished":var s=i.payload,n=s.timerBase.directionOfTime===o.Backward?0:s.timerBase.timestamps.end;return r({},t,{timerIsFinished:!0,pastFinish:s.continueAfterFinish,timerText:s.getTimerText(n),timerDisplayStrings:s.getTimerDisplayStrings(n),timeElapsed:s.timerBase.timestamps.end});case"resetTimer":return r({},i.payload);case"timerIsPastFinish":return r({},t,{pastFinish:!0});default:return t}}!function(t){t[t.Duration=0]="Duration",t[t.UnixTimestamp=1]="UnixTimestamp",t[t.Stopwatch=2]="Stopwatch"}(a||(a={})),function(t){t[t.Forward=0]="Forward",t[t.Backward=1]="Backward"}(o||(o={})),function(t){t[t.OnProgress=0]="OnProgress",t[t.OnTimeLeft=1]="OnTimeLeft"}(u||(u={}));var m=/*#__PURE__*/function(){function t(t){this.milliseconds="object"==typeof t?t.milliseconds:t,this.seconds="object"==typeof t?t.seconds:0,this.minutes="object"==typeof t?t.minutes:0,this.hours="object"==typeof t?t.hours:0,this.days="object"==typeof t?t.days:0}return t.prototype.toMilliseconds=function(){var t=0;return this.days&&(t=24*this.days),this.hours&&(t+=this.hours),t&&(t*=60),this.minutes&&(t+=this.minutes),t&&(t*=60),this.seconds&&(t+=this.seconds),t&&(t*=1e3),this.milliseconds&&(t+=this.milliseconds),t},t}(),c=/*#__PURE__*/function(){function t(t,i,e,s,n){var r;if(!t.create.stopwatch&&!t.create.timerWithDuration&&!t.create.timerWithUnixTimestamp)throw new Error("useTimer requires one of options.create.stopwatch, options.create.timerWithDuration, or options.create.timerWithUnixTimestamp. None provided.");this.creationType=e,this.display=s,this.timerBase=i,this.callbacks=n,this.continueAfterFinish=i.continueAfterFinish,this.intervalRate=null!=(r=t.intervalRate)?r:1e3,this.timerIsFinished=this.timerBase.isFinished(),this.pastFinish=this.timerBase.IsPastFinish()}var i=t.prototype;return i.isFinished=function(){return this.timerBase.isFinished()},i.isPastFinish=function(){var t=this.timerBase.IsPastFinish();return this.pastFinish=t,t},i.addTime=function(t){var i=new m(t).toMilliseconds();this.timerBase.addTime(i)},i.subtractTime=function(t){var i=new m(t).toMilliseconds();this.timerBase.subtractTime(i)},i.advanceTimestamps=function(){this.timerBase.advanceTimestamps()},i.getTimerText=function(t){return this.display.createTimerText(void 0===t?this.timerBase.millisecondsToDisplay():t,this.pastFinish)},i.getTimeElapsed=function(){return this.timerBase.timeElapsed},i.getTimerDisplayNumbers=function(t){return this.display.createDisplayNumbers(void 0===t?this.timerBase.millisecondsToDisplay():t)},i.getTimerDisplayStrings=function(t){return this.display.createDisplayStrings(void 0===t?this.timerBase.millisecondsToDisplay():t)},i.fireCallbacks=function(){var t,i;null==(t=(i=this.callbacks).onTick)||t.call(i),this.callbacks.onProgress(this.timerBase.timeElapsed),this.creationType!==a.Stopwatch&&this.callbacks.onTimeLeft(this.timerBase.timeLeft)},t}(),h=/*#__PURE__*/function(){function t(t){var i,e,s,n,r;this.intervalRate=null!=(i=t.intervalRate)?i:1e3,this.continueAfterFinish=null!=(e=null==(s=t.create.timerWithUnixTimestamp)?void 0:s.continueAfterFinish)&&e,this.directionOfTime=o.Backward,this.unixTimeMilliseconds=null!=(n=null==(r=t.create.timerWithUnixTimestamp)?void 0:r.unixTimestampMilliseconds)?n:Date.now(),this.millisecondsLeft=this.unixTimeMilliseconds-Date.now(),this.timestamps={start:0,end:this.millisecondsLeft}}var i=t.prototype;return i.millisecondsToDisplay=function(){return this.timestamps.end-this.timestamps.start},i.advanceTimestamps=function(){this.timestamps.start+=this.intervalRate},i.isFinished=function(){return this.timestamps.start>=this.timestamps.end},i.IsPastFinish=function(){return this.timestamps.start>=this.timestamps.end&&this.continueAfterFinish},i.addTime=function(t){},i.subtractTime=function(t){},n(t,[{key:"timeElapsed",get:function(){return this.timestamps.start}},{key:"timeLeft",get:function(){return this.timestamps.end-this.timestamps.start}}]),t}(),d=/*#__PURE__*/function(){function t(t){var i,e,s,n;this.IsPastFinish=function(){return!1},this.intervalRate=null!=(i=t.intervalRate)?i:1e3,this.continueAfterFinish=!1;var r=new m(null!=(e=null==(s=t.create.timerWithDuration)?void 0:s.time)?e:0).toMilliseconds();this.directionOfTime=null!=(n=t.create.timerWithDuration)&&n.directionOfTimeForward?o.Forward:o.Backward,this.timestamps={start:0,end:0+r}}var i=t.prototype;return i.millisecondsToDisplay=function(){return this.directionOfTime===o.Forward?this.timestamps.start:this.timestamps.end-this.timestamps.start},i.advanceTimestamps=function(){this.timestamps.start+=this.intervalRate},i.isFinished=function(){return this.timestamps.start>=this.timestamps.end},i.addTime=function(t){var i=new m(t).toMilliseconds();this.timestamps.end+=i},i.subtractTime=function(t){var i=new m(t).toMilliseconds(),e=this.timestamps.start+i;this.timestamps.start=e>this.timestamps.end?this.timestamps.end:e},n(t,[{key:"timeElapsed",get:function(){return this.timestamps.start}},{key:"timeLeft",get:function(){return this.timestamps.end-this.timestamps.start}}]),t}(),p=/*#__PURE__*/function(){function t(t){var i,e,s;this.timeLeft=0,this.isFinished=function(){return!1},this.IsPastFinish=function(){return!1},this.intervalRate=null!=(i=t.intervalRate)?i:1e3,this.continueAfterFinish=!1,this.directionOfTime=o.Forward,this.timestamps={start:null!=(e=null==(s=t.create.stopwatch)?void 0:s.startAtMilliseconds)?e:0,end:0}}var i=t.prototype;return i.millisecondsToDisplay=function(){return this.timestamps.start},i.advanceTimestamps=function(){this.timestamps.start+=this.intervalRate},i.addTime=function(t){var i=new m(t).toMilliseconds();this.timestamps.start+=i},i.subtractTime=function(t){var i=new m(t).toMilliseconds();this.timestamps.start-=i,this.timestamps.start<0&&(this.timestamps.start=0)},n(t,[{key:"timeElapsed",get:function(){return this.timestamps.start}}]),t}(),f=/*#__PURE__*/function(){function t(t,i){var e,s,n,r,a,o;this.onTick=null!=(e=null==(s=t.callbacks)?void 0:s.onTick)?e:void 0,this.onFinish=null!=(n=null==(r=t.callbacks)?void 0:r.onFinish)?n:void 0,this.onProgressControllers=null!=(a=t.callbacks)&&a.onProgress?this.CallbackOptionsToControllers(t.callbacks.onProgress,u.OnProgress):[],this.onTimeLeftControllers=null!=(o=t.callbacks)&&o.onTimeLeft?this.CallbackOptionsToControllers(t.callbacks.onTimeLeft,u.OnTimeLeft,i):[]}var i=t.prototype;return i.CallbackOptionToController=function(t){return{milliseconds:new m(t.time).toMilliseconds(),callback:t.callback,fired:!1}},i.CallbackOptionsToControllers=function(t,i,e){var s=this;return t.filter(function(t){if("function"==typeof t.callback){var s=new m(t.time).toMilliseconds();if(i===u.OnProgress&&s>0)return!0;if(i===u.OnTimeLeft&&void 0!==e)return e>s}return!1}).map(function(t){return s.CallbackOptionToController(t)})},i.onProgress=function(t){if(this.onProgressControllers.length>0){var i=!1;this.onProgressControllers.forEach(function(e){if(t>=e.milliseconds)return e.callback(),e.fired=!0,void(i=!0)}),i&&(this.onProgressControllers=this.onProgressControllers.filter(function(t){return!t.fired}))}},i.onTimeLeft=function(t){if(this.onTimeLeftControllers.length>0){var i=!1;this.onTimeLeftControllers.forEach(function(e){if(t<=e.milliseconds)return e.callback(),e.fired=!0,void(i=!0)}),i&&(this.onTimeLeftControllers=this.onTimeLeftControllers.filter(function(t){return!t.fired}))}},t}(),T=/*#__PURE__*/function(){function t(t){var i;this.textOutputWithWords=!!t.textOutputWithWords,this.includeMilliseconds=null!=(i=t.includeMilliseconds)&&i}var i=t.prototype;return i.getRelevantDisplayUnits=function(t){return{days:t>=864e5,hours:t>=36e5,minutes:t>=6e4,seconds:t>=1e3,milliseconds:!0}},i.createDisplayNumbers=function(t){var i=Math.abs(t),e=i%1e3,s=i/1e3,n=Math.floor(s),r=s/60,a=Math.floor(r),o=r/60,u=Math.floor(o),l=Math.floor(o/24);return{milliseconds:e,seconds:this.totalOrRemainderTime(n,60,"seconds"),minutes:this.totalOrRemainderTime(a,60,"minutes"),hours:this.totalOrRemainderTime(u,24,"hours"),days:l}},i.createDisplayStrings=function(t){var i=this.createDisplayNumbers(t);return this.displayNumbersToDisplayStrings(i)},i.displayNumberToString=function(t,i){return t=Math.abs(t),i?t<100?t<10?"00"+t:"0"+t:String(t):this.textOutputWithWords?String(t):t<10?"0"+t:String(t)},i.displayNumbersToDisplayStrings=function(t){return{seconds:this.displayNumberToString(t.seconds),minutes:this.displayNumberToString(t.minutes),hours:this.displayNumberToString(t.hours),days:this.displayNumberToString(t.days),milliseconds:this.displayNumberToString(t.milliseconds,!0)}},i.createTimerText=function(t,i){t=Math.abs(t);var e=this.createDisplayNumbers(t),s=this.displayNumbersToDisplayStrings(e),n=this.getRelevantDisplayUnits(t);return this.textOutputWithWords?this.createTextOutputWithWords(s,n,i):this.createTextOutputWithoutWords(s,n,i)},i.createTextOutputWithoutWords=function(t,i,e){return(e?"-":"")+(i.days?t.days+":":"")+t.hours+":"+t.minutes+":"+t.seconds+(this.includeMilliseconds?":"+t.milliseconds:"")},i.createTextOutputWithWords=function(t,i,e){var s=e?" ago":"",n="",r=1===Number(t.days)?"":"s";i.days&&(n=t.days+" day"+r+", ");var a="",o=1===Number(t.hours)?"":"s";i.hours&&(a=t.hours+" hour"+o+", ");var u="",l=1===Number(t.minutes)?"":"s";i.minutes&&(u=t.minutes+" minute"+l+", ");var m="",c=1===Number(t.seconds)?"":"s";i.seconds&&(m=t.seconds+" second"+c),i.seconds||this.includeMilliseconds||(m="0 seconds");var h="";return this.includeMilliseconds&&(h=i.seconds?", "+t.milliseconds+" milliseconds":t.milliseconds+" milliseconds"),""+n+a+u+m+h+s},i.totalOrRemainderTime=function(t,i,e){if("days"!==e){if("seconds"===e&&t>60)return t%60?t%60:0;if("minutes"===e&&t>60)return t%60?t%60:0;if("hours"===e&&t>24)return t%24?t%24:0}return t===i?0:0===t?t:t%i?t%i:i},t}();function y(t){var i=function(t){var i;return t.create.timerWithDuration?a.Duration:null!=(i=t.create.timerWithUnixTimestamp)&&i.unixTimestampMilliseconds?a.UnixTimestamp:a.Stopwatch}(t),e=function(t,i){return t===a.Duration?new d(i):t===a.UnixTimestamp?new h(i):new p(i)}(i,t),s=new T(t),n=new f(t,e.timeLeft);return new c(t,e,i,s,n)}function v(t,i){return{paused:!1===t.autoplay&&i.creationType!==a.UnixTimestamp,timerIsFinished:i.timerIsFinished,timerText:i.getTimerText(),timerDisplayStrings:i.getTimerDisplayStrings(),timeElapsed:i.getTimeElapsed(),pastFinish:!1}}function g(t,i){i({type:"updateTimer",payload:t})}function F(t,i){null==t.callbacks.onFinish||t.callbacks.onFinish(),t.timerIsFinished=!0,t.pastFinish=t.continueAfterFinish,i({type:"timerIsFinished",payload:t})}function b(s){var n,o,u,m=t(null),c=function(t,i){return null===t.current&&(t.current=y(i)),t.current}(m,s),h=e(l,null,function(){return v(s,c)}),d=h[0],p=h[1];return n=function(){c.isFinished()&&!c.isPastFinish()||(c.advanceTimestamps(),c.fireCallbacks()),d.timerIsFinished||!c.isFinished()?g(c,p):F(c,p)},o=function(t,i){return t.timerBase.continueAfterFinish&&t.creationType===a.UnixTimestamp?!i.paused:!i.paused&&!i.timerIsFinished}(c,d)?c.intervalRate:null,u=t(function(){}),i(function(){u.current=n},[n]),i(function(){if(null!==o){var t=setInterval(function(){u.current()},o);return function(){return clearInterval(t)}}},[o]),{timerText:d.timerText,timerDisplayStrings:d.timerDisplayStrings,timeElapsed:d.timeElapsed,timerIsPaused:d.paused,timerIsFinished:d.timerIsFinished,pastFinish:d.pastFinish,pauseTimer:function(){c.creationType!==a.UnixTimestamp&&(!d.paused&&!d.timerIsFinished||c.pastFinish)&&p({type:"pause"})},resumeTimer:function(){c.creationType!==a.UnixTimestamp&&(d.paused&&!d.timerIsFinished||c.pastFinish)&&p({type:"resume"})},togglePause:function(){c.creationType!==a.UnixTimestamp&&(d.timerIsFinished&&!c.pastFinish||p({type:"togglePause"}))},resetTimer:function(t,i){if(c.creationType!==a.UnixTimestamp){var e=function(t,i,e){return i?e&&i.create?r({create:{}},i):r({},t,i):t}(s,t,i);m.current=y(e),p({type:"resetTimer",payload:v(e,m.current)})}},addTime:function(t){c.creationType!==a.UnixTimestamp&&(d.timerIsFinished||(c.addTime(t),c.isFinished()?F(c,p):g(c,p)))},subtractTime:function(t){c.creationType!==a.UnixTimestamp&&(d.timerIsFinished||(c.subtractTime(t),c.isFinished()?F(c,p):g(c,p)))}}}export{b as useTimer}; | ||
//# sourceMappingURL=index.module.js.map |
@@ -1,2 +0,2 @@ | ||
!function(t,i){"object"==typeof exports&&"undefined"!=typeof module?i(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],i):i((t||self).reactTimerAndStopwatch={},t.react)}(this,function(t,i){function e(t,i){for(var e=0;e<i.length;e++){var s=i[e];s.enumerable=s.enumerable||!1,s.configurable=!0,"value"in s&&(s.writable=!0),Object.defineProperty(t,s.key,s)}}function s(t,i,s){return i&&e(t.prototype,i),s&&e(t,s),t}function n(){return n=Object.assign||function(t){for(var i=1;i<arguments.length;i++){var e=arguments[i];for(var s in e)Object.prototype.hasOwnProperty.call(e,s)&&(t[s]=e[s])}return t},n.apply(this,arguments)}var r,a,o;function u(t,i){switch(i.type){case"pause":return n({},t,{paused:!0});case"resume":return n({},t,{paused:!1});case"togglePause":return n({},t,{paused:!t.paused});case"updateTimer":var e=i.payload;return n({},t,{timerText:e.getTimerText(),timerDisplayStrings:e.getTimerDisplayStrings(),timeElapsed:e.getTimeElapsed()});case"timerIsFinished":var s=i.payload,r=s.timerBase.directionOfTime===a.Backward?0:s.timerBase.timestamps.end;return n({},t,{timerIsFinished:!0,pastFinish:s.continueAfterFinish,timerText:s.getTimerText(r),timerDisplayStrings:s.getTimerDisplayStrings(r),timeElapsed:s.timerBase.timestamps.end});case"resetTimer":return n({},i.payload);case"timerIsPastFinish":return n({},t,{pastFinish:!0});default:return t}}!function(t){t[t.Duration=0]="Duration",t[t.UnixTimestamp=1]="UnixTimestamp",t[t.Stopwatch=2]="Stopwatch"}(r||(r={})),function(t){t[t.Forward=0]="Forward",t[t.Backward=1]="Backward"}(a||(a={})),function(t){t[t.OnProgress=0]="OnProgress",t[t.OnTimeLeft=1]="OnTimeLeft"}(o||(o={}));var l=/*#__PURE__*/function(){function t(t){this.milliseconds="object"==typeof t?t.milliseconds:t,this.seconds="object"==typeof t?t.seconds:0,this.minutes="object"==typeof t?t.minutes:0,this.hours="object"==typeof t?t.hours:0,this.days="object"==typeof t?t.days:0}return t.prototype.toMilliseconds=function(){var t=0;return this.days&&(t=24*this.days),this.hours&&(t+=this.hours),t&&(t*=60),this.minutes&&(t+=this.minutes),t&&(t*=60),this.seconds&&(t+=this.seconds),t&&(t*=1e3),this.milliseconds&&(t+=this.milliseconds),t},t}(),m=/*#__PURE__*/function(){function t(t,i,e,s,n){var r;if(!t.create.stopwatch&&!t.create.timerWithDuration&&!t.create.timerWithUnixTimestamp)throw new Error("useTimer requires one of options.create.stopwatch, options.create.timerWithDuration, or options.create.timerWithUnixTimestamp. None provided.");this.creationType=e,this.display=s,this.timerBase=i,this.callbacks=n,this.continueAfterFinish=i.continueAfterFinish,this.intervalRate=null!=(r=t.intervalRate)?r:1e3,this.timerIsFinished=this.timerBase.isFinished(),this.pastFinish=this.timerBase.IsPastFinish()}var i=t.prototype;return i.isFinished=function(){return this.timerBase.isFinished()},i.isPastFinish=function(){var t=this.timerBase.IsPastFinish();return this.pastFinish=t,t},i.addTime=function(t){var i=new l(t).toMilliseconds();this.timerBase.addTime(i)},i.subtractTime=function(t){var i=new l(t).toMilliseconds();this.timerBase.subtractTime(i)},i.advanceTimestamps=function(){this.timerBase.advanceTimestamps()},i.getTimerText=function(t){return this.display.createTimerText(void 0===t?this.timerBase.millisecondsToDisplay():t,this.pastFinish)},i.getTimeElapsed=function(){return this.timerBase.timeElapsed},i.getTimerDisplayNumbers=function(t){return this.display.createDisplayNumbers(void 0===t?this.timerBase.millisecondsToDisplay():t)},i.getTimerDisplayStrings=function(t){return this.display.createDisplayStrings(void 0===t?this.timerBase.millisecondsToDisplay():t)},i.fireCallbacks=function(){var t,i;null==(t=(i=this.callbacks).onTick)||t.call(i),this.callbacks.onProgress(this.timerBase.timeElapsed),this.creationType!==r.Stopwatch&&this.callbacks.onTimeLeft(this.timerBase.timeLeft)},t}(),c=/*#__PURE__*/function(){function t(t){var i,e,s,n,r;this.intervalRate=null!=(i=t.intervalRate)?i:1e3,this.continueAfterFinish=null!=(e=null==(s=t.create.timerWithUnixTimestamp)?void 0:s.continueAfterFinish)&&e,this.directionOfTime=a.Backward,this.unixTimeMilliseconds=null!=(n=null==(r=t.create.timerWithUnixTimestamp)?void 0:r.unixTimestampMilliseconds)?n:Date.now(),this.millisecondsLeft=this.unixTimeMilliseconds-Date.now(),this.timestamps={start:0,end:this.millisecondsLeft}}var i=t.prototype;return i.millisecondsToDisplay=function(){return this.timestamps.end-this.timestamps.start},i.advanceTimestamps=function(){this.timestamps.start+=this.intervalRate},i.isFinished=function(){return this.timestamps.start>=this.timestamps.end},i.IsPastFinish=function(){return this.timestamps.start>=this.timestamps.end&&this.continueAfterFinish},i.addTime=function(t){},i.subtractTime=function(t){},s(t,[{key:"timeElapsed",get:function(){return this.timestamps.start}},{key:"timeLeft",get:function(){return this.timestamps.end-this.timestamps.start}}]),t}(),h=/*#__PURE__*/function(){function t(t){var i,e,s,n;this.IsPastFinish=function(){return!1},this.intervalRate=null!=(i=t.intervalRate)?i:1e3,this.continueAfterFinish=!1;var r=new l(null!=(e=null==(s=t.create.timerWithDuration)?void 0:s.time)?e:0).toMilliseconds();this.directionOfTime=null!=(n=t.create.timerWithDuration)&&n.directionOfTimeForward?a.Forward:a.Backward,this.timestamps={start:0,end:0+r}}var i=t.prototype;return i.millisecondsToDisplay=function(){return this.directionOfTime===a.Forward?this.timestamps.start:this.timestamps.end-this.timestamps.start},i.advanceTimestamps=function(){this.timestamps.start+=this.intervalRate},i.isFinished=function(){return this.timestamps.start>=this.timestamps.end},i.addTime=function(t){var i=new l(t).toMilliseconds();this.timestamps.end+=i},i.subtractTime=function(t){var i=new l(t).toMilliseconds(),e=this.timestamps.start+i;this.timestamps.start=e>this.timestamps.end?this.timestamps.end:e},s(t,[{key:"timeElapsed",get:function(){return this.timestamps.start}},{key:"timeLeft",get:function(){return this.timestamps.end-this.timestamps.start}}]),t}(),d=/*#__PURE__*/function(){function t(t){var i,e,s;this.timeLeft=0,this.isFinished=function(){return!1},this.IsPastFinish=function(){return!1},this.intervalRate=null!=(i=t.intervalRate)?i:1e3,this.continueAfterFinish=!1,this.directionOfTime=a.Forward,this.timestamps={start:null!=(e=null==(s=t.create.stopwatch)?void 0:s.startAtMilliseconds)?e:0,end:0}}var i=t.prototype;return i.millisecondsToDisplay=function(){return this.timestamps.start},i.advanceTimestamps=function(){this.timestamps.start+=this.intervalRate},i.addTime=function(t){var i=new l(t).toMilliseconds();this.timestamps.start+=i},i.subtractTime=function(t){var i=new l(t).toMilliseconds();this.timestamps.start-=i,this.timestamps.start<0&&(this.timestamps.start=0)},s(t,[{key:"timeElapsed",get:function(){return this.timestamps.start}}]),t}(),p=/*#__PURE__*/function(){function t(t,i){var e,s,n,r,a,u;this.onTick=null!=(e=null==(s=t.callbacks)?void 0:s.onTick)?e:void 0,this.onFinish=null!=(n=null==(r=t.callbacks)?void 0:r.onFinish)?n:void 0,this.onProgressControllers=null!=(a=t.callbacks)&&a.onProgress?this.CallbackOptionsToControllers(t.callbacks.onProgress,o.OnProgress):[],this.onTimeLeftControllers=null!=(u=t.callbacks)&&u.onTimeLeft?this.CallbackOptionsToControllers(t.callbacks.onTimeLeft,o.OnTimeLeft,i):[]}var i=t.prototype;return i.CallbackOptionToController=function(t){return{milliseconds:new l(t.time).toMilliseconds(),callback:t.callback,fired:!1}},i.CallbackOptionsToControllers=function(t,i,e){var s=this;return t.filter(function(t){if("function"==typeof t.callback){var s=new l(t.time).toMilliseconds();if(i===o.OnProgress&&s>0)return!0;if(i===o.OnTimeLeft&&void 0!==e)return e>s}return!1}).map(function(t){return s.CallbackOptionToController(t)})},i.onProgress=function(t){if(this.onProgressControllers.length>0){var i=!1;this.onProgressControllers.forEach(function(e){if(t>=e.milliseconds)return e.callback(),e.fired=!0,void(i=!0)}),i&&(this.onProgressControllers=this.onProgressControllers.filter(function(t){return!t.fired}))}},i.onTimeLeft=function(t){if(this.onTimeLeftControllers.length>0){var i=!1;this.onTimeLeftControllers.forEach(function(e){if(t<=e.milliseconds)return e.callback(),e.fired=!0,void(i=!0)}),i&&(this.onTimeLeftControllers=this.onTimeLeftControllers.filter(function(t){return!t.fired}))}},t}(),f=/*#__PURE__*/function(){function t(t){var i;this.textOutputWithWords=!!t.textOutputWithWords,this.includeMilliseconds=null!=(i=t.includeMilliseconds)&&i}var i=t.prototype;return i.getRelevantDisplayUnits=function(t){return{days:t>=864e5,hours:t>=36e5,minutes:t>=6e4,seconds:t>=1e3,milliseconds:!0}},i.createDisplayNumbers=function(t){var i=Math.abs(t),e=i%1e3,s=i/1e3,n=Math.floor(s),r=s/60,a=Math.floor(r),o=r/60,u=Math.floor(o),l=Math.floor(o/24);return{milliseconds:e,seconds:this.totalOrRemainderTime(n,60,"seconds"),minutes:this.totalOrRemainderTime(a,60,"minutes"),hours:this.totalOrRemainderTime(u,24,"hours"),days:l}},i.createDisplayStrings=function(t){var i=this.createDisplayNumbers(t);return this.displayNumbersToDisplayStrings(i)},i.displayNumberToString=function(t,i){return t=Math.abs(t),i?t<100?t<10?"00"+t:"0"+t:String(t):this.textOutputWithWords?String(t):t<10?"0"+t:String(t)},i.displayNumbersToDisplayStrings=function(t){return{seconds:this.displayNumberToString(t.seconds),minutes:this.displayNumberToString(t.minutes),hours:this.displayNumberToString(t.hours),days:this.displayNumberToString(t.days),milliseconds:this.displayNumberToString(t.milliseconds,!0)}},i.createTimerText=function(t,i){t=Math.abs(t);var e=this.createDisplayNumbers(t),s=this.displayNumbersToDisplayStrings(e),n=this.getRelevantDisplayUnits(t);return this.textOutputWithWords?this.createTextOutputWithWords(s,n,i):this.createTextOutputWithoutWords(s,n,i)},i.createTextOutputWithoutWords=function(t,i,e){return(e?"-":"")+(i.days?t.days+":":"")+t.hours+":"+t.minutes+":"+t.seconds+(this.includeMilliseconds?":"+t.milliseconds:"")},i.createTextOutputWithWords=function(t,i,e){var s=e?" ago":"",n="",r=1===Number(t.days)?"":"s";i.days&&(n=t.days+" day"+r+", ");var a="",o=1===Number(t.hours)?"":"s";i.hours&&(a=t.hours+" hour"+o+", ");var u="",l=1===Number(t.minutes)?"":"s";i.minutes&&(u=t.minutes+" minute"+l+", ");var m="",c=1===Number(t.seconds)?"":"s";i.seconds&&(m=t.seconds+" second"+c),i.seconds||this.includeMilliseconds||(m="0 seconds");var h="";return this.includeMilliseconds&&(h=i.seconds?", "+t.milliseconds+" milliseconds":t.milliseconds+" milliseconds"),""+n+a+u+m+h+s},i.totalOrRemainderTime=function(t,i,e){if("days"!==e){if("seconds"===e&&t>60)return t%60?t%60:0;if("minutes"===e&&t>60)return t%60?t%60:0;if("hours"===e&&t>24)return t%24?t%24:0}return t===i?0:0===t?t:t%i?t%i:i},t}();function T(t){var i=function(t){var i;return t.create.timerWithDuration?r.Duration:null!=(i=t.create.timerWithUnixTimestamp)&&i.unixTimestampMilliseconds?r.UnixTimestamp:r.Stopwatch}(t),e=function(t,i){return t===r.Duration?new h(i):t===r.UnixTimestamp?new c(i):new d(i)}(i,t),s=new f(t),n=new p(t,e.timeLeft);return new m(t,e,i,s,n)}function y(t,i){return{paused:!1===t.autoplay&&i.creationType!==r.UnixTimestamp,timerIsFinished:i.timerIsFinished,timerText:i.getTimerText(),timerDisplayStrings:i.getTimerDisplayStrings(),timeElapsed:i.getTimeElapsed(),pastFinish:!1}}function v(t,i){i({type:"updateTimer",payload:t})}function g(t,i){null==t.callbacks.onFinish||t.callbacks.onFinish(),t.timerIsFinished=!0,t.pastFinish=t.continueAfterFinish,i({type:"timerIsFinished",payload:t})}t.useTimer=function(t){var e,s,n,a=i.useRef(null),o=function(t,i){return null===t.current&&(t.current=T(i)),t.current}(a,t),l=i.useReducer(u,null,function(){return y(t,o)}),m=l[0],c=l[1];return e=function(){o.isFinished()&&!o.isPastFinish()||(o.advanceTimestamps(),o.fireCallbacks()),m.timerIsFinished||!o.isFinished()?v(o,c):g(o,c)},s=function(t,i){return t.timerBase.continueAfterFinish&&t.creationType===r.UnixTimestamp?!i.paused:!i.paused&&!i.timerIsFinished}(o,m)?o.intervalRate:null,n=i.useRef(function(){}),i.useEffect(function(){n.current=e},[e]),i.useEffect(function(){if(null!==s){var t=setInterval(function(){n.current()},s);return function(){return clearInterval(t)}}},[s]),{timerText:m.timerText,timerDisplayStrings:m.timerDisplayStrings,timeElapsed:m.timeElapsed,timerIsPaused:m.paused,timerIsFinished:m.timerIsFinished,pastFinish:m.pastFinish,pauseTimer:function(){o.creationType!==r.UnixTimestamp&&(!m.paused&&!m.timerIsFinished||o.pastFinish)&&c({type:"pause"})},resumeTimer:function(){o.creationType!==r.UnixTimestamp&&(m.paused&&!m.timerIsFinished||o.pastFinish)&&c({type:"resume"})},togglePause:function(){o.creationType!==r.UnixTimestamp&&(m.timerIsFinished&&!o.pastFinish||c({type:"togglePause"}))},resetTimer:function(){o.creationType!==r.UnixTimestamp&&(a.current=T(t),c({type:"resetTimer",payload:y(t,a.current)}))},addTime:function(t){o.creationType!==r.UnixTimestamp&&(m.timerIsFinished||(o.addTime(t),o.isFinished()?g(o,c):v(o,c)))},subtractTime:function(t){o.creationType!==r.UnixTimestamp&&(m.timerIsFinished||(o.subtractTime(t),o.isFinished()?g(o,c):v(o,c)))}}}}); | ||
!function(t,i){"object"==typeof exports&&"undefined"!=typeof module?i(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],i):i((t||self).reactTimerAndStopwatch={},t.react)}(this,function(t,i){function e(t,i){for(var e=0;e<i.length;e++){var s=i[e];s.enumerable=s.enumerable||!1,s.configurable=!0,"value"in s&&(s.writable=!0),Object.defineProperty(t,s.key,s)}}function s(t,i,s){return i&&e(t.prototype,i),s&&e(t,s),t}function n(){return n=Object.assign||function(t){for(var i=1;i<arguments.length;i++){var e=arguments[i];for(var s in e)Object.prototype.hasOwnProperty.call(e,s)&&(t[s]=e[s])}return t},n.apply(this,arguments)}var r,a,o;function u(t,i){switch(i.type){case"pause":return n({},t,{paused:!0});case"resume":return n({},t,{paused:!1});case"togglePause":return n({},t,{paused:!t.paused});case"updateTimer":var e=i.payload;return n({},t,{timerText:e.getTimerText(),timerDisplayStrings:e.getTimerDisplayStrings(),timeElapsed:e.getTimeElapsed()});case"timerIsFinished":var s=i.payload,r=s.timerBase.directionOfTime===a.Backward?0:s.timerBase.timestamps.end;return n({},t,{timerIsFinished:!0,pastFinish:s.continueAfterFinish,timerText:s.getTimerText(r),timerDisplayStrings:s.getTimerDisplayStrings(r),timeElapsed:s.timerBase.timestamps.end});case"resetTimer":return n({},i.payload);case"timerIsPastFinish":return n({},t,{pastFinish:!0});default:return t}}!function(t){t[t.Duration=0]="Duration",t[t.UnixTimestamp=1]="UnixTimestamp",t[t.Stopwatch=2]="Stopwatch"}(r||(r={})),function(t){t[t.Forward=0]="Forward",t[t.Backward=1]="Backward"}(a||(a={})),function(t){t[t.OnProgress=0]="OnProgress",t[t.OnTimeLeft=1]="OnTimeLeft"}(o||(o={}));var l=/*#__PURE__*/function(){function t(t){this.milliseconds="object"==typeof t?t.milliseconds:t,this.seconds="object"==typeof t?t.seconds:0,this.minutes="object"==typeof t?t.minutes:0,this.hours="object"==typeof t?t.hours:0,this.days="object"==typeof t?t.days:0}return t.prototype.toMilliseconds=function(){var t=0;return this.days&&(t=24*this.days),this.hours&&(t+=this.hours),t&&(t*=60),this.minutes&&(t+=this.minutes),t&&(t*=60),this.seconds&&(t+=this.seconds),t&&(t*=1e3),this.milliseconds&&(t+=this.milliseconds),t},t}(),c=/*#__PURE__*/function(){function t(t,i,e,s,n){var r;if(!t.create.stopwatch&&!t.create.timerWithDuration&&!t.create.timerWithUnixTimestamp)throw new Error("useTimer requires one of options.create.stopwatch, options.create.timerWithDuration, or options.create.timerWithUnixTimestamp. None provided.");this.creationType=e,this.display=s,this.timerBase=i,this.callbacks=n,this.continueAfterFinish=i.continueAfterFinish,this.intervalRate=null!=(r=t.intervalRate)?r:1e3,this.timerIsFinished=this.timerBase.isFinished(),this.pastFinish=this.timerBase.IsPastFinish()}var i=t.prototype;return i.isFinished=function(){return this.timerBase.isFinished()},i.isPastFinish=function(){var t=this.timerBase.IsPastFinish();return this.pastFinish=t,t},i.addTime=function(t){var i=new l(t).toMilliseconds();this.timerBase.addTime(i)},i.subtractTime=function(t){var i=new l(t).toMilliseconds();this.timerBase.subtractTime(i)},i.advanceTimestamps=function(){this.timerBase.advanceTimestamps()},i.getTimerText=function(t){return this.display.createTimerText(void 0===t?this.timerBase.millisecondsToDisplay():t,this.pastFinish)},i.getTimeElapsed=function(){return this.timerBase.timeElapsed},i.getTimerDisplayNumbers=function(t){return this.display.createDisplayNumbers(void 0===t?this.timerBase.millisecondsToDisplay():t)},i.getTimerDisplayStrings=function(t){return this.display.createDisplayStrings(void 0===t?this.timerBase.millisecondsToDisplay():t)},i.fireCallbacks=function(){var t,i;null==(t=(i=this.callbacks).onTick)||t.call(i),this.callbacks.onProgress(this.timerBase.timeElapsed),this.creationType!==r.Stopwatch&&this.callbacks.onTimeLeft(this.timerBase.timeLeft)},t}(),m=/*#__PURE__*/function(){function t(t){var i,e,s,n,r;this.intervalRate=null!=(i=t.intervalRate)?i:1e3,this.continueAfterFinish=null!=(e=null==(s=t.create.timerWithUnixTimestamp)?void 0:s.continueAfterFinish)&&e,this.directionOfTime=a.Backward,this.unixTimeMilliseconds=null!=(n=null==(r=t.create.timerWithUnixTimestamp)?void 0:r.unixTimestampMilliseconds)?n:Date.now(),this.millisecondsLeft=this.unixTimeMilliseconds-Date.now(),this.timestamps={start:0,end:this.millisecondsLeft}}var i=t.prototype;return i.millisecondsToDisplay=function(){return this.timestamps.end-this.timestamps.start},i.advanceTimestamps=function(){this.timestamps.start+=this.intervalRate},i.isFinished=function(){return this.timestamps.start>=this.timestamps.end},i.IsPastFinish=function(){return this.timestamps.start>=this.timestamps.end&&this.continueAfterFinish},i.addTime=function(t){},i.subtractTime=function(t){},s(t,[{key:"timeElapsed",get:function(){return this.timestamps.start}},{key:"timeLeft",get:function(){return this.timestamps.end-this.timestamps.start}}]),t}(),h=/*#__PURE__*/function(){function t(t){var i,e,s,n;this.IsPastFinish=function(){return!1},this.intervalRate=null!=(i=t.intervalRate)?i:1e3,this.continueAfterFinish=!1;var r=new l(null!=(e=null==(s=t.create.timerWithDuration)?void 0:s.time)?e:0).toMilliseconds();this.directionOfTime=null!=(n=t.create.timerWithDuration)&&n.directionOfTimeForward?a.Forward:a.Backward,this.timestamps={start:0,end:0+r}}var i=t.prototype;return i.millisecondsToDisplay=function(){return this.directionOfTime===a.Forward?this.timestamps.start:this.timestamps.end-this.timestamps.start},i.advanceTimestamps=function(){this.timestamps.start+=this.intervalRate},i.isFinished=function(){return this.timestamps.start>=this.timestamps.end},i.addTime=function(t){var i=new l(t).toMilliseconds();this.timestamps.end+=i},i.subtractTime=function(t){var i=new l(t).toMilliseconds(),e=this.timestamps.start+i;this.timestamps.start=e>this.timestamps.end?this.timestamps.end:e},s(t,[{key:"timeElapsed",get:function(){return this.timestamps.start}},{key:"timeLeft",get:function(){return this.timestamps.end-this.timestamps.start}}]),t}(),d=/*#__PURE__*/function(){function t(t){var i,e,s;this.timeLeft=0,this.isFinished=function(){return!1},this.IsPastFinish=function(){return!1},this.intervalRate=null!=(i=t.intervalRate)?i:1e3,this.continueAfterFinish=!1,this.directionOfTime=a.Forward,this.timestamps={start:null!=(e=null==(s=t.create.stopwatch)?void 0:s.startAtMilliseconds)?e:0,end:0}}var i=t.prototype;return i.millisecondsToDisplay=function(){return this.timestamps.start},i.advanceTimestamps=function(){this.timestamps.start+=this.intervalRate},i.addTime=function(t){var i=new l(t).toMilliseconds();this.timestamps.start+=i},i.subtractTime=function(t){var i=new l(t).toMilliseconds();this.timestamps.start-=i,this.timestamps.start<0&&(this.timestamps.start=0)},s(t,[{key:"timeElapsed",get:function(){return this.timestamps.start}}]),t}(),p=/*#__PURE__*/function(){function t(t,i){var e,s,n,r,a,u;this.onTick=null!=(e=null==(s=t.callbacks)?void 0:s.onTick)?e:void 0,this.onFinish=null!=(n=null==(r=t.callbacks)?void 0:r.onFinish)?n:void 0,this.onProgressControllers=null!=(a=t.callbacks)&&a.onProgress?this.CallbackOptionsToControllers(t.callbacks.onProgress,o.OnProgress):[],this.onTimeLeftControllers=null!=(u=t.callbacks)&&u.onTimeLeft?this.CallbackOptionsToControllers(t.callbacks.onTimeLeft,o.OnTimeLeft,i):[]}var i=t.prototype;return i.CallbackOptionToController=function(t){return{milliseconds:new l(t.time).toMilliseconds(),callback:t.callback,fired:!1}},i.CallbackOptionsToControllers=function(t,i,e){var s=this;return t.filter(function(t){if("function"==typeof t.callback){var s=new l(t.time).toMilliseconds();if(i===o.OnProgress&&s>0)return!0;if(i===o.OnTimeLeft&&void 0!==e)return e>s}return!1}).map(function(t){return s.CallbackOptionToController(t)})},i.onProgress=function(t){if(this.onProgressControllers.length>0){var i=!1;this.onProgressControllers.forEach(function(e){if(t>=e.milliseconds)return e.callback(),e.fired=!0,void(i=!0)}),i&&(this.onProgressControllers=this.onProgressControllers.filter(function(t){return!t.fired}))}},i.onTimeLeft=function(t){if(this.onTimeLeftControllers.length>0){var i=!1;this.onTimeLeftControllers.forEach(function(e){if(t<=e.milliseconds)return e.callback(),e.fired=!0,void(i=!0)}),i&&(this.onTimeLeftControllers=this.onTimeLeftControllers.filter(function(t){return!t.fired}))}},t}(),f=/*#__PURE__*/function(){function t(t){var i;this.textOutputWithWords=!!t.textOutputWithWords,this.includeMilliseconds=null!=(i=t.includeMilliseconds)&&i}var i=t.prototype;return i.getRelevantDisplayUnits=function(t){return{days:t>=864e5,hours:t>=36e5,minutes:t>=6e4,seconds:t>=1e3,milliseconds:!0}},i.createDisplayNumbers=function(t){var i=Math.abs(t),e=i%1e3,s=i/1e3,n=Math.floor(s),r=s/60,a=Math.floor(r),o=r/60,u=Math.floor(o),l=Math.floor(o/24);return{milliseconds:e,seconds:this.totalOrRemainderTime(n,60,"seconds"),minutes:this.totalOrRemainderTime(a,60,"minutes"),hours:this.totalOrRemainderTime(u,24,"hours"),days:l}},i.createDisplayStrings=function(t){var i=this.createDisplayNumbers(t);return this.displayNumbersToDisplayStrings(i)},i.displayNumberToString=function(t,i){return t=Math.abs(t),i?t<100?t<10?"00"+t:"0"+t:String(t):this.textOutputWithWords?String(t):t<10?"0"+t:String(t)},i.displayNumbersToDisplayStrings=function(t){return{seconds:this.displayNumberToString(t.seconds),minutes:this.displayNumberToString(t.minutes),hours:this.displayNumberToString(t.hours),days:this.displayNumberToString(t.days),milliseconds:this.displayNumberToString(t.milliseconds,!0)}},i.createTimerText=function(t,i){t=Math.abs(t);var e=this.createDisplayNumbers(t),s=this.displayNumbersToDisplayStrings(e),n=this.getRelevantDisplayUnits(t);return this.textOutputWithWords?this.createTextOutputWithWords(s,n,i):this.createTextOutputWithoutWords(s,n,i)},i.createTextOutputWithoutWords=function(t,i,e){return(e?"-":"")+(i.days?t.days+":":"")+t.hours+":"+t.minutes+":"+t.seconds+(this.includeMilliseconds?":"+t.milliseconds:"")},i.createTextOutputWithWords=function(t,i,e){var s=e?" ago":"",n="",r=1===Number(t.days)?"":"s";i.days&&(n=t.days+" day"+r+", ");var a="",o=1===Number(t.hours)?"":"s";i.hours&&(a=t.hours+" hour"+o+", ");var u="",l=1===Number(t.minutes)?"":"s";i.minutes&&(u=t.minutes+" minute"+l+", ");var c="",m=1===Number(t.seconds)?"":"s";i.seconds&&(c=t.seconds+" second"+m),i.seconds||this.includeMilliseconds||(c="0 seconds");var h="";return this.includeMilliseconds&&(h=i.seconds?", "+t.milliseconds+" milliseconds":t.milliseconds+" milliseconds"),""+n+a+u+c+h+s},i.totalOrRemainderTime=function(t,i,e){if("days"!==e){if("seconds"===e&&t>60)return t%60?t%60:0;if("minutes"===e&&t>60)return t%60?t%60:0;if("hours"===e&&t>24)return t%24?t%24:0}return t===i?0:0===t?t:t%i?t%i:i},t}();function T(t){var i=function(t){var i;return t.create.timerWithDuration?r.Duration:null!=(i=t.create.timerWithUnixTimestamp)&&i.unixTimestampMilliseconds?r.UnixTimestamp:r.Stopwatch}(t),e=function(t,i){return t===r.Duration?new h(i):t===r.UnixTimestamp?new m(i):new d(i)}(i,t),s=new f(t),n=new p(t,e.timeLeft);return new c(t,e,i,s,n)}function y(t,i){return{paused:!1===t.autoplay&&i.creationType!==r.UnixTimestamp,timerIsFinished:i.timerIsFinished,timerText:i.getTimerText(),timerDisplayStrings:i.getTimerDisplayStrings(),timeElapsed:i.getTimeElapsed(),pastFinish:!1}}function v(t,i){i({type:"updateTimer",payload:t})}function g(t,i){null==t.callbacks.onFinish||t.callbacks.onFinish(),t.timerIsFinished=!0,t.pastFinish=t.continueAfterFinish,i({type:"timerIsFinished",payload:t})}t.useTimer=function(t){var e,s,a,o=i.useRef(null),l=function(t,i){return null===t.current&&(t.current=T(i)),t.current}(o,t),c=i.useReducer(u,null,function(){return y(t,l)}),m=c[0],h=c[1];return e=function(){l.isFinished()&&!l.isPastFinish()||(l.advanceTimestamps(),l.fireCallbacks()),m.timerIsFinished||!l.isFinished()?v(l,h):g(l,h)},s=function(t,i){return t.timerBase.continueAfterFinish&&t.creationType===r.UnixTimestamp?!i.paused:!i.paused&&!i.timerIsFinished}(l,m)?l.intervalRate:null,a=i.useRef(function(){}),i.useEffect(function(){a.current=e},[e]),i.useEffect(function(){if(null!==s){var t=setInterval(function(){a.current()},s);return function(){return clearInterval(t)}}},[s]),{timerText:m.timerText,timerDisplayStrings:m.timerDisplayStrings,timeElapsed:m.timeElapsed,timerIsPaused:m.paused,timerIsFinished:m.timerIsFinished,pastFinish:m.pastFinish,pauseTimer:function(){l.creationType!==r.UnixTimestamp&&(!m.paused&&!m.timerIsFinished||l.pastFinish)&&h({type:"pause"})},resumeTimer:function(){l.creationType!==r.UnixTimestamp&&(m.paused&&!m.timerIsFinished||l.pastFinish)&&h({type:"resume"})},togglePause:function(){l.creationType!==r.UnixTimestamp&&(m.timerIsFinished&&!l.pastFinish||h({type:"togglePause"}))},resetTimer:function(i,e){if(l.creationType!==r.UnixTimestamp){var s=function(t,i,e){return i?e&&i.create?n({create:{}},i):n({},t,i):t}(t,i,e);o.current=T(s),h({type:"resetTimer",payload:y(s,o.current)})}},addTime:function(t){l.creationType!==r.UnixTimestamp&&(m.timerIsFinished||(l.addTime(t),l.isFinished()?g(l,h):v(l,h)))},subtractTime:function(t){l.creationType!==r.UnixTimestamp&&(m.timerIsFinished||(l.subtractTime(t),l.isFinished()?g(l,h):v(l,h)))}}}}); | ||
//# sourceMappingURL=index.umd.js.map |
@@ -157,4 +157,8 @@ export declare type UnitOfTime = 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days'; | ||
togglePause: () => void; | ||
/** Resets timer to beginning and to original options supplied to useTimer */ | ||
resetTimer: () => void; | ||
/** Resets timer to beginning and to original options supplied to useTimer if no parameters included. | ||
* If you'd like to add or change some options to the timer/stopwatch, include an options object in the first parameter. | ||
* If you'd like that object to replace the original options entirely (rather than adjust it), set the second parameter to true. | ||
* @param {Partial<UseTimerOptions>} [adjustedOptions] - a partial useTimer options object to adjust your options, or a full useTimer options object if you'd like to replace it | ||
* @param {boolean} [replaceOptions] - Indicating whether you want to replace the old original options entirely with your new one, or just adjust a few included settings on it and keep the rest of the old options.*/ | ||
resetTimer: (adjustedOptions?: Partial<UseTimerOptions>, replaceOptions?: boolean) => void; | ||
/** - Stopwatch - Adds time to elapsed Stopwatch time. Example: addTime(5000) on a Stopwatch with 10 seconds increases it to 15 seconds | ||
@@ -161,0 +165,0 @@ - Duration timer - Expands timer time, pushing back its end |
@@ -15,3 +15,4 @@ import React, { MutableRefObject } from "react"; | ||
export declare function getTimerController(timerControllerRef: MutableRefObject<TimerController | null>, options: UseTimerOptions): TimerController; | ||
export declare function handleOptionsReset(oldOptions: UseTimerOptions, adjustedOptions?: Partial<UseTimerOptions>, replaceOptions?: boolean): UseTimerOptions; | ||
export declare function useInterval(cb: () => void, delay: number | null): void; | ||
//# sourceMappingURL=util.d.ts.map |
{ | ||
"name": "react-timer-and-stopwatch", | ||
"version": "0.5.2", | ||
"version": "0.6.0", | ||
"description": "A countdown timer and stopwatch hook for React", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
333342
615