timermodule
Advanced tools
Comparing version 0.0.0 to 0.0.1
@@ -1,1 +0,1 @@ | ||
let defaultValue={hh:"00",mm:"00",ss:"00"};const hhInMilli=36e5,mmInMilli=6e4,ssInMilli=1e3;function Timer(){this.timerDetails={},this.notifier=(()=>{}),this.clientData={}}function timeObjToMillis(i){let{hh:t,mm:e,ss:s}=i;return Number(t)*hhInMilli+Number(e)*mmInMilli+Number(s)*ssInMilli}function millisToTimeObj(i){return{hh:i/hhInMilli,mm:i%hhInMilli/mmInMilli,ss:i%hhInMilli%mmInMilli/ssInMilli}}Timer.prototype={init:i=>(this.initTimerDetails(i),Object.keys(i).map(t=>{let{time:e,status:s}=i[t];this.initTimerMechanism(t,s,e)}),this.initClientData(),this.clientData),initClientData:()=>{Object.keys(this.timerDetails).map(i=>{let{currentTime:t,startTime:e}=this.timerDetails[i]||{};this.clientData[i]={},this.clientData[i].time=millisToTimeObj(t-e),this.clientData[i].status=this.getTimerDetail(i).status})},setNotifier:setNotifier=(i=>{this.notifier=i}),clearInterval:i=>{let t=this.getTimerDetail(i);clearInterval(t.intervalId),delete t.intervalId},initTimerDetails:i=>{Object.keys(i).map(i=>{this.timerDetails[i]={currentTime:"",startTime:""}})},getTimerDetail:i=>this.timerDetails[i],notify:()=>{this.notifier(this.clientDeta)},setTimerDetail:setTimerDetail=((i,t)=>{this.timerDetails[i]=Object.assign({},this.timerDetails[i],t)}),setClientData:(i,t)=>{this.clientData[i]=Object.assign({},this.clientData[i],t)},initTimerMechanism:(i,t,e=defaultValue)=>{let{currentTime:s,previousTime:l,startTime:a}=this.getTimerDetail(i),m=timeObjToMillis(e),r=(new Date).getTime();"START"===t?(a=r-m,this.setTimerDetail(i,{startTime:a,previousTime:l}),this.start(i)):"PAUSED"===t?(a=r-timeObjToMillis(e),this.setTimerDetail(i,{startTime:a,previousTime:l}),this.pause(i)):"RESUME"===t&&this.resume(i)},triggerTimerMechanism:i=>{let t=setInterval(()=>{let{startTime:t}=this.getTimerDetail(i),e=millisToTimeObj((new Date).getTime()-t);this.setClientData(i,{time:e}),this.notify()},1e3);this.setTimerDetail(i,{intervalId:t})},start:i=>{this.clearInterval(i),this.triggerTimerMechanism(i)},pause:i=>{this.clearInterval(i);let{startTime:t}=this.getTimerDetail(i),e=millisToTimeObj((new Date).getTime()-t);this.setTimerDetail(i,{previousTime:e})},resume:i=>{this.clearInterval(i);let{previousTime:t}=this.getTimerDetail(i),e=(new Date).getTime()-timeObjToMillis(t);this.setTimerDetail(i,{startTime:e}),this.triggerTimerMechanism(i)}};export default Timer; | ||
let defaultValue={hh:"00",mm:"00",ss:"00"};const hhInMilli=36e5,mmInMilli=6e4,ssInMilli=1e3;function Timer(){this.timerDetails={},this.notifier=function(){},this.clientData={}}Timer.timeObjToMillis=function(t={}){let{hh:e="00",mm:i="00",ss:s="00"}=t;return 36e5*Number(e)+6e4*Number(i)+1e3*Number(s)},Timer.millisToTimeObj=function(t){let e=`${Math.floor(t/36e5)}`,i=`${Math.floor(t%36e5/6e4)}`,s=`${Math.floor(t%36e5%6e4/1e3)}`;return{hh:e=1===e.length?`0${e}`:e,mm:i=1===i.length?`0${i}`:i,ss:s=1===s.length?`0${s}`:s}},Timer.prototype={init:function(t){let{data:e,notifier:i}=t;this.initTimerDetails(e),this.initClientData(e),this.setNotifier(i),Object.keys(e).map(t=>{let{time:i,status:s}=e[t];this.updateTimerState(t,s,i),this.deleteTimerDetailKey(t,"isInitCall")})},initClientData:function(t){Object.keys(t).map(t=>{this.clientData[t]={}})},setNotifier:function(t){this.notifier=t},clearInterval:function(t){let e=this.getTimerDetail(t);clearInterval(e.intervalId),delete e.intervalId},initTimerDetails:function(t){Object.keys(t).map(t=>{this.timerDetails[t]={isInitCall:!0}})},getTimerDetail:function(t){return this.timerDetails[t]},notify:function(){this.notifier(this.clientData)},setTimerDetail:function(t,e){this.timerDetails[t]=Object.assign({},this.timerDetails[t],e)},deleteTimerDetailKey:function(t,e){let i=Object.assign({},this.timerDetails[t]);delete i[e],this.timerDetails[t]=i},setClientData:function(t,e){let i={};Object.keys(this.clientData).map(s=>{i[s]=s!==t?this.clientData[s]:Object.assign({},this.clientData[t],e)}),this.clientData=i},updateTimerState:function(t,e,i=defaultValue){"START"===e?(this.setTimerDetail(t,{previousTimeObj:i}),this.start(t)):"PAUSE"===e?(this.setTimerDetail(t,{previousTimeObj:i}),this.pause(t)):"RESUME"===e?(this.setTimerDetail(t,{previousTimeObj:i}),this.resume(t)):"INIT"===e&&this.reset(t)},triggerTimerMechanism:function(t){let e=setInterval(()=>{let{startTime:e}=this.getTimerDetail(t),i=Timer.millisToTimeObj((new Date).getTime()-e);this.setClientData(t,{time:i}),this.notify()},1e3);this.setTimerDetail(t,{intervalId:e})},start:function(t){this.clearInterval(t);let{previousTimeObj:e,isInitCall:i}=this.getTimerDetail(t),s=(new Date).getTime()-Timer.timeObjToMillis(e);this.deleteTimerDetailKey(t,"previousTimeObj"),this.setTimerDetail(t,{startTime:s}),this.setClientData(t,{status:"RUNNING",time:Timer.millisToTimeObj((new Date).getTime()-s)}),this.triggerTimerMechanism(t),1!=i&&this.notify()},pause:function(t){this.clearInterval(t);let e=(new Date).getTime(),{startTime:i=e,isInitCall:s,previousTimeObj:l}=this.getTimerDetail(t);i-=Timer.timeObjToMillis(l);let a=Timer.millisToTimeObj(e-i);this.setTimerDetail(t,{previousTimeObj:a}),this.setClientData(t,{status:"PAUSED",time:a}),1!=s&&this.notify()},resume:function(t){this.clearInterval(t);let{previousTimeObj:e,isInitCall:i}=this.getTimerDetail(t),s=(new Date).getTime()-Timer.timeObjToMillis(e);this.setTimerDetail(t,{startTime:s}),this.setClientData(t,{status:"RUNNING",time:e}),this.deleteTimerDetailKey(t,"previousTimeObj"),this.triggerTimerMechanism(t),1!=i&&this.notify()},reset:function(t){this.clearInterval(t),this.setClientData(t,{status:"INIT",time:{hh:"00",mm:"00",ss:"00"}}),this.deleteTimerDetailKey(t,"previousTimeObj"),this.deleteTimerDetailKey(t,"startTime"),1!=this.getTimerDetail(t).isInitCall&&this.notify()}};export default Timer; |
{ | ||
"name" : "timermodule", | ||
"version" : "0.0.0", | ||
"version" : "0.0.1", | ||
"main" : "./dist/index.js", | ||
"author" : "Vallinayagam Thirumalaimurugan" | ||
} | ||
} |
130
src/index.js
@@ -5,16 +5,22 @@ let defaultValue = {hh : "00", mm : "00", ss : "00"}; | ||
this.timerDetails = {}; | ||
this.notifier = () => {}; | ||
this.notifier = function() {}; | ||
this.clientData = {}; | ||
} | ||
function timeObjToMillis(duration) { | ||
let {hh, mm, ss} = duration; | ||
Timer.timeObjToMillis = function(duration = {}) { | ||
let {hh = "00", mm = "00", ss = "00"} = duration; | ||
return Number(hh) * hhInMilli + Number(mm) * mmInMilli + Number(ss) * ssInMilli | ||
} | ||
function millisToTimeObj(millis) { | ||
Timer.millisToTimeObj = function(millis) { | ||
let hh = `${Math.floor(millis / hhInMilli)}`, | ||
mm = `${Math.floor((millis % hhInMilli) / mmInMilli)}`, | ||
ss = `${Math.floor(((millis % hhInMilli) % mmInMilli) / ssInMilli)}`; | ||
hh = hh.length === 1 ? `0${hh}` : hh; | ||
mm = mm.length === 1 ? `0${mm}` : mm; | ||
ss = ss.length === 1 ? `0${ss}` : ss; | ||
return { | ||
hh : millis / hhInMilli, | ||
mm : (millis % hhInMilli) / mmInMilli, | ||
ss : ((millis % hhInMilli) % mmInMilli) / ssInMilli | ||
hh, | ||
mm, | ||
ss | ||
} | ||
@@ -24,23 +30,22 @@ } | ||
Timer.prototype = { | ||
init : (details) => { | ||
this.initTimerDetails(details); | ||
Object.keys(details).map((detailKey) => { | ||
let {time, status} = details[detailKey]; | ||
this.initTimerMechanism(detailKey, status, time); | ||
init : function(details) { | ||
let {data, notifier} = details; | ||
this.initTimerDetails(data); | ||
this.initClientData(data); | ||
this.setNotifier(notifier); | ||
Object.keys(data).map((detailKey) => { | ||
let {time, status} = data[detailKey]; | ||
this.updateTimerState(detailKey, status, time); | ||
this.deleteTimerDetailKey(detailKey, "isInitCall"); | ||
}); | ||
this.initClientData(); | ||
return this.clientData | ||
}, | ||
initClientData : () => { | ||
Object.keys(this.timerDetails).map((detailKey) => { | ||
let {currentTime, startTime} = this.timerDetails[detailKey] || {}; | ||
initClientData : function(data) { | ||
Object.keys(data).map((detailKey) => { | ||
this.clientData[detailKey] = {}; | ||
this.clientData[detailKey].time = millisToTimeObj(currentTime - startTime); | ||
this.clientData[detailKey].status = this.getTimerDetail(detailKey).status; | ||
}); | ||
}, | ||
setNotifier = (func) => { | ||
setNotifier : function(func) { | ||
this.notifier = func; | ||
}, | ||
clearInterval : (key) => { | ||
clearInterval : function(key) { | ||
let timerDetail = this.getTimerDetail(key); | ||
@@ -50,39 +55,46 @@ clearInterval(timerDetail.intervalId); | ||
}, | ||
initTimerDetails : (details) => { | ||
initTimerDetails : function(details) { | ||
Object.keys(details).map((detailKey) => { | ||
this.timerDetails[detailKey] = { | ||
currentTime : "", | ||
startTime : "" | ||
isInitCall : true | ||
} | ||
}); | ||
}, | ||
getTimerDetail : (key) => this.timerDetails[key], | ||
notify : () => { | ||
this.notifier(this.clientDeta); | ||
getTimerDetail : function(key) {return this.timerDetails[key]} , | ||
notify : function() { | ||
this.notifier(this.clientData); | ||
}, | ||
setTimerDetail = (key, obj) => { | ||
setTimerDetail : function(key, obj) { | ||
this.timerDetails[key] = Object.assign({}, this.timerDetails[key], obj); | ||
}, | ||
setClientData : (key, obj) => { | ||
this.clientData[key] = Object.assign({}, this.clientData[key], obj); | ||
deleteTimerDetailKey : function(key, detailKey) { | ||
let newObj = Object.assign({}, this.timerDetails[key]); | ||
delete newObj[detailKey]; | ||
this.timerDetails[key] = newObj; | ||
}, | ||
initTimerMechanism : (key, action, duration=defaultValue) => { | ||
let {currentTime, previousTime, startTime} = this.getTimerDetail(key); | ||
let prevRunTime = timeObjToMillis(duration), time = new Date().getTime(); | ||
setClientData : function(key, obj) { | ||
let newData = {} | ||
Object.keys(this.clientData).map((clientKey) => { | ||
newData[clientKey] = clientKey !== key ? this.clientData[clientKey] : Object.assign({}, this.clientData[key], obj) | ||
}) | ||
this.clientData = newData | ||
}, | ||
updateTimerState : function(key, action, duration=defaultValue) { | ||
if(action === "START") { | ||
startTime = time - prevRunTime; | ||
this.setTimerDetail(key, {startTime, previousTime}); | ||
this.setTimerDetail(key, {previousTimeObj : duration}); | ||
this.start(key); | ||
} else if(action === "PAUSED") { | ||
startTime = time - timeObjToMillis(duration); | ||
this.setTimerDetail(key, {startTime, previousTime}); | ||
} else if(action === "PAUSE") { | ||
this.setTimerDetail(key, {previousTimeObj : duration}); | ||
this.pause(key); | ||
} else if(action === "RESUME") { | ||
} else if(action === "RESUME") { | ||
this.setTimerDetail(key, {previousTimeObj : duration}); | ||
this.resume(key); | ||
} else if(action === "INIT") { | ||
this.reset(key); | ||
} | ||
}, | ||
triggerTimerMechanism : (key) => { | ||
triggerTimerMechanism : function(key) { | ||
let intervalId = setInterval(() => { | ||
let {startTime} = this.getTimerDetail(key); | ||
let time = millisToTimeObj(new Date().getTime() - startTime); | ||
let time = Timer.millisToTimeObj(new Date().getTime() - startTime); | ||
this.setClientData(key, {time}); | ||
@@ -93,20 +105,40 @@ this.notify(); | ||
}, | ||
start : (key) => { | ||
start : function(key) { | ||
this.clearInterval(key); | ||
let {previousTimeObj, isInitCall} = this.getTimerDetail(key); | ||
let startTime = new Date().getTime() - Timer.timeObjToMillis(previousTimeObj); | ||
this.deleteTimerDetailKey(key, "previousTimeObj"); | ||
this.setTimerDetail(key, {startTime}); | ||
this.setClientData(key, {status : "RUNNING", time : Timer.millisToTimeObj(new Date().getTime() - startTime) }); | ||
this.triggerTimerMechanism(key); | ||
isInitCall != true && this.notify(); | ||
}, | ||
pause : (key) => { | ||
pause : function(key) { | ||
this.clearInterval(key); | ||
let {startTime} = this.getTimerDetail(key); | ||
let previousTime = millisToTimeObj(new Date().getTime() - startTime); | ||
this.setTimerDetail(key, {previousTime}); | ||
let currentTime = new Date().getTime(); | ||
let {startTime = currentTime, isInitCall, previousTimeObj} = this.getTimerDetail(key); | ||
startTime = startTime - Timer.timeObjToMillis(previousTimeObj); | ||
let previousTime = Timer.millisToTimeObj(currentTime - startTime); | ||
this.setTimerDetail(key, {previousTimeObj : previousTime}); | ||
this.setClientData(key, {status : "PAUSED", time : previousTime}); | ||
isInitCall != true && this.notify(); | ||
}, | ||
resume : (key) => { | ||
resume : function(key) { | ||
this.clearInterval(key); | ||
let {previousTime} = this.getTimerDetail(key); | ||
let startTime = new Date().getTime() - timeObjToMillis(previousTime); | ||
let {previousTimeObj, isInitCall} = this.getTimerDetail(key), currentTime = new Date().getTime(); | ||
let startTime = currentTime - Timer.timeObjToMillis(previousTimeObj); | ||
this.setTimerDetail(key, {startTime}); | ||
this.setClientData(key, {status : "RUNNING", time : previousTimeObj}); | ||
this.deleteTimerDetailKey(key, "previousTimeObj"); | ||
this.triggerTimerMechanism(key); | ||
isInitCall != true && this.notify(); | ||
}, | ||
reset : function(key) { | ||
this.clearInterval(key); | ||
this.setClientData(key, {status : "INIT", time : {hh: "00" , mm : "00", ss : "00"}}); | ||
this.deleteTimerDetailKey(key, "previousTimeObj"); | ||
this.deleteTimerDetailKey(key, "startTime"); | ||
this.getTimerDetail(key).isInitCall != true && this.notify(); | ||
} | ||
} | ||
export default Timer |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
8609
136
1