| export default (seed, modulus, multiplier, increment) => { | ||
| const m = modulus | ||
| const a = multiplier | ||
| const c = increment | ||
| let z = seed | ||
| return { | ||
| rand: () => { | ||
| z = (a * z + c) % m | ||
| return z / m | ||
| } | ||
| } | ||
| } |
| import createLcg from './createLcg' | ||
| const getSeedsFromDate = (date) => { | ||
| const year = date.getFullYear() | ||
| const month = (date.getMonth() + 1).toString().padStart(2, '0') | ||
| const day = date | ||
| .getDate() // day of the month | ||
| .toString() | ||
| .padStart(2, '0') | ||
| const hour = date | ||
| .getHours() | ||
| .toString() | ||
| .padStart(2, '0') | ||
| return { | ||
| minute: parseInt(`${year}${month}${day}${hour}`), | ||
| hour: parseInt(`${year}${month}${day}`) | ||
| } | ||
| } | ||
| const getSequenceFromLcg = (lcg, length) => { | ||
| const values = [] | ||
| // generate sequence | ||
| for (let i = 0; i < length; i++) { | ||
| values.push(lcg.rand()) | ||
| } | ||
| // copy and sort sequence | ||
| const sorted = values.slice().sort() | ||
| // normalize values based on their order | ||
| for (let i = 0; i < sorted.length; i++) { | ||
| values[values.indexOf(sorted[i])] = i | ||
| } | ||
| return values | ||
| } | ||
| const getHourLcg = (seed) => | ||
| // values from the Numerical Recipes book | ||
| createLcg(seed, 4294967296, 1664525, 1013904223) | ||
| const getMinuteLcg = (seed) => | ||
| // values from MINSTD | ||
| createLcg(seed, 2147483647, 48271, 0) | ||
| const mergeTimeWithDate = (time, date) => { | ||
| const flowdate = new Date(date.getTime()) | ||
| flowdate.setHours(time.hour) | ||
| flowdate.setMinutes(time.minute) | ||
| flowdate.setSeconds(time.second) | ||
| return flowdate | ||
| } | ||
| export const fromDate = (date) => { | ||
| // extract seeds | ||
| const seed = getSeedsFromDate(date) | ||
| // generate sequences | ||
| const mLcg = getMinuteLcg(seed.minute) | ||
| const mSequence = getSequenceFromLcg(mLcg, 60) | ||
| const hLcg = getHourLcg(seed.hour) | ||
| const hSequence = getSequenceFromLcg(hLcg, 24) | ||
| // build flowtime | ||
| const time = { | ||
| hour: hSequence[date.getHours()], | ||
| minute: mSequence[date.getMinutes()], | ||
| second: date.getSeconds() | ||
| } | ||
| return { | ||
| ...time, | ||
| toDate: () => mergeTimeWithDate(time, date) | ||
| } | ||
| } | ||
| export const fromNow = () => fromDate(new Date()) |
| import { fromDate, fromNow } from './flowtime' | ||
| export { fromDate, fromNow } |
+3
-4
| { | ||
| "name": "flowtime", | ||
| "version": "1.2.1", | ||
| "version": "1.2.2", | ||
| "description": "An unpredictable time library for JavaScript", | ||
@@ -20,5 +20,4 @@ "license": "MIT", | ||
| "files": [ | ||
| "dist/flowtime.js", | ||
| "dist/flowtime.mjs", | ||
| "dist/flowtime.umd.js" | ||
| "src/*.js", | ||
| "dist/*.js" | ||
| ], | ||
@@ -25,0 +24,0 @@ "scripts": { |
| function t(t,n,r,e){var u=n,o=r,a=e,i=t;return{rand:function(){return(i=(o*i+a)%u)/u}}}var n=function(t,n){for(var r=[],e=0;e<n;e++)r.push(t.rand());for(var u=r.slice().sort(),o=0;o<u.length;o++)r[r.indexOf(u[o])]=o;return r},r=function(r){var e=function(t){var n=t.getFullYear(),r=(t.getMonth()+1).toString().padStart(2,"0"),e=t.getDate().toString().padStart(2,"0"),u=t.getHours().toString().padStart(2,"0");return{minute:parseInt(""+n+r+e+u),hour:parseInt(""+n+r+e)}}(r),u=function(n){return t(n,2147483647,48271,0)}(e.minute),o=n(u,60),a=function(n){return t(n,4294967296,1664525,1013904223)}(e.hour),i={hour:n(a,24)[r.getHours()],minute:o[r.getMinutes()],second:r.getSeconds()};return Object.assign({},i,{toDate:function(){return function(t,n){var r=new Date(n.getTime());return r.setHours(t.hour),r.setMinutes(t.minute),r.setSeconds(t.second),r}(i,r)}})},e=function(){return r(new Date)};export{r as fromDate,e as fromNow}; | ||
| //# sourceMappingURL=flowtime.mjs.map |
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
11805
10.66%8
33.33%89
456.25%3
-25%1
Infinity%