@bereasoftware/time-guard 2.5.2 → 2.5.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +1 -1
- package/README.md +1 -1
- package/dist/calendars/index.es.js +1 -1
- package/dist/plugins/advanced-format.cjs.map +1 -1
- package/dist/plugins/advanced-format.es.js +1 -1
- package/dist/plugins/advanced-format.es.js.map +1 -1
- package/dist/plugins/duration.cjs.map +1 -1
- package/dist/plugins/duration.es.js +1 -1
- package/dist/plugins/duration.es.js.map +1 -1
- package/dist/plugins/relative-time.cjs.map +1 -1
- package/dist/plugins/relative-time.es.js +1 -1
- package/dist/plugins/relative-time.es.js.map +1 -1
- package/dist/time-guard.cjs +1 -1
- package/dist/time-guard.cjs.map +1 -1
- package/dist/time-guard.es.js +3586 -3587
- package/dist/time-guard.es.js.map +1 -1
- package/dist/time-guard.iife.js +1 -1
- package/dist/time-guard.iife.js.map +1 -1
- package/dist/time-guard.umd.js +1 -1
- package/dist/time-guard.umd.js.map +1 -1
- package/dist/types/index.d.ts +188 -3
- package/dist/types/plugins/advanced-format/index.d.ts +1 -1
- package/dist/types/plugins/advanced-format.d.ts +6 -0
- package/dist/types/plugins/duration/index.d.ts +1 -1
- package/dist/types/plugins/duration.d.ts +6 -0
- package/dist/types/plugins/manager.d.ts +1 -1
- package/dist/types/plugins/relative-time/index.d.ts +1 -1
- package/dist/types/plugins/relative-time.d.ts +6 -0
- package/dist/types/types/index.d.ts +3 -3
- package/package.json +6 -6
- package/dist/types/time-guard.d.ts +0 -270
package/README.en.md
CHANGED
|
@@ -1524,7 +1524,7 @@ time-guard/
|
|
|
1524
1524
|
├── src/
|
|
1525
1525
|
│ ├── index.ts # Lightweight core (~5KB gzip, EN/ES)
|
|
1526
1526
|
│ ├── polyfill-loader.ts # Temporal polyfill loader
|
|
1527
|
-
│ ├──
|
|
1527
|
+
│ ├── index.ts # Main class and entry point
|
|
1528
1528
|
│ ├── adapters/
|
|
1529
1529
|
│ │ └── temporal.adapter.ts # Temporal API wrapper
|
|
1530
1530
|
│ ├── calendars/ # 6 calendar systems
|
package/README.md
CHANGED
|
@@ -1708,7 +1708,7 @@ time-guard/
|
|
|
1708
1708
|
├── src/
|
|
1709
1709
|
│ ├── index.ts # Core ligero (~5KB gzip, EN/ES)
|
|
1710
1710
|
│ ├── polyfill-loader.ts # Cargador del polyfill Temporal
|
|
1711
|
-
│ ├──
|
|
1711
|
+
│ ├── index.ts # Clase principal y punto de entrada
|
|
1712
1712
|
│ ├── adapters/
|
|
1713
1713
|
│ │ └── temporal.adapter.ts # Wrapper del API Temporal
|
|
1714
1714
|
│ ├── calendars/ # 6 sistemas de calendario
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/*! time-guard v2.5.
|
|
1
|
+
/*! time-guard v2.5.4 | (c) 2026 Berea-Soft | MIT License | https://github.com/Berea-Soft/time-guard */
|
|
2
2
|
//#region src/calendars/index.ts
|
|
3
3
|
var e = class {
|
|
4
4
|
id = "islamic";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"advanced-format.cjs","names":[],"sources":["../../src/plugins/advanced-format/index.ts"],"sourcesContent":["/**\r\n * TimeGuard Advanced Format Plugin\r\n * Adds advanced format tokens: Q (quarter), Do (ordinal), w (week), etc.\r\n * Follows SOLID principles and extends core formatting capabilities\r\n */\r\n\r\nimport type { ITimeGuardPlugin } from '../../types';\r\nimport type { TimeGuard } from '../../
|
|
1
|
+
{"version":3,"file":"advanced-format.cjs","names":[],"sources":["../../src/plugins/advanced-format/index.ts"],"sourcesContent":["/**\r\n * TimeGuard Advanced Format Plugin\r\n * Adds advanced format tokens: Q (quarter), Do (ordinal), w (week), etc.\r\n * Follows SOLID principles and extends core formatting capabilities\r\n */\r\n\r\nimport type { ITimeGuardPlugin } from '../../types';\r\nimport type { TimeGuard } from '../../index';\r\nimport { Temporal } from '@js-temporal/polyfill';\r\n\r\nexport class AdvancedFormatPlugin implements ITimeGuardPlugin {\r\n name = 'advanced-format';\r\n version = '1.0.0';\r\n\r\n /**\r\n * Install plugin into TimeGuard\r\n */\r\n install(TimeGuardClass: typeof TimeGuard): void {\r\n // Store original format method\r\n const originalFormat = TimeGuardClass.prototype.format;\r\n\r\n /**\r\n * Extended format method with advanced tokens\r\n */\r\n (TimeGuardClass.prototype as any).format = function (pattern: string) {\r\n if (!pattern || typeof pattern !== 'string') {\r\n return originalFormat.call(this, pattern);\r\n }\r\n\r\n // Check if pattern contains advanced tokens\r\n if (!/Q|Do|w|W|gggg|GGGG|k{1,2}|X|x|zzz?/.test(pattern)) {\r\n return originalFormat.call(this, pattern);\r\n }\r\n\r\n // Get the underlying Temporal object\r\n const temporal = this.toTemporal();\r\n const temporal_dt =\r\n 'toPlainDateTime' in temporal\r\n ? (temporal as any).toPlainDateTime()\r\n : (temporal as Temporal.PlainDateTime);\r\n \r\n const ordinalFn = (n: number) => {\r\n const suffixes = ['th', 'st', 'nd', 'rd'];\r\n const v = n % 100;\r\n return n + (suffixes[(v - 20) % 10] || suffixes[v] || suffixes[0]);\r\n };\r\n const padFn = (n: number, length: number) => String(n).padStart(length, '0');\r\n const getISOWeek = (temporal: any) => {\r\n const jan4 = new Date(temporal.year, 0, 4);\r\n const week1Start = new Date(jan4);\r\n week1Start.setDate(jan4.getDate() - jan4.getDay() + (jan4.getDay() === 0 ? -6 : 1));\r\n const currentDate = new Date(temporal.year, temporal.month - 1, temporal.day);\r\n const weekNum = Math.floor((currentDate.getTime() - week1Start.getTime()) / (7 * 24 * 60 * 60 * 1000)) + 1;\r\n return Math.max(1, weekNum);\r\n };\r\n const getWeekOfYear = (temporal: any) => {\r\n const weekNum = Math.ceil(((temporal.day + new Date(temporal.year, temporal.month - 1, 1).getDay()) / 7));\r\n return Math.max(1, weekNum);\r\n };\r\n const getISOWeekYear = (temporal: any) => {\r\n const currentDate = new Date(temporal.year, temporal.month - 1, temporal.day);\r\n const yearAdjust = currentDate.getTime() < new Date(temporal.year, 0, 1).getTime() ? -1 : currentDate.getTime() >= new Date(temporal.year + 1, 0, 1).getTime() ? 1 : 0;\r\n return temporal.year + yearAdjust;\r\n };\r\n const getWeekYear = (temporal: any) => {\r\n const yearAdjust = temporal.month === 1 && temporal.day < 4 ? -1 : temporal.month === 12 && temporal.day > 28 ? 1 : 0;\r\n return temporal.year + yearAdjust;\r\n };\r\n\r\n // Replace advanced tokens - wrap results in brackets to protect from standard formatter\r\n let result = pattern.replace(\r\n /Q|Do|w|W|gggg|GGGG|k{1,2}|X|x|zzz?/g,\r\n (match) => {\r\n let replacement = '';\r\n switch (match) {\r\n // Quarter\r\n case 'Q':\r\n replacement = String(Math.ceil((temporal_dt.month) / 3));\r\n break;\r\n\r\n // Ordinal day\r\n case 'Do':\r\n replacement = ordinalFn(temporal_dt.day);\r\n break;\r\n\r\n // Week of year (ISO)\r\n case 'W':\r\n case 'WW':\r\n replacement = padFn(getISOWeek(temporal_dt), match === 'W' ? 1 : 2);\r\n break;\r\n\r\n // Week of year (locale)\r\n case 'w':\r\n case 'ww':\r\n replacement = padFn(getWeekOfYear(temporal_dt), match === 'w' ? 1 : 2);\r\n break;\r\n\r\n // ISO week year\r\n case 'GGGG':\r\n replacement = String(getISOWeekYear(temporal_dt));\r\n break;\r\n\r\n // Week year\r\n case 'gggg':\r\n replacement = String(getWeekYear(temporal_dt));\r\n break;\r\n\r\n // Hour (1-24)\r\n case 'k':\r\n case 'kk':\r\n const hour = temporal_dt.hour === 0 ? 24 : temporal_dt.hour;\r\n replacement = padFn(hour, match === 'k' ? 1 : 2);\r\n break;\r\n\r\n // Unix seconds timestamp\r\n case 'X':\r\n replacement = String(Math.floor(this.valueOf() / 1000));\r\n break;\r\n\r\n // Unix milliseconds timestamp\r\n case 'x':\r\n replacement = String(this.valueOf());\r\n break;\r\n\r\n // Timezone offset\r\n case 'z':\r\n replacement = `${this.getTimezoneOffset()}`;\r\n break;\r\n\r\n case 'zzz':\r\n replacement = `${this.getTimezoneOffsetLong()}`;\r\n break;\r\n\r\n default:\r\n return match;\r\n }\r\n // Wrap in brackets to protect from standard formatter\r\n return `[${replacement}]`;\r\n },\r\n );\r\n\r\n // Apply standard format to the result\r\n return originalFormat.call(this, result);\r\n };\r\n }\r\n}\r\n\r\n/**\r\n * Create and export default instance\r\n */\r\nexport default new AdvancedFormatPlugin();\r\n"],"mappings":"+FAUA,IAAa,EAAb,KAA8D,CAC5D,KAAO,kBACP,QAAU,QAKV,QAAQ,EAAwC,CAE9C,IAAM,EAAiB,EAAe,UAAU,OAK/C,EAAe,UAAkB,OAAS,SAAU,EAAiB,CAMpE,GALI,CAAC,GAAW,OAAO,GAAY,UAK/B,CAAC,qCAAqC,KAAK,EAAQ,CACrD,OAAO,EAAe,KAAK,KAAM,EAAQ,CAI3C,IAAM,EAAW,KAAK,YAAY,CAC5B,EACJ,oBAAqB,EAChB,EAAiB,iBAAiB,CAClC,EAED,EAAa,GAAc,CAC/B,IAAM,EAAW,CAAC,KAAM,KAAM,KAAM,KAAK,CACnC,EAAI,EAAI,IACd,OAAO,GAAK,GAAU,EAAI,IAAM,KAAO,EAAS,IAAM,EAAS,KAE3D,GAAS,EAAW,IAAmB,OAAO,EAAE,CAAC,SAAS,EAAQ,IAAI,CACtE,EAAc,GAAkB,CACpC,IAAM,EAAO,IAAI,KAAK,EAAS,KAAM,EAAG,EAAE,CACpC,EAAa,IAAI,KAAK,EAAK,CACjC,EAAW,QAAQ,EAAK,SAAS,CAAG,EAAK,QAAQ,EAAI,EAAK,QAAQ,GAAK,EAAI,GAAK,GAAG,CACnF,IAAM,EAAc,IAAI,KAAK,EAAS,KAAM,EAAS,MAAQ,EAAG,EAAS,IAAI,CACvE,EAAU,KAAK,OAAO,EAAY,SAAS,CAAG,EAAW,SAAS,GAAK,MAAc,GAAK,KAAM,CAAG,EACzG,OAAO,KAAK,IAAI,EAAG,EAAQ,EAEvB,EAAiB,GAAkB,CACvC,IAAM,EAAU,KAAK,MAAO,EAAS,IAAM,IAAI,KAAK,EAAS,KA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K,IACH,EAAc,GAAG,KAAK,mBAAmB,GACzC,MAEF,IAAK,MACH,EAAc,GAAG,KAAK,uBAAuB,GAC7C,MAEF,QACE,OAAO,EAGX,MAAO,IAAI,EAAY,IAE1B,CAGD,OAAO,EAAe,KAAK,KAAM,EAAO,IAQ9C,EAAe,IAAI"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/*! time-guard v2.5.
|
|
1
|
+
/*! time-guard v2.5.4 | (c) 2026 Berea-Soft | MIT License | https://github.com/Berea-Soft/time-guard */
|
|
2
2
|
//#region src/plugins/advanced-format/index.ts
|
|
3
3
|
var e = class {
|
|
4
4
|
name = "advanced-format";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"advanced-format.es.js","names":[],"sources":["../../src/plugins/advanced-format/index.ts"],"sourcesContent":["/**\r\n * TimeGuard Advanced Format Plugin\r\n * Adds advanced format tokens: Q (quarter), Do (ordinal), w (week), etc.\r\n * Follows SOLID principles and extends core formatting capabilities\r\n */\r\n\r\nimport type { ITimeGuardPlugin } from '../../types';\r\nimport type { TimeGuard } from '../../
|
|
1
|
+
{"version":3,"file":"advanced-format.es.js","names":[],"sources":["../../src/plugins/advanced-format/index.ts"],"sourcesContent":["/**\r\n * TimeGuard Advanced Format Plugin\r\n * Adds advanced format tokens: Q (quarter), Do (ordinal), w (week), etc.\r\n * Follows SOLID principles and extends core formatting capabilities\r\n */\r\n\r\nimport type { ITimeGuardPlugin } from '../../types';\r\nimport type { TimeGuard } from '../../index';\r\nimport { Temporal } from '@js-temporal/polyfill';\r\n\r\nexport class AdvancedFormatPlugin implements ITimeGuardPlugin {\r\n name = 'advanced-format';\r\n version = '1.0.0';\r\n\r\n /**\r\n * Install plugin into TimeGuard\r\n */\r\n install(TimeGuardClass: typeof TimeGuard): void {\r\n // Store original format method\r\n const originalFormat = TimeGuardClass.prototype.format;\r\n\r\n /**\r\n * Extended format method with advanced tokens\r\n */\r\n (TimeGuardClass.prototype as any).format = function (pattern: string) {\r\n if (!pattern || typeof pattern !== 'string') {\r\n return originalFormat.call(this, pattern);\r\n }\r\n\r\n // Check if pattern contains advanced tokens\r\n if (!/Q|Do|w|W|gggg|GGGG|k{1,2}|X|x|zzz?/.test(pattern)) {\r\n return originalFormat.call(this, pattern);\r\n }\r\n\r\n // Get the underlying Temporal object\r\n const temporal = this.toTemporal();\r\n const temporal_dt =\r\n 'toPlainDateTime' in temporal\r\n ? (temporal as any).toPlainDateTime()\r\n : (temporal as Temporal.PlainDateTime);\r\n \r\n const ordinalFn = (n: number) => {\r\n const suffixes = ['th', 'st', 'nd', 'rd'];\r\n const v = n % 100;\r\n return n + (suffixes[(v - 20) % 10] || suffixes[v] || suffixes[0]);\r\n };\r\n const padFn = (n: number, length: number) => String(n).padStart(length, '0');\r\n const getISOWeek = (temporal: any) => {\r\n const jan4 = new Date(temporal.year, 0, 4);\r\n const week1Start = new Date(jan4);\r\n week1Start.setDate(jan4.getDate() - jan4.getDay() + (jan4.getDay() === 0 ? -6 : 1));\r\n const currentDate = new Date(temporal.year, temporal.month - 1, temporal.day);\r\n const weekNum = Math.floor((currentDate.getTime() - week1Start.getTime()) / (7 * 24 * 60 * 60 * 1000)) + 1;\r\n return Math.max(1, weekNum);\r\n };\r\n const getWeekOfYear = (temporal: any) => {\r\n const weekNum = Math.ceil(((temporal.day + new Date(temporal.year, temporal.month - 1, 1).getDay()) / 7));\r\n return Math.max(1, weekNum);\r\n };\r\n const getISOWeekYear = (temporal: any) => {\r\n const currentDate = new Date(temporal.year, temporal.month - 1, temporal.day);\r\n const yearAdjust = currentDate.getTime() < new Date(temporal.year, 0, 1).getTime() ? -1 : currentDate.getTime() >= new Date(temporal.year + 1, 0, 1).getTime() ? 1 : 0;\r\n return temporal.year + yearAdjust;\r\n };\r\n const getWeekYear = (temporal: any) => {\r\n const yearAdjust = temporal.month === 1 && temporal.day < 4 ? -1 : temporal.month === 12 && temporal.day > 28 ? 1 : 0;\r\n return temporal.year + yearAdjust;\r\n };\r\n\r\n // Replace advanced tokens - wrap results in brackets to protect from standard formatter\r\n let result = pattern.replace(\r\n /Q|Do|w|W|gggg|GGGG|k{1,2}|X|x|zzz?/g,\r\n (match) => {\r\n let replacement = '';\r\n switch (match) {\r\n // Quarter\r\n case 'Q':\r\n replacement = String(Math.ceil((temporal_dt.month) / 3));\r\n break;\r\n\r\n // Ordinal day\r\n case 'Do':\r\n replacement = ordinalFn(temporal_dt.day);\r\n break;\r\n\r\n // Week of year (ISO)\r\n case 'W':\r\n case 'WW':\r\n replacement = padFn(getISOWeek(temporal_dt), match === 'W' ? 1 : 2);\r\n break;\r\n\r\n // Week of year (locale)\r\n case 'w':\r\n case 'ww':\r\n replacement = padFn(getWeekOfYear(temporal_dt), match === 'w' ? 1 : 2);\r\n break;\r\n\r\n // ISO week year\r\n case 'GGGG':\r\n replacement = String(getISOWeekYear(temporal_dt));\r\n break;\r\n\r\n // Week year\r\n case 'gggg':\r\n replacement = String(getWeekYear(temporal_dt));\r\n break;\r\n\r\n // Hour (1-24)\r\n case 'k':\r\n case 'kk':\r\n const hour = temporal_dt.hour === 0 ? 24 : temporal_dt.hour;\r\n replacement = padFn(hour, match === 'k' ? 1 : 2);\r\n break;\r\n\r\n // Unix seconds timestamp\r\n case 'X':\r\n replacement = String(Math.floor(this.valueOf() / 1000));\r\n break;\r\n\r\n // Unix milliseconds timestamp\r\n case 'x':\r\n replacement = String(this.valueOf());\r\n break;\r\n\r\n // Timezone offset\r\n case 'z':\r\n replacement = `${this.getTimezoneOffset()}`;\r\n break;\r\n\r\n case 'zzz':\r\n replacement = `${this.getTimezoneOffsetLong()}`;\r\n break;\r\n\r\n default:\r\n return match;\r\n }\r\n // Wrap in brackets to protect from standard formatter\r\n return `[${replacement}]`;\r\n },\r\n );\r\n\r\n // Apply standard format to the result\r\n return originalFormat.call(this, result);\r\n };\r\n }\r\n}\r\n\r\n/**\r\n * Create and export default instance\r\n */\r\nexport default new AdvancedFormatPlugin();\r\n"],"mappings":";;AAUA,IAA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|
|
|
1
|
-
{"version":3,"file":"duration.cjs","names":[],"sources":["../../src/plugins/duration/index.ts"],"sourcesContent":["/**\r\n * TimeGuard Duration Plugin\r\n * Implements ISO 8601 duration support and time calculations\r\n * Follows SOLID principles and enables advanced duration operations\r\n */\r\n\r\nimport type { ITimeGuardPlugin } from '../../types';\r\nimport type { TimeGuard } from '../../time-guard';\r\nimport type { DurationInput, DurationObject, DurationUnit, IDuration } from './types';\r\n\r\n/**\r\n * Duration class - represents time span following ISO 8601 standard\r\n */\r\nexport class Duration implements IDuration {\r\n private years: number = 0;\r\n private months: number = 0;\r\n private weeks: number = 0;\r\n private days: number = 0;\r\n private hours: number = 0;\r\n private minutes: number = 0;\r\n private seconds: number = 0;\r\n private milliseconds: number = 0;\r\n\r\n constructor(input: DurationInput) {\r\n this.years = input.years || 0;\r\n this.months = input.months || 0;\r\n this.weeks = input.weeks || 0;\r\n this.days = input.days || 0;\r\n this.hours = input.hours || 0;\r\n this.minutes = input.minutes || 0;\r\n this.seconds = input.seconds || 0;\r\n this.milliseconds = input.milliseconds || 0;\r\n }\r\n\r\n /**\r\n * Create Duration from ISO 8601 string\r\n * @example Duration.fromISO(\"P3Y6M4DT12H30M5S\")\r\n */\r\n static fromISO(iso: string): Duration {\r\n const isoRegex = /^(-)?P(?:(\\d+)Y)?(?:(\\d+)M)?(?:(\\d+)W)?(?:(\\d+)D)?(?:T(?:(\\d+)H)?(?:(\\d+)M)?(?:([\\d.]+)S)?)?$/;\r\n const match = iso.match(isoRegex);\r\n\r\n if (!match) {\r\n throw new Error(`Invalid ISO 8601 duration: ${iso}`);\r\n }\r\n\r\n const [, negative, years, months, , days, hours, minutes, seconds] = match;\r\n const multiplier = negative ? -1 : 1;\r\n\r\n return new Duration({\r\n years: parseInt(years || '0', 10) * multiplier,\r\n months: parseInt(months || '0', 10) * multiplier,\r\n days: parseInt(days || '0', 10) * multiplier,\r\n hours: parseInt(hours || '0', 10) * multiplier,\r\n minutes: parseInt(minutes || '0', 10) * multiplier,\r\n seconds: parseFloat(seconds || '0') * multiplier,\r\n });\r\n }\r\n\r\n /**\r\n * Create a Duration between this date and another\r\n */\r\n static between(from: TimeGuard, to: TimeGuard): Duration {\r\n // Calculate (to - from) using since() for proper duration\r\n const fromDT = (from as any).toTemporal();\r\n const toDT = (to as any).toTemporal();\r\n const duration = (toDT as any).since(fromDT);\r\n \r\n return new Duration({\r\n years: duration.years || 0,\r\n months: duration.months || 0,\r\n weeks: 0,\r\n days: duration.days || 0,\r\n hours: duration.hours || 0,\r\n minutes: duration.minutes || 0,\r\n seconds: duration.seconds || 0,\r\n milliseconds: duration.milliseconds || 0,\r\n });\r\n }\r\n\r\n /**\r\n * Create Duration from milliseconds\r\n */\r\n static fromMilliseconds(ms: number): Duration {\r\n const isNegative = ms < 0;\r\n const absMilhs = Math.abs(ms);\r\n\r\n const years = Math.floor(absMilhs / (1000 * 60 * 60 * 24 * 365));\r\n const months = Math.floor((absMilhs % (1000 * 60 * 60 * 24 * 365)) / (1000 * 60 * 60 * 24 * 30));\r\n const days = Math.floor((absMilhs % (1000 * 60 * 60 * 24 * 30)) / (1000 * 60 * 60 * 24));\r\n const hours = Math.floor((absMilhs % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));\r\n const minutes = Math.floor((absMilhs % (1000 * 60 * 60)) / (1000 * 60));\r\n const seconds = Math.floor((absMilhs % (1000 * 60)) / 1000);\r\n const milliseconds = Math.floor(absMilhs % 1000);\r\n\r\n const multiplier = isNegative ? -1 : 1;\r\n\r\n return new Duration({\r\n years: years * multiplier,\r\n months: months * multiplier,\r\n days: days * multiplier,\r\n hours: hours * multiplier,\r\n minutes: minutes * multiplier,\r\n seconds: seconds * multiplier,\r\n milliseconds: milliseconds * multiplier,\r\n });\r\n }\r\n\r\n /**\r\n * Get duration in specific unit\r\n */\r\n as(unit: DurationUnit): number {\r\n const ms = this.asMilliseconds();\r\n\r\n const conversions: Record<DurationUnit, number> = {\r\n milliseconds: 1,\r\n seconds: 1000,\r\n minutes: 1000 * 60,\r\n hours: 1000 * 60 * 60,\r\n days: 1000 * 60 * 60 * 24,\r\n weeks: 1000 * 60 * 60 * 24 * 7,\r\n months: 1000 * 60 * 60 * 24 * 30,\r\n years: 1000 * 60 * 60 * 24 * 365,\r\n };\r\n\r\n return ms / conversions[unit];\r\n }\r\n\r\n /**\r\n * Get duration as milliseconds\r\n */\r\n asMilliseconds(): number {\r\n return (\r\n this.years * 1000 * 60 * 60 * 24 * 365 +\r\n this.months * 1000 * 60 * 60 * 24 * 30 +\r\n this.weeks * 1000 * 60 * 60 * 24 * 7 +\r\n this.days * 1000 * 60 * 60 * 24 +\r\n this.hours * 1000 * 60 * 60 +\r\n this.minutes * 1000 * 60 +\r\n this.seconds * 1000 +\r\n this.milliseconds\r\n );\r\n }\r\n\r\n /**\r\n * Get duration as seconds\r\n */\r\n asSeconds(): number {\r\n return this.asMilliseconds() / 1000;\r\n }\r\n\r\n /**\r\n * Get duration as minutes\r\n */\r\n asMinutes(): number {\r\n return this.asSeconds() / 60;\r\n }\r\n\r\n /**\r\n * Get duration as hours\r\n */\r\n asHours(): number {\r\n return this.asMinutes() / 60;\r\n }\r\n\r\n /**\r\n * Get duration as days\r\n */\r\n asDays(): number {\r\n return this.asHours() / 24;\r\n }\r\n\r\n /**\r\n * Get duration as weeks\r\n */\r\n asWeeks(): number {\r\n return this.asDays() / 7;\r\n }\r\n\r\n /**\r\n * Get duration as months\r\n */\r\n asMonths(): number {\r\n return this.asDays() / 30;\r\n }\r\n\r\n /**\r\n * Get duration as years\r\n */\r\n asYears(): number {\r\n return this.asDays() / 365;\r\n }\r\n\r\n /**\r\n * Get all components\r\n */\r\n toObject(): DurationObject {\r\n return {\r\n years: this.years,\r\n months: this.months,\r\n weeks: this.weeks,\r\n days: this.days,\r\n hours: this.hours,\r\n minutes: this.minutes,\r\n seconds: this.seconds,\r\n milliseconds: this.milliseconds,\r\n };\r\n }\r\n\r\n /**\r\n * Get ISO 8601 string representation\r\n * @example \"P3Y6M4DT12H30M5S\"\r\n */\r\n toISO(): string {\r\n\r\n // Date part\r\n let datePart = '';\r\n if (this.years) datePart += `${this.years}Y`;\r\n if (this.months) datePart += `${this.months}M`;\r\n if (this.weeks || this.days) datePart += `${this.weeks * 7 + this.days}D`;\r\n\r\n // Time part\r\n let timePart = '';\r\n if (this.hours) timePart += `${this.hours}H`;\r\n if (this.minutes) timePart += `${this.minutes}M`;\r\n if (this.seconds || this.milliseconds) {\r\n timePart += `${this.seconds + this.milliseconds / 1000}S`;\r\n }\r\n\r\n const sign = this.isNegative() ? '-' : '';\r\n return `${sign}P${datePart}${timePart ? `T${timePart}` : ''}`;\r\n }\r\n\r\n /**\r\n * Get human-readable string\r\n */\r\n humanize(): string {\r\n const parts: string[] = [];\r\n\r\n if (this.years) parts.push(`${Math.abs(this.years)} year${Math.abs(this.years) !== 1 ? 's' : ''}`);\r\n if (this.months) parts.push(`${Math.abs(this.months)} month${Math.abs(this.months) !== 1 ? 's' : ''}`);\r\n if (this.weeks) parts.push(`${Math.abs(this.weeks)} week${Math.abs(this.weeks) !== 1 ? 's' : ''}`);\r\n if (this.days) parts.push(`${Math.abs(this.days)} day${Math.abs(this.days) !== 1 ? 's' : ''}`);\r\n if (this.hours) parts.push(`${Math.abs(this.hours)} hour${Math.abs(this.hours) !== 1 ? 's' : ''}`);\r\n if (this.minutes) parts.push(`${Math.abs(this.minutes)} minute${Math.abs(this.minutes) !== 1 ? 's' : ''}`);\r\n if (this.seconds) parts.push(`${Math.abs(this.seconds)} second${Math.abs(this.seconds) !== 1 ? 's' : ''}`);\r\n if (this.milliseconds) parts.push(`${Math.abs(this.milliseconds)} ms`);\r\n\r\n if (parts.length === 0) return '0 seconds';\r\n\r\n const text = parts.join(', ');\r\n return this.isNegative() ? `-${text}` : text;\r\n }\r\n\r\n /**\r\n * Check if duration is negative\r\n */\r\n isNegative(): boolean {\r\n return (\r\n this.years < 0 ||\r\n this.months < 0 ||\r\n this.weeks < 0 ||\r\n this.days < 0 ||\r\n this.hours < 0 ||\r\n this.minutes < 0 ||\r\n this.seconds < 0 ||\r\n this.milliseconds < 0\r\n );\r\n }\r\n\r\n /**\r\n * Get absolute duration\r\n */\r\n abs(): Duration {\r\n return new Duration({\r\n years: Math.abs(this.years),\r\n months: Math.abs(this.months),\r\n weeks: Math.abs(this.weeks),\r\n days: Math.abs(this.days),\r\n hours: Math.abs(this.hours),\r\n minutes: Math.abs(this.minutes),\r\n seconds: Math.abs(this.seconds),\r\n milliseconds: Math.abs(this.milliseconds),\r\n });\r\n }\r\n\r\n /**\r\n * String representation\r\n */\r\n toString(): string {\r\n return this.toISO();\r\n }\r\n}\r\n\r\n/**\r\n * Duration Plugin\r\n */\r\nexport class DurationPlugin implements ITimeGuardPlugin {\r\n name = 'duration';\r\n version = '1.0.0';\r\n\r\n /**\r\n * Install plugin into TimeGuard\r\n */\r\n install(TimeGuardClass: typeof TimeGuard): void {\r\n /**\r\n * Create a Duration between this date and another\r\n */\r\n (TimeGuardClass.prototype as any).duration = function (other: TimeGuard): Duration {\r\n return Duration.between(this, other);\r\n };\r\n\r\n /**\r\n * Get Duration class for creating custom durations\r\n */\r\n (TimeGuardClass as any).Duration = Duration;\r\n\r\n /**\r\n * Create Duration from ISO 8601 string\r\n */\r\n (TimeGuardClass as any).duration = {\r\n fromISO: (iso: string): Duration => Duration.fromISO(iso),\r\n between: (from: TimeGuard, to: TimeGuard): Duration => Duration.between(from, to),\r\n fromMilliseconds: (ms: number): Duration => Duration.fromMilliseconds(ms),\r\n };\r\n }\r\n}\r\n\r\n/**\r\n * Create and export default instance\r\n */\r\nexport const durationPlugin = new DurationPlugin();\r\n\r\nexport default durationPlugin;\r\n"],"mappings":"+FAaA,IAAa,EAAb,MAAa,CAA8B,CACzC,MAAwB,EACxB,OAAyB,EACzB,MAAwB,EACxB,KAAuB,EACvB,MAAwB,EACxB,QAA0B,EAC1B,QAA0B,EAC1B,aAA+B,EAE/B,YAAY,EAAsB,CAChC,KAAK,MAAQ,EAAM,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P,QAAU,QAKV,QAAQ,EAAwC,CAI7C,EAAe,UAAkB,SAAW,SAAU,EAA4B,CACjF,OAAO,EAAS,QAAQ,KAAM,EAAM,EAMrC,EAAuB,SAAW,EAKlC,EAAuB,SAAW,CACjC,QAAU,GAA0B,EAAS,QAAQ,EAAI,CACzD,SAAU,EAAiB,IAA4B,EAAS,QAAQ,EAAM,EAAG,CACjF,iBAAmB,GAAyB,EAAS,iBAAiB,EAAG,CAC1E,GAOQ,EAAiB,IAAI"}
|
|
1
|
+
{"version":3,"file":"duration.cjs","names":[],"sources":["../../src/plugins/duration/index.ts"],"sourcesContent":["/**\r\n * TimeGuard Duration Plugin\r\n * Implements ISO 8601 duration support and time calculations\r\n * Follows SOLID principles and enables advanced duration operations\r\n */\r\n\r\nimport type { ITimeGuardPlugin } from '../../types';\r\nimport type { TimeGuard } from '../../index';\r\nimport type { DurationInput, DurationObject, DurationUnit, IDuration } from './types';\r\n\r\n/**\r\n * Duration class - represents time span following ISO 8601 standard\r\n */\r\nexport class Duration implements IDuration {\r\n private years: number = 0;\r\n private months: number = 0;\r\n private weeks: number = 0;\r\n private days: number = 0;\r\n private hours: number = 0;\r\n private minutes: number = 0;\r\n private seconds: number = 0;\r\n private milliseconds: number = 0;\r\n\r\n constructor(input: DurationInput) {\r\n this.years = input.years || 0;\r\n this.months = input.months || 0;\r\n this.weeks = input.weeks || 0;\r\n this.days = input.days || 0;\r\n this.hours = input.hours || 0;\r\n this.minutes = input.minutes || 0;\r\n this.seconds = input.seconds || 0;\r\n this.milliseconds = input.milliseconds || 0;\r\n }\r\n\r\n /**\r\n * Create Duration from ISO 8601 string\r\n * @example Duration.fromISO(\"P3Y6M4DT12H30M5S\")\r\n */\r\n static fromISO(iso: string): Duration {\r\n const isoRegex = /^(-)?P(?:(\\d+)Y)?(?:(\\d+)M)?(?:(\\d+)W)?(?:(\\d+)D)?(?:T(?:(\\d+)H)?(?:(\\d+)M)?(?:([\\d.]+)S)?)?$/;\r\n const match = iso.match(isoRegex);\r\n\r\n if (!match) {\r\n throw new Error(`Invalid ISO 8601 duration: ${iso}`);\r\n }\r\n\r\n const [, negative, years, months, , days, hours, minutes, seconds] = match;\r\n const multiplier = negative ? -1 : 1;\r\n\r\n return new Duration({\r\n years: parseInt(years || '0', 10) * multiplier,\r\n months: parseInt(months || '0', 10) * multiplier,\r\n days: parseInt(days || '0', 10) * multiplier,\r\n hours: parseInt(hours || '0', 10) * multiplier,\r\n minutes: parseInt(minutes || '0', 10) * multiplier,\r\n seconds: parseFloat(seconds || '0') * multiplier,\r\n });\r\n }\r\n\r\n /**\r\n * Create a Duration between this date and another\r\n */\r\n static between(from: TimeGuard, to: TimeGuard): Duration {\r\n // Calculate (to - from) using since() for proper duration\r\n const fromDT = (from as any).toTemporal();\r\n const toDT = (to as any).toTemporal();\r\n const duration = (toDT as any).since(fromDT);\r\n \r\n return new Duration({\r\n years: duration.years || 0,\r\n months: duration.months || 0,\r\n weeks: 0,\r\n days: duration.days || 0,\r\n hours: duration.hours || 0,\r\n minutes: duration.minutes || 0,\r\n seconds: duration.seconds || 0,\r\n milliseconds: duration.milliseconds || 0,\r\n });\r\n }\r\n\r\n /**\r\n * Create Duration from milliseconds\r\n */\r\n static fromMilliseconds(ms: number): Duration {\r\n const isNegative = ms < 0;\r\n const absMilhs = Math.abs(ms);\r\n\r\n const years = Math.floor(absMilhs / (1000 * 60 * 60 * 24 * 365));\r\n const months = Math.floor((absMilhs % (1000 * 60 * 60 * 24 * 365)) / (1000 * 60 * 60 * 24 * 30));\r\n const days = Math.floor((absMilhs % (1000 * 60 * 60 * 24 * 30)) / (1000 * 60 * 60 * 24));\r\n const hours = Math.floor((absMilhs % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));\r\n const minutes = Math.floor((absMilhs % (1000 * 60 * 60)) / (1000 * 60));\r\n const seconds = Math.floor((absMilhs % (1000 * 60)) / 1000);\r\n const milliseconds = Math.floor(absMilhs % 1000);\r\n\r\n const multiplier = isNegative ? -1 : 1;\r\n\r\n return new Duration({\r\n years: years * multiplier,\r\n months: months * multiplier,\r\n days: days * multiplier,\r\n hours: hours * multiplier,\r\n minutes: minutes * multiplier,\r\n seconds: seconds * multiplier,\r\n milliseconds: milliseconds * multiplier,\r\n });\r\n }\r\n\r\n /**\r\n * Get duration in specific unit\r\n */\r\n as(unit: DurationUnit): number {\r\n const ms = this.asMilliseconds();\r\n\r\n const conversions: Record<DurationUnit, number> = {\r\n milliseconds: 1,\r\n seconds: 1000,\r\n minutes: 1000 * 60,\r\n hours: 1000 * 60 * 60,\r\n days: 1000 * 60 * 60 * 24,\r\n weeks: 1000 * 60 * 60 * 24 * 7,\r\n months: 1000 * 60 * 60 * 24 * 30,\r\n years: 1000 * 60 * 60 * 24 * 365,\r\n };\r\n\r\n return ms / conversions[unit];\r\n }\r\n\r\n /**\r\n * Get duration as milliseconds\r\n */\r\n asMilliseconds(): number {\r\n return (\r\n this.years * 1000 * 60 * 60 * 24 * 365 +\r\n this.months * 1000 * 60 * 60 * 24 * 30 +\r\n this.weeks * 1000 * 60 * 60 * 24 * 7 +\r\n this.days * 1000 * 60 * 60 * 24 +\r\n this.hours * 1000 * 60 * 60 +\r\n this.minutes * 1000 * 60 +\r\n this.seconds * 1000 +\r\n this.milliseconds\r\n );\r\n }\r\n\r\n /**\r\n * Get duration as seconds\r\n */\r\n asSeconds(): number {\r\n return this.asMilliseconds() / 1000;\r\n }\r\n\r\n /**\r\n * Get duration as minutes\r\n */\r\n asMinutes(): number {\r\n return this.asSeconds() / 60;\r\n }\r\n\r\n /**\r\n * Get duration as hours\r\n */\r\n asHours(): number {\r\n return this.asMinutes() / 60;\r\n }\r\n\r\n /**\r\n * Get duration as days\r\n */\r\n asDays(): number {\r\n return this.asHours() / 24;\r\n }\r\n\r\n /**\r\n * Get duration as weeks\r\n */\r\n asWeeks(): number {\r\n return this.asDays() / 7;\r\n }\r\n\r\n /**\r\n * Get duration as months\r\n */\r\n asMonths(): number {\r\n return this.asDays() / 30;\r\n }\r\n\r\n /**\r\n * Get duration as years\r\n */\r\n asYears(): number {\r\n return this.asDays() / 365;\r\n }\r\n\r\n /**\r\n * Get all components\r\n */\r\n toObject(): DurationObject {\r\n return {\r\n years: this.years,\r\n months: this.months,\r\n weeks: this.weeks,\r\n days: this.days,\r\n hours: this.hours,\r\n minutes: this.minutes,\r\n seconds: this.seconds,\r\n milliseconds: this.milliseconds,\r\n };\r\n }\r\n\r\n /**\r\n * Get ISO 8601 string representation\r\n * @example \"P3Y6M4DT12H30M5S\"\r\n */\r\n toISO(): string {\r\n\r\n // Date part\r\n let datePart = '';\r\n if (this.years) datePart += `${this.years}Y`;\r\n if (this.months) datePart += `${this.months}M`;\r\n if (this.weeks || this.days) datePart += `${this.weeks * 7 + this.days}D`;\r\n\r\n // Time part\r\n let timePart = '';\r\n if (this.hours) timePart += `${this.hours}H`;\r\n if (this.minutes) timePart += `${this.minutes}M`;\r\n if (this.seconds || this.milliseconds) {\r\n timePart += `${this.seconds + this.milliseconds / 1000}S`;\r\n }\r\n\r\n const sign = this.isNegative() ? '-' : '';\r\n return `${sign}P${datePart}${timePart ? `T${timePart}` : ''}`;\r\n }\r\n\r\n /**\r\n * Get human-readable string\r\n */\r\n humanize(): string {\r\n const parts: string[] = [];\r\n\r\n if (this.years) parts.push(`${Math.abs(this.years)} year${Math.abs(this.years) !== 1 ? 's' : ''}`);\r\n if (this.months) parts.push(`${Math.abs(this.months)} month${Math.abs(this.months) !== 1 ? 's' : ''}`);\r\n if (this.weeks) parts.push(`${Math.abs(this.weeks)} week${Math.abs(this.weeks) !== 1 ? 's' : ''}`);\r\n if (this.days) parts.push(`${Math.abs(this.days)} day${Math.abs(this.days) !== 1 ? 's' : ''}`);\r\n if (this.hours) parts.push(`${Math.abs(this.hours)} hour${Math.abs(this.hours) !== 1 ? 's' : ''}`);\r\n if (this.minutes) parts.push(`${Math.abs(this.minutes)} minute${Math.abs(this.minutes) !== 1 ? 's' : ''}`);\r\n if (this.seconds) parts.push(`${Math.abs(this.seconds)} second${Math.abs(this.seconds) !== 1 ? 's' : ''}`);\r\n if (this.milliseconds) parts.push(`${Math.abs(this.milliseconds)} ms`);\r\n\r\n if (parts.length === 0) return '0 seconds';\r\n\r\n const text = parts.join(', ');\r\n return this.isNegative() ? `-${text}` : text;\r\n }\r\n\r\n /**\r\n * Check if duration is negative\r\n */\r\n isNegative(): boolean {\r\n return (\r\n this.years < 0 ||\r\n this.months < 0 ||\r\n this.weeks < 0 ||\r\n this.days < 0 ||\r\n this.hours < 0 ||\r\n this.minutes < 0 ||\r\n this.seconds < 0 ||\r\n this.milliseconds < 0\r\n );\r\n }\r\n\r\n /**\r\n * Get absolute duration\r\n */\r\n abs(): Duration {\r\n return new Duration({\r\n years: Math.abs(this.years),\r\n months: Math.abs(this.months),\r\n weeks: Math.abs(this.weeks),\r\n days: Math.abs(this.days),\r\n hours: Math.abs(this.hours),\r\n minutes: Math.abs(this.minutes),\r\n seconds: Math.abs(this.seconds),\r\n milliseconds: Math.abs(this.milliseconds),\r\n });\r\n }\r\n\r\n /**\r\n * String representation\r\n */\r\n toString(): string {\r\n return this.toISO();\r\n }\r\n}\r\n\r\n/**\r\n * Duration Plugin\r\n */\r\nexport class DurationPlugin implements ITimeGuardPlugin {\r\n name = 'duration';\r\n version = '1.0.0';\r\n\r\n /**\r\n * Install plugin into TimeGuard\r\n */\r\n install(TimeGuardClass: typeof TimeGuard): void {\r\n /**\r\n * Create a Duration between this date and another\r\n */\r\n (TimeGuardClass.prototype as any).duration = function (other: TimeGuard): Duration {\r\n return Duration.between(this, other);\r\n };\r\n\r\n /**\r\n * Get Duration class for creating custom durations\r\n */\r\n (TimeGuardClass as any).Duration = Duration;\r\n\r\n /**\r\n * Create Duration from ISO 8601 string\r\n */\r\n (TimeGuardClass as any).duration = {\r\n fromISO: (iso: string): Duration => Duration.fromISO(iso),\r\n between: (from: TimeGuard, to: TimeGuard): Duration => Duration.between(from, to),\r\n fromMilliseconds: (ms: number): Duration => Duration.fromMilliseconds(ms),\r\n };\r\n }\r\n}\r\n\r\n/**\r\n * Create and export default instance\r\n */\r\nexport const durationPlugin = new DurationPlugin();\r\n\r\nexport default durationPlugin;\r\n"],"mappings":"+FAaA,IAAa,EAAb,MAAa,CAA8B,CACzC,MAAwB,EACxB,OAAyB,EACzB,MAAwB,EACxB,KAAuB,EACvB,MAAwB,EACxB,QAA0B,EAC1B,QAA0B,EAC1B,aAA+B,EAE/B,YAAY,EAAsB,CAChC,KAAK,MAAQ,EAAM,OAAS,EAC5B,KAAK,OAAS,EAAM,QAAU,EAC9B,KAAK,MAAQ,EAAM,OAAS,EAC5B,KAAK,KAAO,EAAM,MAAQ,EAC1B,KAAK,MAAQ,EAAM,OAAS,EAC5B,KAAK,QAAU,EAAM,SAAW,EAChC,KAAK,QAAU,EAAM,SAAW,EAChC,KAAK,aAAe,EAAM,cAAgB,EAO5C,OAAO,QAAQ,EAAuB,CAEpC,IAAM,EAAQ,EAAI,MADD,gGACgB,CAEjC,GAAI,CAAC,EACH,MAAU,MAAM,8BAA8B,IAAM,CAGtD,GAAM,EAAG,EAAU,EAAO,GAAU,EAAM,EAAO,EAAS,GAAW,EAC/D,EAAa,EAAW,GAAK,EAEnC,OAAO,IAAI,EAAS,CAClB,MAAO,SAAS,GAAS,IAAK,GAAG,CAAG,EACpC,OAAQ,SAAS,GAAU,IAAK,GAAG,CAAG,EACtC,KAAM,SAAS,GAAQ,IAAK,GAAG,CAAG,EAClC,MAAO,SAAS,GAAS,IAAK,GAAG,CAAG,EACpC,QAAS,SAAS,GAAW,IAAK,GAAG,CAAG,EACxC,QAAS,WAAW,GAAW,IAAI,CAAG,EACvC,CAAC,CAMJ,OAAO,QAAQ,EAAiB,EAAyB,CAEvD,IAAM,EAAU,EAAa,YAAY,CAEnC,EADQ,EAAW,YAAY,CACN,MAAM,EAAO,CAE5C,OAAO,IAAI,EAAS,CAClB,MAAO,EAAS,OAAS,EACzB,OAAQ,EAAS,QAAU,EAC3B,MAAO,EACP,KAAM,EAAS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MAAO,KAAK,IAAI,KAAK,MAAM,CAC3B,KAAM,KAAK,IAAI,KAAK,KAAK,CACzB,MAAO,KAAK,IAAI,KAAK,MAAM,CAC3B,QAAS,KAAK,IAAI,KAAK,QAAQ,CAC/B,QAAS,KAAK,IAAI,KAAK,QAAQ,CAC/B,aAAc,KAAK,IAAI,KAAK,aAAa,CAC1C,CAAC,CAMJ,UAAmB,CACjB,OAAO,KAAK,OAAO,GAOV,EAAb,KAAwD,CACtD,KAAO,WACP,QAAU,QAKV,QAAQ,EAAwC,CAI7C,EAAe,UAAkB,SAAW,SAAU,EAA4B,CACjF,OAAO,EAAS,QAAQ,KAAM,EAAM,EAMrC,EAAuB,SAAW,EAKlC,EAAuB,SAAW,CACjC,QAAU,GAA0B,EAAS,QAAQ,EAAI,CACzD,SAAU,EAAiB,IAA4B,EAAS,QAAQ,EAAM,EAAG,CACjF,iBAAmB,GAAyB,EAAS,iBAAiB,EAAG,CAC1E,GAOQ,EAAiB,IAAI"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/*! time-guard v2.5.
|
|
1
|
+
/*! time-guard v2.5.4 | (c) 2026 Berea-Soft | MIT License | https://github.com/Berea-Soft/time-guard */
|
|
2
2
|
//#region src/plugins/duration/index.ts
|
|
3
3
|
var e = class e {
|
|
4
4
|
years = 0;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"duration.es.js","names":[],"sources":["../../src/plugins/duration/index.ts"],"sourcesContent":["/**\r\n * TimeGuard Duration Plugin\r\n * Implements ISO 8601 duration support and time calculations\r\n * Follows SOLID principles and enables advanced duration operations\r\n */\r\n\r\nimport type { ITimeGuardPlugin } from '../../types';\r\nimport type { TimeGuard } from '../../time-guard';\r\nimport type { DurationInput, DurationObject, DurationUnit, IDuration } from './types';\r\n\r\n/**\r\n * Duration class - represents time span following ISO 8601 standard\r\n */\r\nexport class Duration implements IDuration {\r\n private years: number = 0;\r\n private months: number = 0;\r\n private weeks: number = 0;\r\n private days: number = 0;\r\n private hours: number = 0;\r\n private minutes: number = 0;\r\n private seconds: number = 0;\r\n private milliseconds: number = 0;\r\n\r\n constructor(input: DurationInput) {\r\n this.years = input.years || 0;\r\n this.months = input.months || 0;\r\n this.weeks = input.weeks || 0;\r\n this.days = input.days || 0;\r\n this.hours = input.hours || 0;\r\n this.minutes = input.minutes || 0;\r\n this.seconds = input.seconds || 0;\r\n this.milliseconds = input.milliseconds || 0;\r\n }\r\n\r\n /**\r\n * Create Duration from ISO 8601 string\r\n * @example Duration.fromISO(\"P3Y6M4DT12H30M5S\")\r\n */\r\n static fromISO(iso: string): Duration {\r\n const isoRegex = /^(-)?P(?:(\\d+)Y)?(?:(\\d+)M)?(?:(\\d+)W)?(?:(\\d+)D)?(?:T(?:(\\d+)H)?(?:(\\d+)M)?(?:([\\d.]+)S)?)?$/;\r\n const match = iso.match(isoRegex);\r\n\r\n if (!match) {\r\n throw new Error(`Invalid ISO 8601 duration: ${iso}`);\r\n }\r\n\r\n const [, negative, years, months, , days, hours, minutes, seconds] = match;\r\n const multiplier = negative ? -1 : 1;\r\n\r\n return new Duration({\r\n years: parseInt(years || '0', 10) * multiplier,\r\n months: parseInt(months || '0', 10) * multiplier,\r\n days: parseInt(days || '0', 10) * multiplier,\r\n hours: parseInt(hours || '0', 10) * multiplier,\r\n minutes: parseInt(minutes || '0', 10) * multiplier,\r\n seconds: parseFloat(seconds || '0') * multiplier,\r\n });\r\n }\r\n\r\n /**\r\n * Create a Duration between this date and another\r\n */\r\n static between(from: TimeGuard, to: TimeGuard): Duration {\r\n // Calculate (to - from) using since() for proper duration\r\n const fromDT = (from as any).toTemporal();\r\n const toDT = (to as any).toTemporal();\r\n const duration = (toDT as any).since(fromDT);\r\n \r\n return new Duration({\r\n years: duration.years || 0,\r\n months: duration.months || 0,\r\n weeks: 0,\r\n days: duration.days || 0,\r\n hours: duration.hours || 0,\r\n minutes: duration.minutes || 0,\r\n seconds: duration.seconds || 0,\r\n milliseconds: duration.milliseconds || 0,\r\n });\r\n }\r\n\r\n /**\r\n * Create Duration from milliseconds\r\n */\r\n static fromMilliseconds(ms: number): Duration {\r\n const isNegative = ms < 0;\r\n const absMilhs = Math.abs(ms);\r\n\r\n const years = Math.floor(absMilhs / (1000 * 60 * 60 * 24 * 365));\r\n const months = Math.floor((absMilhs % (1000 * 60 * 60 * 24 * 365)) / (1000 * 60 * 60 * 24 * 30));\r\n const days = Math.floor((absMilhs % (1000 * 60 * 60 * 24 * 30)) / (1000 * 60 * 60 * 24));\r\n const hours = Math.floor((absMilhs % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));\r\n const minutes = Math.floor((absMilhs % (1000 * 60 * 60)) / (1000 * 60));\r\n const seconds = Math.floor((absMilhs % (1000 * 60)) / 1000);\r\n const milliseconds = Math.floor(absMilhs % 1000);\r\n\r\n const multiplier = isNegative ? -1 : 1;\r\n\r\n return new Duration({\r\n years: years * multiplier,\r\n months: months * multiplier,\r\n days: days * multiplier,\r\n hours: hours * multiplier,\r\n minutes: minutes * multiplier,\r\n seconds: seconds * multiplier,\r\n milliseconds: milliseconds * multiplier,\r\n });\r\n }\r\n\r\n /**\r\n * Get duration in specific unit\r\n */\r\n as(unit: DurationUnit): number {\r\n const ms = this.asMilliseconds();\r\n\r\n const conversions: Record<DurationUnit, number> = {\r\n milliseconds: 1,\r\n seconds: 1000,\r\n minutes: 1000 * 60,\r\n hours: 1000 * 60 * 60,\r\n days: 1000 * 60 * 60 * 24,\r\n weeks: 1000 * 60 * 60 * 24 * 7,\r\n months: 1000 * 60 * 60 * 24 * 30,\r\n years: 1000 * 60 * 60 * 24 * 365,\r\n };\r\n\r\n return ms / conversions[unit];\r\n }\r\n\r\n /**\r\n * Get duration as milliseconds\r\n */\r\n asMilliseconds(): number {\r\n return (\r\n this.years * 1000 * 60 * 60 * 24 * 365 +\r\n this.months * 1000 * 60 * 60 * 24 * 30 +\r\n this.weeks * 1000 * 60 * 60 * 24 * 7 +\r\n this.days * 1000 * 60 * 60 * 24 +\r\n this.hours * 1000 * 60 * 60 +\r\n this.minutes * 1000 * 60 +\r\n this.seconds * 1000 +\r\n this.milliseconds\r\n );\r\n }\r\n\r\n /**\r\n * Get duration as seconds\r\n */\r\n asSeconds(): number {\r\n return this.asMilliseconds() / 1000;\r\n }\r\n\r\n /**\r\n * Get duration as minutes\r\n */\r\n asMinutes(): number {\r\n return this.asSeconds() / 60;\r\n }\r\n\r\n /**\r\n * Get duration as hours\r\n */\r\n asHours(): number {\r\n return this.asMinutes() / 60;\r\n }\r\n\r\n /**\r\n * Get duration as days\r\n */\r\n asDays(): number {\r\n return this.asHours() / 24;\r\n }\r\n\r\n /**\r\n * Get duration as weeks\r\n */\r\n asWeeks(): number {\r\n return this.asDays() / 7;\r\n }\r\n\r\n /**\r\n * Get duration as months\r\n */\r\n asMonths(): number {\r\n return this.asDays() / 30;\r\n }\r\n\r\n /**\r\n * Get duration as years\r\n */\r\n asYears(): number {\r\n return this.asDays() / 365;\r\n }\r\n\r\n /**\r\n * Get all components\r\n */\r\n toObject(): DurationObject {\r\n return {\r\n years: this.years,\r\n months: this.months,\r\n weeks: this.weeks,\r\n days: this.days,\r\n hours: this.hours,\r\n minutes: this.minutes,\r\n seconds: this.seconds,\r\n milliseconds: this.milliseconds,\r\n };\r\n }\r\n\r\n /**\r\n * Get ISO 8601 string representation\r\n * @example \"P3Y6M4DT12H30M5S\"\r\n */\r\n toISO(): string {\r\n\r\n // Date part\r\n let datePart = '';\r\n if (this.years) datePart += `${this.years}Y`;\r\n if (this.months) datePart += `${this.months}M`;\r\n if (this.weeks || this.days) datePart += `${this.weeks * 7 + this.days}D`;\r\n\r\n // Time part\r\n let timePart = '';\r\n if (this.hours) timePart += `${this.hours}H`;\r\n if (this.minutes) timePart += `${this.minutes}M`;\r\n if (this.seconds || this.milliseconds) {\r\n timePart += `${this.seconds + this.milliseconds / 1000}S`;\r\n }\r\n\r\n const sign = this.isNegative() ? '-' : '';\r\n return `${sign}P${datePart}${timePart ? `T${timePart}` : ''}`;\r\n }\r\n\r\n /**\r\n * Get human-readable string\r\n */\r\n humanize(): string {\r\n const parts: string[] = [];\r\n\r\n if (this.years) parts.push(`${Math.abs(this.years)} year${Math.abs(this.years) !== 1 ? 's' : ''}`);\r\n if (this.months) parts.push(`${Math.abs(this.months)} month${Math.abs(this.months) !== 1 ? 's' : ''}`);\r\n if (this.weeks) parts.push(`${Math.abs(this.weeks)} week${Math.abs(this.weeks) !== 1 ? 's' : ''}`);\r\n if (this.days) parts.push(`${Math.abs(this.days)} day${Math.abs(this.days) !== 1 ? 's' : ''}`);\r\n if (this.hours) parts.push(`${Math.abs(this.hours)} hour${Math.abs(this.hours) !== 1 ? 's' : ''}`);\r\n if (this.minutes) parts.push(`${Math.abs(this.minutes)} minute${Math.abs(this.minutes) !== 1 ? 's' : ''}`);\r\n if (this.seconds) parts.push(`${Math.abs(this.seconds)} second${Math.abs(this.seconds) !== 1 ? 's' : ''}`);\r\n if (this.milliseconds) parts.push(`${Math.abs(this.milliseconds)} ms`);\r\n\r\n if (parts.length === 0) return '0 seconds';\r\n\r\n const text = parts.join(', ');\r\n return this.isNegative() ? `-${text}` : text;\r\n }\r\n\r\n /**\r\n * Check if duration is negative\r\n */\r\n isNegative(): boolean {\r\n return (\r\n this.years < 0 ||\r\n this.months < 0 ||\r\n this.weeks < 0 ||\r\n this.days < 0 ||\r\n this.hours < 0 ||\r\n this.minutes < 0 ||\r\n this.seconds < 0 ||\r\n this.milliseconds < 0\r\n );\r\n }\r\n\r\n /**\r\n * Get absolute duration\r\n */\r\n abs(): Duration {\r\n return new Duration({\r\n years: Math.abs(this.years),\r\n months: Math.abs(this.months),\r\n weeks: Math.abs(this.weeks),\r\n days: Math.abs(this.days),\r\n hours: Math.abs(this.hours),\r\n minutes: Math.abs(this.minutes),\r\n seconds: Math.abs(this.seconds),\r\n milliseconds: Math.abs(this.milliseconds),\r\n });\r\n }\r\n\r\n /**\r\n * String representation\r\n */\r\n toString(): string {\r\n return this.toISO();\r\n }\r\n}\r\n\r\n/**\r\n * Duration Plugin\r\n */\r\nexport class DurationPlugin implements ITimeGuardPlugin {\r\n name = 'duration';\r\n version = '1.0.0';\r\n\r\n /**\r\n * Install plugin into TimeGuard\r\n */\r\n install(TimeGuardClass: typeof TimeGuard): void {\r\n /**\r\n * Create a Duration between this date and another\r\n */\r\n (TimeGuardClass.prototype as any).duration = function (other: TimeGuard): Duration {\r\n return Duration.between(this, other);\r\n };\r\n\r\n /**\r\n * Get Duration class for creating custom durations\r\n */\r\n (TimeGuardClass as any).Duration = Duration;\r\n\r\n /**\r\n * Create Duration from ISO 8601 string\r\n */\r\n (TimeGuardClass as any).duration = {\r\n fromISO: (iso: string): Duration => Duration.fromISO(iso),\r\n between: (from: TimeGuard, to: TimeGuard): Duration => Duration.between(from, to),\r\n fromMilliseconds: (ms: number): Duration => Duration.fromMilliseconds(ms),\r\n };\r\n }\r\n}\r\n\r\n/**\r\n * Create and export default instance\r\n */\r\nexport const durationPlugin = new DurationPlugin();\r\n\r\nexport default durationPlugin;\r\n"],"mappings":";;AAaA,IAAa,IAAb,MAAa,EAA8B;CACzC,QAAwB;CACxB,SAAyB;CACzB,QAAwB;CACxB,OAAuB;CACvB,QAAwB;CACxB,UAA0B;CAC1B,UAA0B;CAC1B,eAA+B;CAE/B,YAAY,GAAsB;AAQhC,EAPA,KAAK,QAAQ,EAAM,SAAS,GAC5B,KAAK,SAAS,EAAM,UAAU,GAC9B,KAAK,QAAQ,EAAM,SAAS,GAC5B,KAAK,OAAO,EAAM,QAAQ,GAC1B,KAAK,QAAQ,EAAM,SAAS,GAC5B,KAAK,UAAU,EAAM,WAAW,GAChC,KAAK,UAAU,EAAM,WAAW,GAChC,KAAK,eAAe,EAAM,gBAAgB;;CAO5C,OAAO,QAAQ,GAAuB;EAEpC,IAAM,IAAQ,EAAI,MADD,gGACgB;AAEjC,MAAI,CAAC,EACH,OAAU,MAAM,8BAA8B,IAAM;EAGtD,IAAM,GAAG,GAAU,GAAO,KAAU,GAAM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js","names":[],"sources":["../../src/plugins/duration/index.ts"],"sourcesContent":["/**\r\n * TimeGuard Duration Plugin\r\n * Implements ISO 8601 duration support and time calculations\r\n * Follows SOLID principles and enables advanced duration operations\r\n */\r\n\r\nimport type { ITimeGuardPlugin } from '../../types';\r\nimport type { TimeGuard } from '../../index';\r\nimport type { DurationInput, DurationObject, DurationUnit, IDuration } from './types';\r\n\r\n/**\r\n * Duration class - represents time span following ISO 8601 standard\r\n */\r\nexport class Duration implements IDuration {\r\n private years: number = 0;\r\n private months: number = 0;\r\n private weeks: number = 0;\r\n private days: number = 0;\r\n private hours: number = 0;\r\n private minutes: number = 0;\r\n private seconds: number = 0;\r\n private milliseconds: number = 0;\r\n\r\n constructor(input: DurationInput) {\r\n this.years = input.years || 0;\r\n this.months = input.months || 0;\r\n this.weeks = input.weeks || 0;\r\n this.days = input.days || 0;\r\n this.hours = input.hours || 0;\r\n this.minutes = input.minutes || 0;\r\n this.seconds = input.seconds || 0;\r\n this.milliseconds = input.milliseconds || 0;\r\n }\r\n\r\n /**\r\n * Create Duration from ISO 8601 string\r\n * @example Duration.fromISO(\"P3Y6M4DT12H30M5S\")\r\n */\r\n static fromISO(iso: string): Duration {\r\n const isoRegex = /^(-)?P(?:(\\d+)Y)?(?:(\\d+)M)?(?:(\\d+)W)?(?:(\\d+)D)?(?:T(?:(\\d+)H)?(?:(\\d+)M)?(?:([\\d.]+)S)?)?$/;\r\n const match = iso.match(isoRegex);\r\n\r\n if (!match) {\r\n throw new Error(`Invalid ISO 8601 duration: ${iso}`);\r\n }\r\n\r\n const [, negative, years, months, , days, hours, minutes, seconds] = match;\r\n const multiplier = negative ? -1 : 1;\r\n\r\n return new Duration({\r\n years: parseInt(years || '0', 10) * multiplier,\r\n months: parseInt(months || '0', 10) * multiplier,\r\n days: parseInt(days || '0', 10) * multiplier,\r\n hours: parseInt(hours || '0', 10) * multiplier,\r\n minutes: parseInt(minutes || '0', 10) * multiplier,\r\n seconds: parseFloat(seconds || '0') * multiplier,\r\n });\r\n }\r\n\r\n /**\r\n * Create a Duration between this date and another\r\n */\r\n static between(from: TimeGuard, to: TimeGuard): Duration {\r\n // Calculate (to - from) using since() for proper duration\r\n const fromDT = (from as any).toTemporal();\r\n const toDT = (to as any).toTemporal();\r\n const duration = (toDT as any).since(fromDT);\r\n \r\n return new Duration({\r\n years: duration.years || 0,\r\n months: duration.months || 0,\r\n weeks: 0,\r\n days: duration.days || 0,\r\n hours: duration.hours || 0,\r\n minutes: duration.minutes || 0,\r\n seconds: duration.seconds || 0,\r\n milliseconds: duration.milliseconds || 0,\r\n });\r\n }\r\n\r\n /**\r\n * Create Duration from milliseconds\r\n */\r\n static fromMilliseconds(ms: number): Duration {\r\n const isNegative = ms < 0;\r\n const absMilhs = Math.abs(ms);\r\n\r\n const years = Math.floor(absMilhs / (1000 * 60 * 60 * 24 * 365));\r\n const months = Math.floor((absMilhs % (1000 * 60 * 60 * 24 * 365)) / (1000 * 60 * 60 * 24 * 30));\r\n const days = Math.floor((absMilhs % (1000 * 60 * 60 * 24 * 30)) / (1000 * 60 * 60 * 24));\r\n const hours = Math.floor((absMilhs % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));\r\n const minutes = Math.floor((absMilhs % (1000 * 60 * 60)) / (1000 * 60));\r\n const seconds = Math.floor((absMilhs % (1000 * 60)) / 1000);\r\n const milliseconds = Math.floor(absMilhs % 1000);\r\n\r\n const multiplier = isNegative ? -1 : 1;\r\n\r\n return new Duration({\r\n years: years * multiplier,\r\n months: months * multiplier,\r\n days: days * multiplier,\r\n hours: hours * multiplier,\r\n minutes: minutes * multiplier,\r\n seconds: seconds * multiplier,\r\n milliseconds: milliseconds * multiplier,\r\n });\r\n }\r\n\r\n /**\r\n * Get duration in specific unit\r\n */\r\n as(unit: DurationUnit): number {\r\n const ms = this.asMilliseconds();\r\n\r\n const conversions: Record<DurationUnit, number> = {\r\n milliseconds: 1,\r\n seconds: 1000,\r\n minutes: 1000 * 60,\r\n hours: 1000 * 60 * 60,\r\n days: 1000 * 60 * 60 * 24,\r\n weeks: 1000 * 60 * 60 * 24 * 7,\r\n months: 1000 * 60 * 60 * 24 * 30,\r\n years: 1000 * 60 * 60 * 24 * 365,\r\n };\r\n\r\n return ms / conversions[unit];\r\n }\r\n\r\n /**\r\n * Get duration as milliseconds\r\n */\r\n asMilliseconds(): number {\r\n return (\r\n this.years * 1000 * 60 * 60 * 24 * 365 +\r\n this.months * 1000 * 60 * 60 * 24 * 30 +\r\n this.weeks * 1000 * 60 * 60 * 24 * 7 +\r\n this.days * 1000 * 60 * 60 * 24 +\r\n this.hours * 1000 * 60 * 60 +\r\n this.minutes * 1000 * 60 +\r\n this.seconds * 1000 +\r\n this.milliseconds\r\n );\r\n }\r\n\r\n /**\r\n * Get duration as seconds\r\n */\r\n asSeconds(): number {\r\n return this.asMilliseconds() / 1000;\r\n }\r\n\r\n /**\r\n * Get duration as minutes\r\n */\r\n asMinutes(): number {\r\n return this.asSeconds() / 60;\r\n }\r\n\r\n /**\r\n * Get duration as hours\r\n */\r\n asHours(): number {\r\n return this.asMinutes() / 60;\r\n }\r\n\r\n /**\r\n * Get duration as days\r\n */\r\n asDays(): number {\r\n return this.asHours() / 24;\r\n }\r\n\r\n /**\r\n * Get duration as weeks\r\n */\r\n asWeeks(): number {\r\n return this.asDays() / 7;\r\n }\r\n\r\n /**\r\n * Get duration as months\r\n */\r\n asMonths(): number {\r\n return this.asDays() / 30;\r\n }\r\n\r\n /**\r\n * Get duration as years\r\n */\r\n asYears(): number {\r\n return this.asDays() / 365;\r\n }\r\n\r\n /**\r\n * Get all components\r\n */\r\n toObject(): DurationObject {\r\n return {\r\n years: this.years,\r\n months: this.months,\r\n weeks: this.weeks,\r\n days: this.days,\r\n hours: this.hours,\r\n minutes: this.minutes,\r\n seconds: this.seconds,\r\n milliseconds: this.milliseconds,\r\n };\r\n }\r\n\r\n /**\r\n * Get ISO 8601 string representation\r\n * @example \"P3Y6M4DT12H30M5S\"\r\n */\r\n toISO(): string {\r\n\r\n // Date part\r\n let datePart = '';\r\n if (this.years) datePart += `${this.years}Y`;\r\n if (this.months) datePart += `${this.months}M`;\r\n if (this.weeks || this.days) datePart += `${this.weeks * 7 + this.days}D`;\r\n\r\n // Time part\r\n let timePart = '';\r\n if (this.hours) timePart += `${this.hours}H`;\r\n if (this.minutes) timePart += `${this.minutes}M`;\r\n if (this.seconds || this.milliseconds) {\r\n timePart += `${this.seconds + this.milliseconds / 1000}S`;\r\n }\r\n\r\n const sign = this.isNegative() ? '-' : '';\r\n return `${sign}P${datePart}${timePart ? `T${timePart}` : ''}`;\r\n }\r\n\r\n /**\r\n * Get human-readable string\r\n */\r\n humanize(): string {\r\n const parts: string[] = [];\r\n\r\n if (this.years) parts.push(`${Math.abs(this.years)} year${Math.abs(this.years) !== 1 ? 's' : ''}`);\r\n if (this.months) parts.push(`${Math.abs(this.months)} month${Math.abs(this.months) !== 1 ? 's' : ''}`);\r\n if (this.weeks) parts.push(`${Math.abs(this.weeks)} week${Math.abs(this.weeks) !== 1 ? 's' : ''}`);\r\n if (this.days) parts.push(`${Math.abs(this.days)} day${Math.abs(this.days) !== 1 ? 's' : ''}`);\r\n if (this.hours) parts.push(`${Math.abs(this.hours)} hour${Math.abs(this.hours) !== 1 ? 's' : ''}`);\r\n if (this.minutes) parts.push(`${Math.abs(this.minutes)} minute${Math.abs(this.minutes) !== 1 ? 's' : ''}`);\r\n if (this.seconds) parts.push(`${Math.abs(this.seconds)} second${Math.abs(this.seconds) !== 1 ? 's' : ''}`);\r\n if (this.milliseconds) parts.push(`${Math.abs(this.milliseconds)} ms`);\r\n\r\n if (parts.length === 0) return '0 seconds';\r\n\r\n const text = parts.join(', ');\r\n return this.isNegative() ? `-${text}` : text;\r\n }\r\n\r\n /**\r\n * Check if duration is negative\r\n */\r\n isNegative(): boolean {\r\n return (\r\n this.years < 0 ||\r\n this.months < 0 ||\r\n this.weeks < 0 ||\r\n this.days < 0 ||\r\n this.hours < 0 ||\r\n this.minutes < 0 ||\r\n this.seconds < 0 ||\r\n this.milliseconds < 0\r\n );\r\n }\r\n\r\n /**\r\n * Get absolute duration\r\n */\r\n abs(): Duration {\r\n return new Duration({\r\n years: Math.abs(this.years),\r\n months: Math.abs(this.months),\r\n weeks: Math.abs(this.weeks),\r\n days: Math.abs(this.days),\r\n hours: Math.abs(this.hours),\r\n minutes: Math.abs(this.minutes),\r\n seconds: Math.abs(this.seconds),\r\n milliseconds: Math.abs(this.milliseconds),\r\n });\r\n }\r\n\r\n /**\r\n * String representation\r\n */\r\n toString(): string {\r\n return this.toISO();\r\n }\r\n}\r\n\r\n/**\r\n * Duration Plugin\r\n */\r\nexport class DurationPlugin implements ITimeGuardPlugin {\r\n name = 'duration';\r\n version = '1.0.0';\r\n\r\n /**\r\n * Install plugin into TimeGuard\r\n */\r\n install(TimeGuardClass: typeof TimeGuard): void {\r\n /**\r\n * Create a Duration between this date and another\r\n */\r\n (TimeGuardClass.prototype as any).duration = function (other: TimeGuard): Duration {\r\n return Duration.between(this, other);\r\n };\r\n\r\n /**\r\n * Get Duration class for creating custom durations\r\n */\r\n (TimeGuardClass as any).Duration = Duration;\r\n\r\n /**\r\n * Create Duration from ISO 8601 string\r\n */\r\n (TimeGuardClass as any).duration = {\r\n fromISO: (iso: string): Duration => Duration.fromISO(iso),\r\n between: (from: TimeGuard, to: TimeGuard): Duration => Duration.between(from, to),\r\n fromMilliseconds: (ms: number): Duration => Duration.fromMilliseconds(ms),\r\n };\r\n }\r\n}\r\n\r\n/**\r\n * Create and export default instance\r\n */\r\nexport const durationPlugin = new DurationPlugin();\r\n\r\nexport default durationPlugin;\r\n"],"mappings":";;AAaA,IAAa,IAAb,MAAa,EAA8B;CACzC,QAAwB;CACxB,SAAyB;CACzB,QAAwB;CACxB,OAAuB;CACvB,QAAwB;CACxB,UAA0B;CAC1B,UAA0B;CAC1B,eAA+B;CAE/B,YAAY,GAAsB;AAQhC,EAPA,KAAK,QAAQ,EAAM,SAAS,GAC5B,KAAK,SAAS,EAA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O;CACP,UAAU;CAKV,QAAQ,GAAwC;AAgB7C,EAZA,EAAe,UAAkB,WAAW,SAAU,GAA4B;AACjF,UAAO,EAAS,QAAQ,MAAM,EAAM;KAMrC,EAAuB,WAAW,GAKlC,EAAuB,WAAW;GACjC,UAAU,MAA0B,EAAS,QAAQ,EAAI;GACzD,UAAU,GAAiB,MAA4B,EAAS,QAAQ,GAAM,EAAG;GACjF,mBAAmB,MAAyB,EAAS,iBAAiB,EAAG;GAC1E;;GAOQ,IAAiB,IAAI,GAAgB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"relative-time.cjs","names":[],"sources":["../../src/plugins/relative-time/index.ts"],"sourcesContent":["/**\r\n * TimeGuard Relative Time Plugin\r\n * Adds human-readable time differences (e.g., \"2 hours ago\", \"in 3 days\")\r\n * Follows SOLID principles and Temporal API standards\r\n */\r\n\r\nimport type { ITimeGuardPlugin } from '../../types';\r\nimport type { TimeGuard } from '../../
|
|
1
|
+
{"version":3,"file":"relative-time.cjs","names":[],"sources":["../../src/plugins/relative-time/index.ts"],"sourcesContent":["/**\r\n * TimeGuard Relative Time Plugin\r\n * Adds human-readable time differences (e.g., \"2 hours ago\", \"in 3 days\")\r\n * Follows SOLID principles and Temporal API standards\r\n */\r\n\r\nimport type { ITimeGuardPlugin } from '../../types';\r\nimport type { TimeGuard } from '../../index';\r\nimport type { RelativeTimeConfig, RelativeTimeFormats, RelativeTimeThreshold } from './types';\r\n\r\nconst DEFAULT_THRESHOLDS: RelativeTimeThreshold[] = [\r\n { l: 's', r: 44, d: 'second' },\r\n { l: 'm', r: 89 },\r\n { l: 'mm', r: 44, d: 'minute' },\r\n { l: 'h', r: 89 },\r\n { l: 'hh', r: 21, d: 'hour' },\r\n { l: 'd', r: 35 },\r\n { l: 'dd', r: 25, d: 'day' },\r\n { l: 'M', r: 45 },\r\n { l: 'MM', r: 10, d: 'month' },\r\n { l: 'y', r: 17 },\r\n { l: 'yy', d: 'year' },\r\n];\r\n\r\nconst DEFAULT_FORMATS: RelativeTimeFormats = {\r\n future: 'in %s',\r\n past: '%s ago',\r\n s: 'a few seconds',\r\n m: 'a minute',\r\n mm: '%d minutes',\r\n h: 'an hour',\r\n hh: '%d hours',\r\n d: 'a day',\r\n dd: '%d days',\r\n M: 'a month',\r\n MM: '%d months',\r\n y: 'a year',\r\n yy: '%d years',\r\n};\r\n\r\nexport class RelativeTimePlugin implements ITimeGuardPlugin {\r\n name = 'relative-time';\r\n version = '1.0.0';\r\n \r\n private config: RelativeTimeConfig;\r\n private formats: RelativeTimeFormats;\r\n\r\n constructor(config?: RelativeTimeConfig) {\r\n this.config = {\r\n thresholds: config?.thresholds || DEFAULT_THRESHOLDS,\r\n rounding: config?.rounding || Math.round,\r\n };\r\n this.formats = DEFAULT_FORMATS;\r\n }\r\n\r\n /**\r\n * Install plugin into TimeGuard\r\n */\r\n install(TimeGuardClass: typeof TimeGuard): void {\r\n const plugin = this;\r\n\r\n /**\r\n * Get relative time string (e.g., \"2 hours ago\")\r\n */\r\n (TimeGuardClass.prototype as any).fromNow = function (withoutSuffix?: boolean): string {\r\n return plugin.formatRelativeTime(this, false, withoutSuffix);\r\n };\r\n\r\n /**\r\n * Get future relative time string (e.g., \"in 3 days\")\r\n */\r\n (TimeGuardClass.prototype as any).toNow = function (withoutSuffix?: boolean): string {\r\n return plugin.formatRelativeTime(this, true, withoutSuffix);\r\n };\r\n\r\n /**\r\n * Get human-readable duration\r\n */\r\n (TimeGuardClass.prototype as any).humanize = function (\r\n other?: TimeGuard,\r\n withoutSuffix?: boolean,\r\n ): string {\r\n if (other) {\r\n return plugin.formatRelativeTime(this, other.isAfter(this), withoutSuffix);\r\n }\r\n return plugin.formatRelativeTime(this, false, withoutSuffix);\r\n };\r\n }\r\n\r\n /**\r\n * Format relative time\r\n */\r\n private formatRelativeTime(\r\n date: TimeGuard,\r\n isFuture: boolean,\r\n withoutSuffix?: boolean,\r\n ): string {\r\n const now = (date.constructor as typeof TimeGuard).now();\r\n let diff = now.diff(date, 'millisecond');\r\n const absDiff = Math.abs(diff);\r\n const isFromNow = diff > 0;\r\n const actualIsFuture = isFuture ?? !isFromNow;\r\n\r\n let result = this.getRelativeTimeString(absDiff);\r\n\r\n if (withoutSuffix) {\r\n return result;\r\n }\r\n\r\n const suffix = actualIsFuture ? this.formats.future : this.formats.past;\r\n return suffix.replace('%s', result);\r\n }\r\n\r\n /**\r\n * Get relative time string based on milliseconds\r\n */\r\n private getRelativeTimeString(milliseconds: number): string {\r\n const thresholds = this.config.thresholds || DEFAULT_THRESHOLDS;\r\n const rounding = this.config.rounding || Math.round;\r\n\r\n for (let i = 0; i < thresholds.length; i++) {\r\n const threshold = thresholds[i];\r\n const nextThreshold = i + 1 < thresholds.length ? thresholds[i + 1] : null;\r\n\r\n // Skip if we haven't reached this threshold\r\n if (nextThreshold && threshold.r && milliseconds < threshold.r * 1000) {\r\n continue;\r\n }\r\n\r\n // Convert to appropriate unit\r\n let value: number;\r\n if (threshold.d) {\r\n const unitMs = this.getUnitMilliseconds(threshold.d);\r\n value = rounding(milliseconds / unitMs);\r\n } else {\r\n value = 1;\r\n }\r\n\r\n const label = threshold.l;\r\n const format = (this.formats as any)[label] || label;\r\n\r\n if (typeof format === 'string') {\r\n return format.includes('%d')\r\n ? format.replace('%d', String(value))\r\n : format;\r\n }\r\n\r\n return format;\r\n }\r\n\r\n // Fallback\r\n return `${rounding(milliseconds / 1000)} seconds`;\r\n }\r\n\r\n /**\r\n * Get milliseconds per unit\r\n */\r\n private getUnitMilliseconds(unit: string): number {\r\n const msMap: Record<string, number> = {\r\n second: 1000,\r\n minute: 1000 * 60,\r\n hour: 1000 * 60 * 60,\r\n day: 1000 * 60 * 60 * 24,\r\n month: 1000 * 60 * 60 * 24 * 30,\r\n year: 1000 * 60 * 60 * 24 * 365,\r\n };\r\n return msMap[unit] || 1;\r\n }\r\n\r\n /**\r\n * Set format strings\r\n */\r\n setFormats(formats: Partial<RelativeTimeFormats>): void {\r\n Object.assign(this.formats, formats);\r\n }\r\n\r\n /**\r\n * Get current formats\r\n */\r\n getFormats(): RelativeTimeFormats {\r\n return { ...this.formats };\r\n }\r\n}\r\n\r\n/**\r\n * Create and export default instance\r\n */\r\nexport default new RelativeTimePlugin();\r\n"],"mappings":"+FAUA,IAAM,EAA8C,CAClD,CAAE,EAAG,IAAK,EAAG,GAAI,EAAG,SAAU,CAC9B,CAAE,EAAG,IAAK,EAAG,GAAI,CACjB,CAAE,EAAG,KAAM,EAAG,GAAI,EAAG,SAAU,CAC/B,CAAE,EAAG,IAAK,EAAG,GAAI,CACjB,CAAE,EAAG,KAAM,EAAG,GAAI,EAAG,OAAQ,CAC7B,CAAE,EAAG,IAAK,EAAG,GAAI,CACjB,CAAE,EAAG,KAAM,EAAG,GAAI,EAAG,MAAO,CAC5B,CAAE,EAAG,IAAK,EAAG,GAAI,CACjB,CAAE,EAAG,KAAM,EAAG,GAAI,EAAG,QAAS,CAC9B,CAAE,EAAG,IAAK,EAAG,GAAI,CACjB,CAAE,EAAG,KAAM,EAAG,OAAQ,CACvB,CAEK,EAAuC,CAC3C,OAAQ,QACR,KAAM,SACN,EAAG,gBACH,EAAG,WACH,GAAI,aACJ,EAAG,UACH,GAAI,WACJ,EAAG,QACH,GAAI,UACJ,EAAG,UACH,GAAI,YACJ,EAAG,SACH,GAAI,WACL,CAEY,EAAb,KAA4D,CAC1D,KAAO,gBACP,QAAU,QAEV,OACA,QAEA,YAAY,EAA6B,CACvC,KAAK,OAAS,CACZ,WAAY,GAAQ,YAAc,EAClC,SAAU,GAAQ,UAAY,KAAK,MACpC,CACD,KAAK,QAAU,EAMjB,QAAQ,EAAwC,CAC9C,IAAM,EAAS,KAKd,EAAe,UAAkB,QAAU,SAAU,EAAiC,CACrF,OAAO,EAAO,mBAAmB,KAAM,GAAO,EAAc,EAM7D,EAAe,UAAkB,MAAQ,SAAU,EAAiC,CACnF,OAAO,EAAO,mBAAmB,KAAM,GAAM,EAAc,EAM5D,EAAe,UAAkB,SAAW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tC,YAAkC,CAChC,MAAO,CAAE,GAAG,KAAK,QAAS,GAO9B,EAAe,IAAI"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/*! time-guard v2.5.
|
|
1
|
+
/*! time-guard v2.5.4 | (c) 2026 Berea-Soft | MIT License | https://github.com/Berea-Soft/time-guard */
|
|
2
2
|
//#region src/plugins/relative-time/index.ts
|
|
3
3
|
var e = [
|
|
4
4
|
{
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"relative-time.es.js","names":[],"sources":["../../src/plugins/relative-time/index.ts"],"sourcesContent":["/**\r\n * TimeGuard Relative Time Plugin\r\n * Adds human-readable time differences (e.g., \"2 hours ago\", \"in 3 days\")\r\n * Follows SOLID principles and Temporal API standards\r\n */\r\n\r\nimport type { ITimeGuardPlugin } from '../../types';\r\nimport type { TimeGuard } from '../../
|
|
1
|
+
{"version":3,"file":"relative-time.es.js","names":[],"sources":["../../src/plugins/relative-time/index.ts"],"sourcesContent":["/**\r\n * TimeGuard Relative Time Plugin\r\n * Adds human-readable time differences (e.g., \"2 hours ago\", \"in 3 days\")\r\n * Follows SOLID principles and Temporal API standards\r\n */\r\n\r\nimport type { ITimeGuardPlugin } from '../../types';\r\nimport type { TimeGuard } from '../../index';\r\nimport type { RelativeTimeConfig, RelativeTimeFormats, RelativeTimeThreshold } from './types';\r\n\r\nconst DEFAULT_THRESHOLDS: RelativeTimeThreshold[] = [\r\n { l: 's', r: 44, d: 'second' },\r\n { l: 'm', r: 89 },\r\n { l: 'mm', r: 44, d: 'minute' },\r\n { l: 'h', r: 89 },\r\n { l: 'hh', r: 21, d: 'hour' },\r\n { l: 'd', r: 35 },\r\n { l: 'dd', r: 25, d: 'day' },\r\n { l: 'M', r: 45 },\r\n { l: 'MM', r: 10, d: 'month' },\r\n { l: 'y', r: 17 },\r\n { l: 'yy', d: 'year' },\r\n];\r\n\r\nconst DEFAULT_FORMATS: RelativeTimeFormats = {\r\n future: 'in %s',\r\n past: '%s ago',\r\n s: 'a few seconds',\r\n m: 'a minute',\r\n mm: '%d minutes',\r\n h: 'an hour',\r\n hh: '%d hours',\r\n d: 'a day',\r\n dd: '%d days',\r\n M: 'a month',\r\n MM: '%d months',\r\n y: 'a year',\r\n yy: '%d years',\r\n};\r\n\r\nexport class RelativeTimePlugin implements ITimeGuardPlugin {\r\n name = 'relative-time';\r\n version = '1.0.0';\r\n \r\n private config: RelativeTimeConfig;\r\n private formats: RelativeTimeFormats;\r\n\r\n constructor(config?: RelativeTimeConfig) {\r\n this.config = {\r\n thresholds: config?.thresholds || DEFAULT_THRESHOLDS,\r\n rounding: config?.rounding || Math.round,\r\n };\r\n this.formats = DEFAULT_FORMATS;\r\n }\r\n\r\n /**\r\n * Install plugin into TimeGuard\r\n */\r\n install(TimeGuardClass: typeof TimeGuard): void {\r\n const plugin = this;\r\n\r\n /**\r\n * Get relative time string (e.g., \"2 hours ago\")\r\n */\r\n (TimeGuardClass.prototype as any).fromNow = function (withoutSuffix?: boolean): string {\r\n return plugin.formatRelativeTime(this, false, withoutSuffix);\r\n };\r\n\r\n /**\r\n * Get future relative time string (e.g., \"in 3 days\")\r\n */\r\n (TimeGuardClass.prototype as any).toNow = function (withoutSuffix?: boolean): string {\r\n return plugin.formatRelativeTime(this, true, withoutSuffix);\r\n };\r\n\r\n /**\r\n * Get human-readable duration\r\n */\r\n (TimeGuardClass.prototype as any).humanize = function (\r\n other?: TimeGuard,\r\n withoutSuffix?: boolean,\r\n ): string {\r\n if (other) {\r\n return plugin.formatRelativeTime(this, other.isAfter(this), withoutSuffix);\r\n }\r\n return plugin.formatRelativeTime(this, false, withoutSuffix);\r\n };\r\n }\r\n\r\n /**\r\n * Format relative time\r\n */\r\n private formatRelativeTime(\r\n date: TimeGuard,\r\n isFuture: boolean,\r\n withoutSuffix?: boolean,\r\n ): string {\r\n const now = (date.constructor as typeof TimeGuard).now();\r\n let diff = now.diff(date, 'millisecond');\r\n const absDiff = Math.abs(diff);\r\n const isFromNow = diff > 0;\r\n const actualIsFuture = isFuture ?? !isFromNow;\r\n\r\n let result = this.getRelativeTimeString(absDiff);\r\n\r\n if (withoutSuffix) {\r\n return result;\r\n }\r\n\r\n const suffix = actualIsFuture ? this.formats.future : this.formats.past;\r\n return suffix.replace('%s', result);\r\n }\r\n\r\n /**\r\n * Get relative time string based on milliseconds\r\n */\r\n private getRelativeTimeString(milliseconds: number): string {\r\n const thresholds = this.config.thresholds || DEFAULT_THRESHOLDS;\r\n const rounding = this.config.rounding || Math.round;\r\n\r\n for (let i = 0; i < thresholds.length; i++) {\r\n const threshold = thresholds[i];\r\n const nextThreshold = i + 1 < thresholds.length ? thresholds[i + 1] : null;\r\n\r\n // Skip if we haven't reached this threshold\r\n if (nextThreshold && threshold.r && milliseconds < threshold.r * 1000) {\r\n continue;\r\n }\r\n\r\n // Convert to appropriate unit\r\n let value: number;\r\n if (threshold.d) {\r\n const unitMs = this.getUnitMilliseconds(threshold.d);\r\n value = rounding(milliseconds / unitMs);\r\n } else {\r\n value = 1;\r\n }\r\n\r\n const label = threshold.l;\r\n const format = (this.formats as any)[label] || label;\r\n\r\n if (typeof format === 'string') {\r\n return format.includes('%d')\r\n ? format.replace('%d', String(value))\r\n : format;\r\n }\r\n\r\n return format;\r\n }\r\n\r\n // Fallback\r\n return `${rounding(milliseconds / 1000)} seconds`;\r\n }\r\n\r\n /**\r\n * Get milliseconds per unit\r\n */\r\n private getUnitMilliseconds(unit: string): number {\r\n const msMap: Record<string, number> = {\r\n second: 1000,\r\n minute: 1000 * 60,\r\n hour: 1000 * 60 * 60,\r\n day: 1000 * 60 * 60 * 24,\r\n month: 1000 * 60 * 60 * 24 * 30,\r\n year: 1000 * 60 * 60 * 24 * 365,\r\n };\r\n return msMap[unit] || 1;\r\n }\r\n\r\n /**\r\n * Set format strings\r\n */\r\n setFormats(formats: Partial<RelativeTimeFormats>): void {\r\n Object.assign(this.formats, formats);\r\n }\r\n\r\n /**\r\n * Get current formats\r\n */\r\n getFormats(): RelativeTimeFormats {\r\n return { ...this.formats };\r\n }\r\n}\r\n\r\n/**\r\n * Create and export default instance\r\n */\r\nexport default new RelativeTimePlugin();\r\n"],"mappings":";;AAUA,IAAM,IAA8C;CAClD;EAAE,GAAG;EAAK,GAAG;EAAI,GAAG;EAAU;CAC9B;EAAE,GAAG;EAAK,GAAG;EAAI;CACjB;EAAE,GAAG;EAAM,GAAG;EAAI,GAAG;EAAU;CAC/B;EAAE,GAAG;EAAK,GAAG;EAAI;CACjB;EAAE,GAAG;EAAM,GAAG;EAAI,GAAG;EAAQ;CAC7B;EAAE,GAAG;EAAK,GAAG;EAAI;CACjB;EAAE,GAAG;EAAM,GAAG;EAAI,GAAG;EAAO;CAC5B;EAAE,GAAG;EAAK,GAAG;EAAI;CACjB;EAAE,GAAG;EAAM,GAAG;EAAI,GAAG;EAAS;CAC9B;EAAE,GAAG;EAAK,GAAG;EAAI;CACjB;EAAE,GAAG;EAAM,GAAG;EAAQ;CACvB,EAEK,IAAuC;CAC3C,QAAQ;CACR,MAAM;CACN,GAAG;CACH,GAAG;CACH,IAAI;CACJ,GAAG;CACH,IAAI;CACJ,GAAG;CACH,IAAI;CACJ,GAAG;CACH,IAAI;CACJ,GAAG;CACH,IAAI;CACL,EAEY,IAAb,MAA4D;CAC1D,OAAO;CACP,UAAU;CAEV;CACA;CAEA,YAAY,GAA6B;AAKvC,EAJA,KAAK,SAAS;GACZ,YAAY,GAAQ,cAAc;GAClC,UAAU,GAAQ,YAAY,KAAK;GACpC,EACD,KAAK,UAAU;;CAMjB,QAAQ,GAAwC;EAC9C,IAAM,IAAS;AAmBd,EAdA,EAAe,UAAkB,UAAU,SAAU,GAAiC;AACrF,UAAO,EAAO,mBAAmB,MAAM,IAAO,EAAc;KAM7D,EAAe,UAAkB,QAAQ,SAAU,GAAiC;AACnF,UAAO,EAAO,mBAAmB,MAAM,IAAM,EAAc;KAM5D,EAAe,UAAkB,WAAW,SAC3C,GACA,GACQ;AAIR,UAHI,IACK,EAAO,mBAAm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