Comparing version 3.5.6 to 3.5.7
@@ -5,2 +5,7 @@ # Changelog | ||
## [3.5.7][] - 2021-07-21 | ||
- Initial implementation of `parseEvery` | ||
- Initial implementation of `nextEvent` | ||
## [3.5.6][] - 2021-07-18 | ||
@@ -78,3 +83,4 @@ | ||
[unreleased]: https://github.com/metarhia/metautil/compare/v3.5.6...HEAD | ||
[unreleased]: https://github.com/metarhia/metautil/compare/v3.5.7...HEAD | ||
[3.5.7]: https://github.com/metarhia/metautil/compare/v3.5.6...v3.5.7 | ||
[3.5.6]: https://github.com/metarhia/metautil/compare/v3.5.5...v3.5.6 | ||
@@ -81,0 +87,0 @@ [3.5.5]: https://github.com/metarhia/metautil/compare/v3.5.4...v3.5.5 |
@@ -119,2 +119,94 @@ 'use strict'; | ||
const ORDINAL = ['st', 'nd', 'rd', 'th']; | ||
const isOrdinal = (s) => { | ||
for (const d of ORDINAL) { | ||
if (s.endsWith(d)) return true; | ||
} | ||
return false; | ||
}; | ||
const MONTHS = [ | ||
'Jan', | ||
'Feb', | ||
'Mar', | ||
'Apr', | ||
'May', | ||
'Jun', | ||
'Jul', | ||
'Aug', | ||
'Sep', | ||
'Oct', | ||
'Nov', | ||
'Dec', | ||
]; | ||
const parseMonth = (s) => { | ||
for (let i = 0; i < MONTHS.length; i++) { | ||
if (s.startsWith(MONTHS[i])) return i; | ||
} | ||
return -1; | ||
}; | ||
const DAYS = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; | ||
const parseDay = (s) => { | ||
for (let i = 0; i < DAYS.length; i++) { | ||
if (s.startsWith(DAYS[i])) return i; | ||
} | ||
return -1; | ||
}; | ||
const parseEvery = (s = '') => { | ||
let month = -1; | ||
let day = -1; | ||
let dd = -1; | ||
let hh = -1; | ||
let mm = -1; | ||
let interval = 0; | ||
const parts = s.split(' '); | ||
for (const part of parts) { | ||
if (part.includes(':')) { | ||
const [h, m] = split(part, ':'); | ||
if (h !== '') hh = parseInt(h); | ||
mm = m === '' ? 0 : parseInt(m); | ||
continue; | ||
} | ||
if (isOrdinal(part)) { | ||
dd = parseInt(part); | ||
continue; | ||
} | ||
month = parseMonth(part); | ||
if (month > -1) continue; | ||
day = parseDay(part); | ||
if (day > -1) continue; | ||
const unit = part.slice(-1); | ||
const mult = DURATION_UNITS[unit]; | ||
if (typeof mult === 'number') { | ||
const value = parseInt(part); | ||
if (!isNaN(value)) interval += value * mult; | ||
} | ||
} | ||
return { month, day, dd, hh, mm, interval: interval * 1000 }; | ||
}; | ||
const nextEvent = (every, date = new Date()) => { | ||
let interval = 0; | ||
const month = date.getUTCMonth(); | ||
if (every.month > -1 && every.month !== month) return 0; | ||
const day = date.getUTCDay(); | ||
if (every.day > -1 && every.day !== day) return 0; | ||
const dd = date.getUTCDate(); | ||
if (every.dd > -1 && every.dd !== dd) return 0; | ||
const hh = date.getUTCHours(); | ||
if (every.hh > -1 && every.hh >= hh) { | ||
interval += (every.hh - hh) * DURATION_UNITS.h; | ||
} | ||
const mm = date.getUTCMinutes(); | ||
if (every.mm > -1 && every.mm >= mm) { | ||
interval += (every.mm - mm) * DURATION_UNITS.m; | ||
} | ||
return interval * 1000 + every.interval; | ||
}; | ||
const makePrivate = (instance) => { | ||
@@ -204,2 +296,4 @@ const iface = {}; | ||
duration, | ||
parseEvery, | ||
nextEvent, | ||
makePrivate, | ||
@@ -206,0 +300,0 @@ protect, |
@@ -36,2 +36,16 @@ import { EventEmitter } from 'events'; | ||
export function duration(s: string | number): number; | ||
type Every = { | ||
month: number; | ||
day: number; | ||
dd: number; | ||
hh: number; | ||
mm: number; | ||
interval: number; | ||
}; | ||
export type { Every }; | ||
export function parseEvery(s: string): Every; | ||
export function nextEvent(every: Every, date?: Date): number; | ||
export function makePrivate(instance: object): object; | ||
@@ -38,0 +52,0 @@ |
{ | ||
"name": "metautil", | ||
"version": "3.5.6", | ||
"version": "3.5.7", | ||
"author": "Timur Shemsedinov <timur.shemsedinov@gmail.com>", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
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
21984
492