Comparing version 3.5.21 to 3.5.22
@@ -5,2 +5,8 @@ # Changelog | ||
## [3.5.22][] - 2022-07-29 | ||
- New functions: `parseDay` and `parseMonth` | ||
- Fixed and `parseEvery` and `nextEvent`, more tests cases | ||
- Add year support to `Every` format | ||
## [3.5.21][] - 2022-06-27 | ||
@@ -152,3 +158,4 @@ | ||
[unreleased]: https://github.com/metarhia/metautil/compare/v3.5.21....HEAD | ||
[unreleased]: https://github.com/metarhia/metautil/compare/v3.5.22....HEAD | ||
[3.5.22]: https://github.com/metarhia/metautil/compare/v3.5.21...v3.5.22 | ||
[3.5.21]: https://github.com/metarhia/metautil/compare/v3.5.20...v3.5.21 | ||
@@ -155,0 +162,0 @@ [3.5.20]: https://github.com/metarhia/metautil/compare/v3.5.19...v3.5.20 |
@@ -216,7 +216,8 @@ 'use strict'; | ||
const NAME_LEN = 3; | ||
const parseMonth = (s) => { | ||
for (let i = 0; i < MONTHS.length; i++) { | ||
if (s.startsWith(MONTHS[i])) return i; | ||
} | ||
return -1; | ||
const name = s.substring(0, NAME_LEN); | ||
const i = MONTHS.indexOf(name); | ||
return i >= 0 ? i + 1 : -1; | ||
}; | ||
@@ -227,15 +228,17 @@ | ||
const parseDay = (s) => { | ||
for (let i = 0; i < DAYS.length; i++) { | ||
if (s.startsWith(DAYS[i])) return i; | ||
} | ||
return -1; | ||
const name = s.substring(0, NAME_LEN); | ||
const i = DAYS.indexOf(name); | ||
return i >= 0 ? i + 1 : -1; | ||
}; | ||
const YEAR_LEN = 4; | ||
const parseEvery = (s = '') => { | ||
let month = -1; | ||
let day = -1; | ||
let dd = -1; | ||
let YY = -1; | ||
let MM = -1; | ||
let DD = -1; | ||
let wd = -1; | ||
let hh = -1; | ||
let mm = -1; | ||
let interval = 0; | ||
let ms = 0; | ||
const parts = s.split(' '); | ||
@@ -250,9 +253,17 @@ for (const part of parts) { | ||
if (isOrdinal(part)) { | ||
dd = parseInt(part); | ||
DD = parseInt(part); | ||
continue; | ||
} | ||
month = parseMonth(part); | ||
if (month > -1) continue; | ||
day = parseDay(part); | ||
if (day > -1) continue; | ||
if (part.length === YEAR_LEN) { | ||
YY = parseInt(part); | ||
continue; | ||
} | ||
if (MM === -1) { | ||
MM = parseMonth(part); | ||
if (MM > -1) continue; | ||
} | ||
if (wd === -1) { | ||
wd = parseDay(part); | ||
if (wd > -1) continue; | ||
} | ||
const unit = part.slice(-1); | ||
@@ -262,26 +273,42 @@ const mult = DURATION_UNITS[unit]; | ||
const value = parseInt(part); | ||
if (!isNaN(value)) interval += value * mult; | ||
if (!isNaN(value)) ms += value * mult; | ||
} | ||
} | ||
return { month, day, dd, hh, mm, interval: interval * 1000 }; | ||
return { YY, MM, DD, wd, hh, mm, ms: ms > 0 ? ms * 1000 : -1 }; | ||
}; | ||
const nextEvent = (every, date = new Date()) => { | ||
let interval = 0; | ||
const month = date.getUTCMonth(); | ||
if (every.month > -1 && every.month !== month) return -1; | ||
const day = date.getUTCDay(); | ||
if (every.day > -1 && every.day !== day) return -1; | ||
const dd = date.getUTCDate(); | ||
if (every.dd > -1 && every.dd !== dd) return -1; | ||
let ms = 0; | ||
const YY = date.getUTCFullYear(); | ||
const MM = date.getUTCMonth() + 1; | ||
const DD = date.getUTCDate(); | ||
const wd = date.getUTCDay() + 1; | ||
const hh = date.getUTCHours(); | ||
if (every.hh > -1) { | ||
interval += (every.hh - hh) * DURATION_UNITS.h; | ||
} | ||
const mm = date.getUTCMinutes(); | ||
if (every.mm > -1) { | ||
interval += (every.mm - mm) * DURATION_UNITS.m; | ||
if (every.YY > -1) { | ||
if (every.YY < YY) return -1; | ||
if (every.YY > YY) return 0; | ||
if (every.MM > -1) { | ||
if (every.MM < MM) return -1; | ||
if (every.MM > MM) return 0; | ||
if (every.DD > -1) { | ||
if (every.DD < DD) return -1; | ||
if (every.DD > DD) return 0; | ||
if (every.hh > -1) { | ||
if (every.hh < hh) return -1; | ||
if (every.hh === hh) { | ||
if (every.mm > -1 && every.mm < mm) return -1; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
if (interval < 0) return -1; | ||
return interval * 1000 + every.interval; | ||
if (every.MM > -1 && every.MM !== MM) return 0; | ||
if (every.DD > -1 && every.DD !== DD) return 0; | ||
if (every.wd > -1 && every.wd !== wd) return 0; | ||
if (every.hh > -1) ms += (every.hh - hh) * DURATION_UNITS.h; | ||
if (every.mm > -1) ms += (every.mm - mm) * DURATION_UNITS.m; | ||
ms *= 1000; | ||
if (every.ms > -1) ms += every.ms; | ||
return ms; | ||
}; | ||
@@ -363,2 +390,4 @@ | ||
namespaceByPath, | ||
parseMonth, | ||
parseDay, | ||
parseEvery, | ||
@@ -365,0 +394,0 @@ nextEvent, |
@@ -51,8 +51,10 @@ import { EventEmitter } from 'events'; | ||
type Every = { | ||
month: number; | ||
day: number; | ||
YY: number; | ||
MM: number; | ||
DD: number; | ||
wd: number; | ||
dd: number; | ||
hh: number; | ||
mm: number; | ||
interval: number; | ||
ms: number; | ||
}; | ||
@@ -62,2 +64,4 @@ | ||
export function parseDay(s: string): number; | ||
export function parseMonth(s: string): number; | ||
export function parseEvery(s: string): Every; | ||
@@ -64,0 +68,0 @@ export function nextEvent(every: Every, date?: Date): number; |
{ | ||
"name": "metautil", | ||
"version": "3.5.21", | ||
"version": "3.5.22", | ||
"author": "Timur Shemsedinov <timur.shemsedinov@gmail.com>", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -34,2 +34,6 @@ # Metarhia utilities | ||
- `namespaceByPath(namespace: object, path: string): object | null` | ||
- `parseDay(s: string): number` | ||
- `parseMonth(s: string): number` | ||
- `parseEvery(s: string): Every` | ||
- `nextEvent(every: Every, date?: Date): number` | ||
- `makePrivate(instance: object): object` | ||
@@ -36,0 +40,0 @@ - `protect(allowMixins: Array<string>, ...namespaces: Array<object>): void` |
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
34127
766
86