@decimalturn/toml-patch 3.2.1-dev → 3.2.1
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/LICENSE +22 -0
- package/README.md +202 -0
- package/dist/toml-patch.js +84 -0
- package/package.json +102 -16
- package/dist/dev/comment-alignment.js +0 -372
- package/dist/dev/comment-alignment.js.map +0 -1
- package/dist/dev/comment-ownership.js +0 -660
- package/dist/dev/comment-ownership.js.map +0 -1
- package/dist/dev/cst-source.js +0 -118
- package/dist/dev/cst-source.js.map +0 -1
- package/dist/dev/cst.js +0 -68
- package/dist/dev/cst.js.map +0 -1
- package/dist/dev/cursor.js +0 -66
- package/dist/dev/cursor.js.map +0 -1
- package/dist/dev/date-format.js +0 -196
- package/dist/dev/date-format.js.map +0 -1
- package/dist/dev/decode-utf8.js +0 -27
- package/dist/dev/decode-utf8.js.map +0 -1
- package/dist/dev/diff-lite.js +0 -123
- package/dist/dev/diff-lite.js.map +0 -1
- package/dist/dev/diff.js +0 -265
- package/dist/dev/diff.js.map +0 -1
- package/dist/dev/encode-lite.js +0 -193
- package/dist/dev/encode-lite.js.map +0 -1
- package/dist/dev/escape-preference.js +0 -166
- package/dist/dev/escape-preference.js.map +0 -1
- package/dist/dev/find-by-path.js +0 -111
- package/dist/dev/find-by-path.js.map +0 -1
- package/dist/dev/formatter.js +0 -298
- package/dist/dev/formatter.js.map +0 -1
- package/dist/dev/generate.js +0 -465
- package/dist/dev/generate.js.map +0 -1
- package/dist/dev/inline-comma-space.js +0 -13
- package/dist/dev/inline-comma-space.js.map +0 -1
- package/dist/dev/inline-format.js +0 -106
- package/dist/dev/inline-format.js.map +0 -1
- package/dist/dev/inline-layout.js +0 -107
- package/dist/dev/inline-layout.js.map +0 -1
- package/dist/dev/line-ending-backslash.js +0 -320
- package/dist/dev/line-ending-backslash.js.map +0 -1
- package/dist/dev/literal-string.js +0 -46
- package/dist/dev/literal-string.js.map +0 -1
- package/dist/dev/location.js +0 -104
- package/dist/dev/location.js.map +0 -1
- package/dist/dev/parse-error.js +0 -27
- package/dist/dev/parse-error.js.map +0 -1
- package/dist/dev/parse-js.js +0 -150
- package/dist/dev/parse-js.js.map +0 -1
- package/dist/dev/parse-string.js +0 -122
- package/dist/dev/parse-string.js.map +0 -1
- package/dist/dev/parse-toml.js +0 -714
- package/dist/dev/parse-toml.js.map +0 -1
- package/dist/dev/patch-lite.js +0 -4
- package/dist/dev/patch-lite2.js +0 -96
- package/dist/dev/patch-lite2.js.map +0 -1
- package/dist/dev/patch.js +0 -2652
- package/dist/dev/patch.js.map +0 -1
- package/dist/dev/smol-toml-date.js +0 -61
- package/dist/dev/smol-toml-date.js.map +0 -1
- package/dist/dev/to-js.js +0 -303
- package/dist/dev/to-js.js.map +0 -1
- package/dist/dev/to-toml.js +0 -874
- package/dist/dev/to-toml.js.map +0 -1
- package/dist/dev/tokenizer.js +0 -225
- package/dist/dev/tokenizer.js.map +0 -1
- package/dist/dev/toml-document.js +0 -220
- package/dist/dev/toml-document.js.map +0 -1
- package/dist/dev/toml-format.js +0 -528
- package/dist/dev/toml-format.js.map +0 -1
- package/dist/dev/toml-patch.js +0 -94
- package/dist/dev/toml-patch.js.map +0 -1
- package/dist/dev/traverse.js +0 -61
- package/dist/dev/traverse.js.map +0 -1
- package/dist/dev/truncate.js +0 -64
- package/dist/dev/truncate.js.map +0 -1
- package/dist/dev/update-order.js +0 -235
- package/dist/dev/update-order.js.map +0 -1
- package/dist/dev/utils.js +0 -244
- package/dist/dev/utils.js.map +0 -1
- package/dist/dev/writer.js +0 -758
- package/dist/dev/writer.js.map +0 -1
- package/dist/patch-lite.d.ts +0 -18
package/dist/dev/date-format.js
DELETED
|
@@ -1,196 +0,0 @@
|
|
|
1
|
-
//! @decimalturn/toml-patch v3.2.1 - https://github.com/DecimalTurn/toml-patch - @license: MIT
|
|
2
|
-
//#region src/date-format.ts
|
|
3
|
-
/**
|
|
4
|
-
* Central module for TOML date/time format handling and custom date classes.
|
|
5
|
-
* This module provides all the patterns, classes, and utilities needed to work
|
|
6
|
-
* with the different date/time formats supported by the TOML specification.
|
|
7
|
-
*/
|
|
8
|
-
/** Extract UTC date/time parts as zero-padded strings. */
|
|
9
|
-
function fmtParts(d) {
|
|
10
|
-
return {
|
|
11
|
-
year: String(d.getUTCFullYear()).padStart(4, "0"),
|
|
12
|
-
month: String(d.getUTCMonth() + 1).padStart(2, "0"),
|
|
13
|
-
day: String(d.getUTCDate()).padStart(2, "0"),
|
|
14
|
-
hours: String(d.getUTCHours()).padStart(2, "0"),
|
|
15
|
-
minutes: String(d.getUTCMinutes()).padStart(2, "0"),
|
|
16
|
-
seconds: String(d.getUTCSeconds()).padStart(2, "0"),
|
|
17
|
-
ms: d.getUTCMilliseconds()
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
/** Format millisecond suffix (".123") preserving original precision, or "" if none needed. */
|
|
21
|
-
function fmtMs(ms, origFmt, msRe = /\.(\d+)\s*$/) {
|
|
22
|
-
if (origFmt && origFmt.includes(".")) {
|
|
23
|
-
const m = origFmt.match(msRe);
|
|
24
|
-
const digits = m ? m[1].length : 3;
|
|
25
|
-
return "." + String(ms).padStart(3, "0").slice(0, digits);
|
|
26
|
-
}
|
|
27
|
-
if (ms > 0) return "." + String(ms).padStart(3, "0").replace(/0+$/, "");
|
|
28
|
-
return "";
|
|
29
|
-
}
|
|
30
|
-
/** Parse a timezone offset string like "+09:00" or "Z" into minutes. */
|
|
31
|
-
function parseOffsetMinutes(offset) {
|
|
32
|
-
if (offset === "Z") return 0;
|
|
33
|
-
const sign = offset[0] === "+" ? 1 : -1;
|
|
34
|
-
const [h, m] = offset.slice(1).split(":");
|
|
35
|
-
return sign * (parseInt(h) * 60 + parseInt(m));
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Helper class containing all date format patterns and utilities for TOML date/time handling
|
|
39
|
-
*/
|
|
40
|
-
var DateFormatHelper = class DateFormatHelper {
|
|
41
|
-
static IS_DATE_ONLY = /^\d{4}-\d{2}-\d{2}$/;
|
|
42
|
-
static IS_TIME_ONLY = /^\d{2}:\d{2}(?::\d{2})?(?:\.\d+)?$/;
|
|
43
|
-
static IS_LOCAL_DATETIME_T = /^\d{4}-\d{2}-\d{2}[Tt]\d{2}:\d{2}(?::\d{2})?(?:\.\d+)?$/;
|
|
44
|
-
static IS_LOCAL_DATETIME_SPACE = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}(?::\d{2})?(?:\.\d+)?$/;
|
|
45
|
-
static IS_OFFSET_DATETIME_T = /^\d{4}-\d{2}-\d{2}[Tt]\d{2}:\d{2}(?::\d{2})?(?:\.\d+)?(?:[Zz]|[+-]\d{2}:\d{2})$/;
|
|
46
|
-
static IS_OFFSET_DATETIME_SPACE = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}(?::\d{2})?(?:\.\d+)?(?:[Zz]|[+-]\d{2}:\d{2})$/;
|
|
47
|
-
static IS_FULL_DATE = /(\d{4})-(\d+)-(\d+)/;
|
|
48
|
-
static IS_FULL_TIME = /(\d+):(\d+)(?::(\d+))?/;
|
|
49
|
-
/**
|
|
50
|
-
* Creates a custom date/time object that preserves the original TOML date/time format.
|
|
51
|
-
*
|
|
52
|
-
* This method detects the TOML date/time format from the raw string and returns an appropriate
|
|
53
|
-
* custom date/time instance (e.g., LocalDate, LocalTime, LocalDateTime, OffsetDateTime) or a Date,
|
|
54
|
-
* using the provided new JavaScript Date value.
|
|
55
|
-
*
|
|
56
|
-
* @param {Date} newJSDate - The new JavaScript Date object representing the updated
|
|
57
|
-
* date/time value. This is used as the source for constructing the custom date/time object.
|
|
58
|
-
* In some cases, this may be a custom date/time object (e.g., LocalTime) instead of a native Date.
|
|
59
|
-
* @param {string} originalRaw - The original TOML date/time string as it appeared in the input.
|
|
60
|
-
* Used to detect the specific TOML date/time format and to extract formatting details (e.g., separator, offset).
|
|
61
|
-
*
|
|
62
|
-
* @returns {Date | LocalDate | LocalTime | LocalDateTime | OffsetDateTime}
|
|
63
|
-
* Returns a custom date/time object that matches the original TOML format:
|
|
64
|
-
* - LocalDate for date-only values (e.g., "2024-01-15")
|
|
65
|
-
* - LocalTime for time-only values (e.g., "10:30:00")
|
|
66
|
-
* - LocalDateTime for local datetimes (e.g., "2024-01-15T10:30:00" or "2024-01-15 10:30:00")
|
|
67
|
-
* - OffsetDateTime for datetimes with offsets (e.g., "2024-01-15T10:30:00+02:00")
|
|
68
|
-
* - Date (native JS Date) as a fallback if the format is unrecognized
|
|
69
|
-
*
|
|
70
|
-
* Format-specific behavior:
|
|
71
|
-
* - Date-only: Returns a LocalDate constructed from the date part of newJSDate.
|
|
72
|
-
* - Time-only: Returns a LocalTime, either from newJSDate (if already LocalTime) or constructed from its time part.
|
|
73
|
-
* - Local datetime: Returns a LocalDateTime, preserving the separator (T or space).
|
|
74
|
-
* - Offset datetime: Returns an OffsetDateTime, reconstructing the date/time with the original offset and separator.
|
|
75
|
-
* - Fallback: Returns newJSDate as-is.
|
|
76
|
-
*/
|
|
77
|
-
static createDateWithOriginalFormat(newJSDate, originalRaw) {
|
|
78
|
-
if (DateFormatHelper.IS_DATE_ONLY.test(originalRaw)) {
|
|
79
|
-
if (newJSDate.getUTCHours() !== 0 || newJSDate.getUTCMinutes() !== 0 || newJSDate.getUTCSeconds() !== 0 || newJSDate.getUTCMilliseconds() !== 0) {
|
|
80
|
-
if (newJSDate instanceof OffsetDateTime) return newJSDate;
|
|
81
|
-
let isoString = newJSDate.toISOString().replace("Z", "");
|
|
82
|
-
isoString = isoString.replace(/\.000$/, "");
|
|
83
|
-
return new LocalDateTime(isoString, false);
|
|
84
|
-
}
|
|
85
|
-
const dateStr = newJSDate.toISOString().split("T")[0];
|
|
86
|
-
return new LocalDate(dateStr);
|
|
87
|
-
} else if (DateFormatHelper.IS_TIME_ONLY.test(originalRaw)) {
|
|
88
|
-
if (newJSDate instanceof LocalTime) return newJSDate;
|
|
89
|
-
else {
|
|
90
|
-
const p = fmtParts(newJSDate);
|
|
91
|
-
const msSuffix = fmtMs(p.ms, originalRaw);
|
|
92
|
-
return new LocalTime(`${p.hours}:${p.minutes}:${p.seconds}${msSuffix}`, originalRaw);
|
|
93
|
-
}
|
|
94
|
-
} else if (DateFormatHelper.IS_LOCAL_DATETIME_T.test(originalRaw)) {
|
|
95
|
-
const p = fmtParts(newJSDate);
|
|
96
|
-
const msSuffix = fmtMs(p.ms, originalRaw);
|
|
97
|
-
return new LocalDateTime(`${p.year}-${p.month}-${p.day}T${p.hours}:${p.minutes}:${p.seconds}${msSuffix}`, false, originalRaw);
|
|
98
|
-
} else if (DateFormatHelper.IS_LOCAL_DATETIME_SPACE.test(originalRaw)) {
|
|
99
|
-
const p = fmtParts(newJSDate);
|
|
100
|
-
const msSuffix = fmtMs(p.ms, originalRaw);
|
|
101
|
-
return new LocalDateTime(`${p.year}-${p.month}-${p.day} ${p.hours}:${p.minutes}:${p.seconds}${msSuffix}`, true, originalRaw);
|
|
102
|
-
} else if (DateFormatHelper.IS_OFFSET_DATETIME_T.test(originalRaw) || DateFormatHelper.IS_OFFSET_DATETIME_SPACE.test(originalRaw)) {
|
|
103
|
-
const offsetMatch = originalRaw.match(/([+-]\d{2}:\d{2}|[Zz])$/);
|
|
104
|
-
const originalOffset = offsetMatch ? offsetMatch[1] === "z" ? "Z" : offsetMatch[1] : "Z";
|
|
105
|
-
const useSpaceSeparator = DateFormatHelper.IS_OFFSET_DATETIME_SPACE.test(originalRaw);
|
|
106
|
-
const p = fmtParts(new Date(newJSDate.getTime() + parseOffsetMinutes(originalOffset) * 6e4));
|
|
107
|
-
const sep = useSpaceSeparator ? " " : "T";
|
|
108
|
-
const msSuffix = fmtMs(p.ms, originalRaw, /\.(\d+)(?:[Zz]|[+-]\d{2}:\d{2})\s*$/);
|
|
109
|
-
const newDateTimeString = `${p.year}-${p.month}-${p.day}${sep}${p.hours}:${p.minutes}:${p.seconds}${msSuffix}${originalOffset}`;
|
|
110
|
-
return new OffsetDateTime(newDateTimeString, useSpaceSeparator);
|
|
111
|
-
} else return newJSDate;
|
|
112
|
-
}
|
|
113
|
-
};
|
|
114
|
-
/**
|
|
115
|
-
* Custom Date class for local dates (date-only).
|
|
116
|
-
* Format: 1979-05-27
|
|
117
|
-
*/
|
|
118
|
-
var LocalDate = class extends Date {
|
|
119
|
-
constructor(value) {
|
|
120
|
-
super(value);
|
|
121
|
-
}
|
|
122
|
-
toISOString() {
|
|
123
|
-
const p = fmtParts(this);
|
|
124
|
-
return `${p.year}-${p.month}-${p.day}`;
|
|
125
|
-
}
|
|
126
|
-
};
|
|
127
|
-
/**
|
|
128
|
-
* Custom Date class for local times (time-only)
|
|
129
|
-
* Format: 07:32:00 or 07:32:00.999
|
|
130
|
-
*/
|
|
131
|
-
var LocalTime = class extends Date {
|
|
132
|
-
originalFormat;
|
|
133
|
-
constructor(value, originalFormat) {
|
|
134
|
-
let normalizedValue = value;
|
|
135
|
-
if (!/:\d{2}:\d{2}/.test(value)) normalizedValue = value + ":00";
|
|
136
|
-
super(`0000-01-01T${normalizedValue}Z`);
|
|
137
|
-
this.originalFormat = originalFormat;
|
|
138
|
-
}
|
|
139
|
-
toISOString() {
|
|
140
|
-
const p = fmtParts(this);
|
|
141
|
-
return `${p.hours}:${p.minutes}:${p.seconds}${fmtMs(p.ms, this.originalFormat)}`;
|
|
142
|
-
}
|
|
143
|
-
};
|
|
144
|
-
/**
|
|
145
|
-
* Custom Date class for local datetime (no timezone)
|
|
146
|
-
* Format: 1979-05-27T07:32:00 or 1979-05-27 07:32:00
|
|
147
|
-
*/
|
|
148
|
-
var LocalDateTime = class extends Date {
|
|
149
|
-
useSpaceSeparator = false;
|
|
150
|
-
originalFormat;
|
|
151
|
-
constructor(value, useSpaceSeparator = false, originalFormat) {
|
|
152
|
-
let normalizedValue = value;
|
|
153
|
-
if (!/\d{2}:\d{2}:\d{2}/.test(value)) normalizedValue = value.replace(/(\d{2}:\d{2})([\s\-+TZ]|$)/, "$1:00$2");
|
|
154
|
-
super(normalizedValue.replace(" ", "T") + "Z");
|
|
155
|
-
this.useSpaceSeparator = useSpaceSeparator;
|
|
156
|
-
this.originalFormat = originalFormat || value;
|
|
157
|
-
}
|
|
158
|
-
toISOString() {
|
|
159
|
-
const p = fmtParts(this);
|
|
160
|
-
const sep = this.useSpaceSeparator ? " " : "T";
|
|
161
|
-
return `${p.year}-${p.month}-${p.day}${sep}${p.hours}:${p.minutes}:${p.seconds}${fmtMs(p.ms, this.originalFormat)}`;
|
|
162
|
-
}
|
|
163
|
-
};
|
|
164
|
-
/**
|
|
165
|
-
* Custom Date class for offset datetime that preserves space separator
|
|
166
|
-
* Format: 1979-05-27T07:32:00Z or 1979-05-27 07:32:00-07:00
|
|
167
|
-
*/
|
|
168
|
-
var OffsetDateTime = class extends Date {
|
|
169
|
-
useSpaceSeparator = false;
|
|
170
|
-
originalOffset;
|
|
171
|
-
originalFormat;
|
|
172
|
-
constructor(value, useSpaceSeparator = false) {
|
|
173
|
-
let normalizedValue = value;
|
|
174
|
-
if (!/\d{2}:\d{2}:\d{2}/.test(value)) normalizedValue = value.replace(/(\d{2}:\d{2})([\s\-+TZ]|$)/, "$1:00$2");
|
|
175
|
-
super(normalizedValue.replace(" ", "T"));
|
|
176
|
-
this.useSpaceSeparator = useSpaceSeparator;
|
|
177
|
-
this.originalFormat = value;
|
|
178
|
-
const offsetMatch = value.match(/([+-]\d{2}:\d{2}|[Zz])$/);
|
|
179
|
-
if (offsetMatch) this.originalOffset = offsetMatch[1] === "z" ? "Z" : offsetMatch[1];
|
|
180
|
-
}
|
|
181
|
-
toISOString() {
|
|
182
|
-
if (this.originalOffset) {
|
|
183
|
-
const p = fmtParts(new Date(this.getTime() + parseOffsetMinutes(this.originalOffset) * 6e4));
|
|
184
|
-
const sep = this.useSpaceSeparator ? " " : "T";
|
|
185
|
-
const msSuffix = fmtMs(p.ms, this.originalFormat, /\.(\d+)(?:[Zz]|[+-]\d{2}:\d{2})\s*$/);
|
|
186
|
-
return `${p.year}-${p.month}-${p.day}${sep}${p.hours}:${p.minutes}:${p.seconds}${msSuffix}${this.originalOffset}`;
|
|
187
|
-
}
|
|
188
|
-
const isoString = super.toISOString();
|
|
189
|
-
if (this.useSpaceSeparator) return isoString.replace("T", " ");
|
|
190
|
-
return isoString;
|
|
191
|
-
}
|
|
192
|
-
};
|
|
193
|
-
|
|
194
|
-
//#endregion
|
|
195
|
-
export { DateFormatHelper, LocalDate, LocalDateTime, LocalTime, OffsetDateTime };
|
|
196
|
-
//# sourceMappingURL=date-format.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"date-format.js","names":[],"sources":["../../src/date-format.ts"],"sourcesContent":["/**\n * Central module for TOML date/time format handling and custom date classes.\n * This module provides all the patterns, classes, and utilities needed to work\n * with the different date/time formats supported by the TOML specification.\n */\n\n// ---------------------------------------------------------------------------\n// Shared formatting helpers (extracted to reduce duplication)\n// ---------------------------------------------------------------------------\n\n/** Extract UTC date/time parts as zero-padded strings. */\nfunction fmtParts(d: Date) {\n return {\n year: String(d.getUTCFullYear()).padStart(4, '0'),\n month: String(d.getUTCMonth() + 1).padStart(2, '0'),\n day: String(d.getUTCDate()).padStart(2, '0'),\n hours: String(d.getUTCHours()).padStart(2, '0'),\n minutes: String(d.getUTCMinutes()).padStart(2, '0'),\n seconds: String(d.getUTCSeconds()).padStart(2, '0'),\n ms: d.getUTCMilliseconds(),\n };\n}\n\n/** Format millisecond suffix (\".123\") preserving original precision, or \"\" if none needed. */\nfunction fmtMs(ms: number, origFmt: string, msRe: RegExp = /\\.(\\d+)\\s*$/): string {\n const hadMs = origFmt && origFmt.includes('.');\n if (hadMs) {\n const m = origFmt.match(msRe);\n const digits = m ? m[1].length : 3;\n return '.' + String(ms).padStart(3, '0').slice(0, digits);\n }\n if (ms > 0) {\n return '.' + String(ms).padStart(3, '0').replace(/0+$/, '');\n }\n return '';\n}\n\n/** Parse a timezone offset string like \"+09:00\" or \"Z\" into minutes. */\nfunction parseOffsetMinutes(offset: string): number {\n if (offset === 'Z') return 0;\n const sign = offset[0] === '+' ? 1 : -1;\n const [h, m] = offset.slice(1).split(':');\n return sign * (parseInt(h) * 60 + parseInt(m));\n}\n\n// ---------------------------------------------------------------------------\n\n/**\n * Helper class containing all date format patterns and utilities for TOML date/time handling\n */\nexport class DateFormatHelper {\n // Patterns for different date/time formats\n static readonly IS_DATE_ONLY = /^\\d{4}-\\d{2}-\\d{2}$/;\n static readonly IS_TIME_ONLY = /^\\d{2}:\\d{2}(?::\\d{2})?(?:\\.\\d+)?$/;\n static readonly IS_LOCAL_DATETIME_T = /^\\d{4}-\\d{2}-\\d{2}[Tt]\\d{2}:\\d{2}(?::\\d{2})?(?:\\.\\d+)?$/;\n static readonly IS_LOCAL_DATETIME_SPACE = /^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}(?::\\d{2})?(?:\\.\\d+)?$/;\n static readonly IS_OFFSET_DATETIME_T = /^\\d{4}-\\d{2}-\\d{2}[Tt]\\d{2}:\\d{2}(?::\\d{2})?(?:\\.\\d+)?(?:[Zz]|[+-]\\d{2}:\\d{2})$/;\n static readonly IS_OFFSET_DATETIME_SPACE = /^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}(?::\\d{2})?(?:\\.\\d+)?(?:[Zz]|[+-]\\d{2}:\\d{2})$/;\n \n // Legacy patterns from parse-toml.ts (for compatibility)\n // Made more permissive to catch malformed dates (e.g., 1987-7-05) for validation\n static readonly IS_FULL_DATE = /(\\d{4})-(\\d+)-(\\d+)/;\n static readonly IS_FULL_TIME = /(\\d+):(\\d+)(?::(\\d+))?/;\n\n /**\n * Creates a custom date/time object that preserves the original TOML date/time format.\n * \n * This method detects the TOML date/time format from the raw string and returns an appropriate\n * custom date/time instance (e.g., LocalDate, LocalTime, LocalDateTime, OffsetDateTime) or a Date,\n * using the provided new JavaScript Date value.\n * \n * @param {Date} newJSDate - The new JavaScript Date object representing the updated \n * date/time value. This is used as the source for constructing the custom date/time object.\n * In some cases, this may be a custom date/time object (e.g., LocalTime) instead of a native Date.\n * @param {string} originalRaw - The original TOML date/time string as it appeared in the input.\n * Used to detect the specific TOML date/time format and to extract formatting details (e.g., separator, offset).\n * \n * @returns {Date | LocalDate | LocalTime | LocalDateTime | OffsetDateTime}\n * Returns a custom date/time object that matches the original TOML format:\n * - LocalDate for date-only values (e.g., \"2024-01-15\")\n * - LocalTime for time-only values (e.g., \"10:30:00\")\n * - LocalDateTime for local datetimes (e.g., \"2024-01-15T10:30:00\" or \"2024-01-15 10:30:00\")\n * - OffsetDateTime for datetimes with offsets (e.g., \"2024-01-15T10:30:00+02:00\")\n * - Date (native JS Date) as a fallback if the format is unrecognized\n * \n * Format-specific behavior:\n * - Date-only: Returns a LocalDate constructed from the date part of newJSDate.\n * - Time-only: Returns a LocalTime, either from newJSDate (if already LocalTime) or constructed from its time part.\n * - Local datetime: Returns a LocalDateTime, preserving the separator (T or space).\n * - Offset datetime: Returns an OffsetDateTime, reconstructing the date/time with the original offset and separator.\n * - Fallback: Returns newJSDate as-is.\n */\n static createDateWithOriginalFormat(newJSDate: Date, originalRaw: string): Date {\n if (DateFormatHelper.IS_DATE_ONLY.test(originalRaw)) {\n // Local date (date-only) - format: 2024-01-15\n // Check if newJSDate has time components - if so, upgrade appropriately\n if (\n newJSDate.getUTCHours() !== 0 ||\n newJSDate.getUTCMinutes() !== 0 ||\n newJSDate.getUTCSeconds() !== 0 ||\n newJSDate.getUTCMilliseconds() !== 0\n ) {\n // Check if the new value is an OffsetDateTime (has offset information)\n if (newJSDate instanceof OffsetDateTime) {\n // Upgrade to OffsetDateTime - it already has the right format\n return newJSDate;\n }\n \n // Upgrade from date-only to local datetime with time components\n // Use T separator as it's the more common format\n let isoString = newJSDate.toISOString().replace('Z', '');\n // Strip .000 milliseconds if present (don't show unnecessary precision)\n isoString = isoString.replace(/\\.000$/, '');\n return new LocalDateTime(isoString, false);\n }\n const dateStr = newJSDate.toISOString().split('T')[0];\n return new LocalDate(dateStr);\n } else if (DateFormatHelper.IS_TIME_ONLY.test(originalRaw)) {\n // Local time (time-only) - format: 10:30:00\n // For time-only values, we need to handle this more carefully\n // The newJSDate might be a LocalTime object itself\n if (newJSDate instanceof LocalTime) {\n // If the new date is already a LocalTime, use its toISOString\n return newJSDate;\n } else {\n const p = fmtParts(newJSDate);\n const msSuffix = fmtMs(p.ms, originalRaw);\n return new LocalTime(`${p.hours}:${p.minutes}:${p.seconds}${msSuffix}`, originalRaw);\n }\n } else if (DateFormatHelper.IS_LOCAL_DATETIME_T.test(originalRaw)) {\n // Local datetime with T separator - format: 2024-01-15T10:30:00\n const p = fmtParts(newJSDate);\n const msSuffix = fmtMs(p.ms, originalRaw);\n return new LocalDateTime(`${p.year}-${p.month}-${p.day}T${p.hours}:${p.minutes}:${p.seconds}${msSuffix}`, false, originalRaw);\n } else if (DateFormatHelper.IS_LOCAL_DATETIME_SPACE.test(originalRaw)) {\n // Local datetime with space separator - format: 2024-01-15 10:30:00\n const p = fmtParts(newJSDate);\n const msSuffix = fmtMs(p.ms, originalRaw);\n return new LocalDateTime(`${p.year}-${p.month}-${p.day} ${p.hours}:${p.minutes}:${p.seconds}${msSuffix}`, true, originalRaw);\n } else if (DateFormatHelper.IS_OFFSET_DATETIME_T.test(originalRaw) || DateFormatHelper.IS_OFFSET_DATETIME_SPACE.test(originalRaw)) {\n // Offset datetime - preserve the original timezone offset and separator\n const offsetMatch = originalRaw.match(/([+-]\\d{2}:\\d{2}|[Zz])$/);\n const originalOffset = offsetMatch ? (offsetMatch[1] === 'z' ? 'Z' : offsetMatch[1]) : 'Z';\n const useSpaceSeparator = DateFormatHelper.IS_OFFSET_DATETIME_SPACE.test(originalRaw);\n \n // Convert UTC time to local time in the original timezone\n const localTime = new Date(newJSDate.getTime() + parseOffsetMinutes(originalOffset) * 60000);\n const p = fmtParts(localTime);\n const sep = useSpaceSeparator ? ' ' : 'T';\n const msSuffix = fmtMs(p.ms, originalRaw, /\\.(\\d+)(?:[Zz]|[+-]\\d{2}:\\d{2})\\s*$/);\n \n const newDateTimeString = `${p.year}-${p.month}-${p.day}${sep}${p.hours}:${p.minutes}:${p.seconds}${msSuffix}${originalOffset}`;\n return new OffsetDateTime(newDateTimeString, useSpaceSeparator);\n } else {\n // Fallback to regular Date\n return newJSDate;\n }\n }\n}\n\n/**\n * Custom Date class for local dates (date-only).\n * Format: 1979-05-27\n */\nexport class LocalDate extends Date {\n\n constructor(value: string) {\n super(value);\n }\n \n toISOString(): string {\n const p = fmtParts(this);\n return `${p.year}-${p.month}-${p.day}`;\n }\n}\n\n/**\n * Custom Date class for local times (time-only)\n * Format: 07:32:00 or 07:32:00.999\n */\nexport class LocalTime extends Date {\n originalFormat: string;\n \n constructor(value: string, originalFormat: string) {\n // Normalize time to include seconds if missing (TOML 1.1.0 allows optional seconds)\n let normalizedValue = value;\n if (!/:\\d{2}:\\d{2}/.test(value)) {\n // No seconds present, add :00\n normalizedValue = value + ':00';\n }\n // For local time, use year 0000 as the base (TOML spec compliance)\n // Add 'Z' to ensure it's parsed as UTC regardless of system timezone\n super(`0000-01-01T${normalizedValue}Z`);\n this.originalFormat = originalFormat;\n }\n \n toISOString(): string {\n const p = fmtParts(this);\n return `${p.hours}:${p.minutes}:${p.seconds}${fmtMs(p.ms, this.originalFormat)}`;\n }\n}\n\n/**\n * Custom Date class for local datetime (no timezone)\n * Format: 1979-05-27T07:32:00 or 1979-05-27 07:32:00\n */\nexport class LocalDateTime extends Date {\n useSpaceSeparator: boolean = false;\n originalFormat: string;\n \n constructor(value: string, useSpaceSeparator: boolean = false, originalFormat?: string) {\n // Normalize time part to include seconds if missing (TOML 1.1.0 allows optional seconds)\n let normalizedValue = value;\n if (!/\\d{2}:\\d{2}:\\d{2}/.test(value)) {\n normalizedValue = value.replace(/(\\d{2}:\\d{2})([\\s\\-+TZ]|$)/, '$1:00$2');\n }\n // Convert space to T for Date parsing, but remember the original format\n super(normalizedValue.replace(' ', 'T') + 'Z');\n this.useSpaceSeparator = useSpaceSeparator;\n this.originalFormat = originalFormat || value;\n }\n \n toISOString(): string {\n const p = fmtParts(this);\n const sep = this.useSpaceSeparator ? ' ' : 'T';\n return `${p.year}-${p.month}-${p.day}${sep}${p.hours}:${p.minutes}:${p.seconds}${fmtMs(p.ms, this.originalFormat)}`;\n }\n}\n\n/**\n * Custom Date class for offset datetime that preserves space separator\n * Format: 1979-05-27T07:32:00Z or 1979-05-27 07:32:00-07:00\n */\nexport class OffsetDateTime extends Date {\n useSpaceSeparator: boolean = false;\n originalOffset?: string;\n originalFormat: string;\n \n constructor(value: string, useSpaceSeparator: boolean = false) {\n // Normalize time part to include seconds if missing (TOML 1.1.0 allows optional seconds)\n let normalizedValue = value;\n if (!/\\d{2}:\\d{2}:\\d{2}/.test(value)) {\n normalizedValue = value.replace(/(\\d{2}:\\d{2})([\\s\\-+TZ]|$)/, '$1:00$2');\n }\n super(normalizedValue.replace(' ', 'T'));\n this.useSpaceSeparator = useSpaceSeparator;\n this.originalFormat = value;\n \n // Extract and preserve the original offset\n const offsetMatch = value.match(/([+-]\\d{2}:\\d{2}|[Zz])$/);\n if (offsetMatch) {\n this.originalOffset = offsetMatch[1] === 'z' ? 'Z' : offsetMatch[1];\n }\n }\n \n toISOString(): string {\n if (this.originalOffset) {\n const localTime = new Date(this.getTime() + parseOffsetMinutes(this.originalOffset) * 60000);\n const p = fmtParts(localTime);\n const sep = this.useSpaceSeparator ? ' ' : 'T';\n const msSuffix = fmtMs(p.ms, this.originalFormat, /\\.(\\d+)(?:[Zz]|[+-]\\d{2}:\\d{2})\\s*$/);\n return `${p.year}-${p.month}-${p.day}${sep}${p.hours}:${p.minutes}:${p.seconds}${msSuffix}${this.originalOffset}`;\n }\n \n const isoString = super.toISOString();\n if (this.useSpaceSeparator) {\n return isoString.replace('T', ' ');\n }\n return isoString;\n }\n}"],"mappings":";;;;;;;;AAWA,SAAS,SAAS,GAAS;CACzB,OAAO;EACL,MAAM,OAAO,EAAE,eAAe,CAAC,CAAC,CAAC,SAAS,GAAG,GAAG;EAChD,OAAO,OAA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AAE,MAAM,GAAG,EAAE,MAAM,MAAM,EAAE,MAAM,GAAG,EAAE,QAAQ,GAAG,EAAE,UAAU,WAAW,KAAK;EACnG;EAEA,MAAM,YAAY,MAAM,YAAY;EACpC,IAAI,KAAK,mBACP,OAAO,UAAU,QAAQ,KAAK,GAAG;EAEnC,OAAO;CACT;AACF"}
|
package/dist/dev/decode-utf8.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
//! @decimalturn/toml-patch v3.2.1 - https://github.com/DecimalTurn/toml-patch - @license: MIT
|
|
2
|
-
//#region src/decode-utf8.ts
|
|
3
|
-
/**
|
|
4
|
-
* Decodes UTF-8 bytes in fatal mode, preserving any leading BOM.
|
|
5
|
-
*/
|
|
6
|
-
function decodeUtf8Bytes(value) {
|
|
7
|
-
return new TextDecoder("utf-8", {
|
|
8
|
-
fatal: true,
|
|
9
|
-
ignoreBOM: true
|
|
10
|
-
}).decode(value);
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* Returns true when the input starts with a Unicode BOM character (U+FEFF).
|
|
14
|
-
*/
|
|
15
|
-
function hasLeadingBom(value) {
|
|
16
|
-
return value.charCodeAt(0) === 65279;
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Strips a leading Unicode BOM character (U+FEFF) from string input.
|
|
20
|
-
*/
|
|
21
|
-
function stripLeadingBom(value) {
|
|
22
|
-
return hasLeadingBom(value) ? value.slice(1) : value;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
//#endregion
|
|
26
|
-
export { decodeUtf8Bytes, hasLeadingBom, stripLeadingBom };
|
|
27
|
-
//# sourceMappingURL=decode-utf8.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"decode-utf8.js","names":[],"sources":["../../src/decode-utf8.ts"],"sourcesContent":["/**\n * UTF-8 BOM marker (U+FEFF).\n */\nexport const UTF8_BOM = '\\uFEFF';\n\n/**\n * Decodes UTF-8 bytes in fatal mode, preserving any leading BOM.\n */\nexport function decodeUtf8Bytes(value: Uint8Array): string {\n return new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(value);\n}\n\n/**\n * Returns true when the input starts with a Unicode BOM character (U+FEFF).\n */\nexport function hasLeadingBom(value: string): boolean {\n return value.charCodeAt(0) === 0xfeff;\n}\n\n/**\n * Returns true when raw bytes start with a UTF-8 BOM prefix.\n */\nexport function hasLeadingUtf8BomBytes(value: Uint8Array): boolean {\n return value.length >= 3 && value[0] === 0xef && value[1] === 0xbb && value[2] === 0xbf;\n}\n\n/**\n * Strips a leading Unicode BOM character (U+FEFF) from string input.\n */\nexport function stripLeadingBom(value: string): string {\n return hasLeadingBom(value) ? value.slice(1) : value;\n}\n"],"mappings":";;;;;AAQA,SAAgB,gBAAgB,OAA2B;CACzD,OAAO,IAAI,YAAY,SAAS;EAAE,OAAO;EAAM,WAAW;CAAK,CAAC,CAAC,CAAC,OAAO,KAAK;AAChF;;;;AAKA,SAAgB,cAAc,OAAwB;CACpD,OAAO,MAAM,WAAW,CAAC,MAAM;AACjC;;;;AAYA,SAAgB,gBAAgB,OAAuB;CACrD,OAAO,cAAc,KAAK,IAAI,MAAM,MAAM,CAAC,IAAI;AACjD"}
|
package/dist/dev/diff-lite.js
DELETED
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
//! @decimalturn/toml-patch v3.2.1 - https://github.com/DecimalTurn/toml-patch - @license: MIT
|
|
2
|
-
import { datesEqual, isNegativeNan, isObject, sameValue, stableStringify } from "./utils.js";
|
|
3
|
-
|
|
4
|
-
//#region src/diff-lite.ts
|
|
5
|
-
var PatchLiteError = class extends Error {
|
|
6
|
-
code;
|
|
7
|
-
path;
|
|
8
|
-
constructor(code, path, message) {
|
|
9
|
-
super(message);
|
|
10
|
-
this.name = "PatchLiteError";
|
|
11
|
-
this.code = code;
|
|
12
|
-
this.path = path;
|
|
13
|
-
}
|
|
14
|
-
};
|
|
15
|
-
/**
|
|
16
|
-
* Formats a path as `a.b[0].c` for error messages.
|
|
17
|
-
*/
|
|
18
|
-
function formatPath(path) {
|
|
19
|
-
let out = "";
|
|
20
|
-
for (const segment of path) if (typeof segment === "number") out += `[${segment}]`;
|
|
21
|
-
else out += out ? `.${segment}` : segment;
|
|
22
|
-
return out || "(root)";
|
|
23
|
-
}
|
|
24
|
-
function isSupportedLeaf(value) {
|
|
25
|
-
const type = typeof value;
|
|
26
|
-
return type === "string" || type === "boolean" || type === "number" || type === "bigint";
|
|
27
|
-
}
|
|
28
|
-
function describe(value) {
|
|
29
|
-
if (value === null) return "null";
|
|
30
|
-
if (Array.isArray(value)) return "array";
|
|
31
|
-
if (value instanceof Date) return "date";
|
|
32
|
-
const type = typeof value;
|
|
33
|
-
return type === "object" ? "table" : type;
|
|
34
|
-
}
|
|
35
|
-
function sameNanSign(a, b) {
|
|
36
|
-
return isNegativeNan(a) === isNegativeNan(b);
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Classifies a Date subclass by the shape of its TOML rendering, duck-typed so
|
|
40
|
-
* the lite bundle needs neither toml-patch's nor smol-toml's date module. The
|
|
41
|
-
* two packages have distinct class objects, so `instanceof` cannot be used to
|
|
42
|
-
* recognise them anyway.
|
|
43
|
-
*/
|
|
44
|
-
function dateKind(value) {
|
|
45
|
-
const iso = value.toISOString();
|
|
46
|
-
if (/^\d{4}-\d{2}-\d{2}$/.test(iso)) return "date";
|
|
47
|
-
if (/^\d{2}:\d{2}:\d{2}(?:\.\d+)?$/.test(iso)) return "time";
|
|
48
|
-
if (/(?:[Zz]|[+-]\d{2}:\d{2})$/.test(iso)) return "offset";
|
|
49
|
-
return "datetime";
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* Dates are unchanged when they hold the same instant and the same TOML kind.
|
|
53
|
-
* Fractional seconds must not be compared by their rendered digit count:
|
|
54
|
-
* smol-toml always writes three digits (`.250`, `.000`) while toml-patch keeps
|
|
55
|
-
* the source precision (`.25`, or no suffix), so `getTime()` plus kind is used
|
|
56
|
-
* instead of `toISOString()`.
|
|
57
|
-
*/
|
|
58
|
-
function sameDateValue(a, b) {
|
|
59
|
-
return a.getTime() === b.getTime() && dateKind(a) === dateKind(b);
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Compares the existing and updated JavaScript values and returns the list of
|
|
63
|
-
* in-place edits. Any structural difference throws a `PatchLiteError` before
|
|
64
|
-
* the source is mutated.
|
|
65
|
-
*/
|
|
66
|
-
function diffLite(before, after, path = []) {
|
|
67
|
-
if (datesEqual(before, after)) return [];
|
|
68
|
-
if (before instanceof Date && after instanceof Date && sameDateValue(before, after)) return [];
|
|
69
|
-
if (sameValue(before, after)) return [];
|
|
70
|
-
if (typeof before === "number" && typeof after === "number" && Number.isNaN(before) && Number.isNaN(after) && sameNanSign(before, after)) return [];
|
|
71
|
-
const beforeIsArray = Array.isArray(before);
|
|
72
|
-
const afterIsArray = Array.isArray(after);
|
|
73
|
-
if (beforeIsArray && afterIsArray) return compareArrays(before, after, path);
|
|
74
|
-
if (beforeIsArray || afterIsArray) throw new PatchLiteError("TypeChange", path, `Cannot change a ${beforeIsArray ? "array" : describe(before)} to a ${afterIsArray ? "array" : describe(after)} at ${formatPath(path)}`);
|
|
75
|
-
const beforeIsObject = isObject(before);
|
|
76
|
-
const afterIsObject = isObject(after);
|
|
77
|
-
if (beforeIsObject && afterIsObject) return compareObjects(before, after, path);
|
|
78
|
-
if (beforeIsObject || afterIsObject) throw new PatchLiteError("TypeChange", path, `Cannot change a ${beforeIsObject ? "table" : describe(before)} to a ${afterIsObject ? "table" : describe(after)} at ${formatPath(path)}`);
|
|
79
|
-
if (before instanceof Date && after instanceof Date) return [{
|
|
80
|
-
type: "Edit",
|
|
81
|
-
path
|
|
82
|
-
}];
|
|
83
|
-
if (before instanceof Date || after instanceof Date) throw new PatchLiteError("TypeChange", path, `Cannot change a ${describe(before)} to a ${describe(after)} at ${formatPath(path)}`);
|
|
84
|
-
if (!isSupportedLeaf(before) || !isSupportedLeaf(after)) throw new PatchLiteError("UnsupportedValue", path, `Unsupported value type ${describe(after)} at ${formatPath(path)}`);
|
|
85
|
-
return [{
|
|
86
|
-
type: "Edit",
|
|
87
|
-
path
|
|
88
|
-
}];
|
|
89
|
-
}
|
|
90
|
-
function compareObjects(before, after, path) {
|
|
91
|
-
const beforeKeys = Object.keys(before);
|
|
92
|
-
const afterKeys = Object.keys(after);
|
|
93
|
-
const afterKeySet = new Set(afterKeys);
|
|
94
|
-
for (const key of beforeKeys) if (!afterKeySet.has(key)) throw new PatchLiteError("RemovedKey", path.concat(key), `Cannot remove key ${formatPath(path.concat(key))}`);
|
|
95
|
-
const beforeKeySet = new Set(beforeKeys);
|
|
96
|
-
for (const key of afterKeys) if (!beforeKeySet.has(key)) throw new PatchLiteError("AddedKey", path.concat(key), `Cannot add key ${formatPath(path.concat(key))}`);
|
|
97
|
-
const changes = [];
|
|
98
|
-
for (const key of beforeKeys) {
|
|
99
|
-
const sub = diffLite(before[key], after[key], path.concat(key));
|
|
100
|
-
for (const change of sub) changes.push(change);
|
|
101
|
-
}
|
|
102
|
-
return changes;
|
|
103
|
-
}
|
|
104
|
-
function compareArrays(before, after, path) {
|
|
105
|
-
if (before.length !== after.length) throw new PatchLiteError("ArrayLengthChange", path, `Cannot change the length of array at ${formatPath(path)} (${before.length} to ${after.length})`);
|
|
106
|
-
const beforeStable = before.map(stableStringify);
|
|
107
|
-
const afterStable = after.map(stableStringify);
|
|
108
|
-
if (!beforeStable.every((value, index) => value === afterStable[index])) {
|
|
109
|
-
const beforeBag = beforeStable.slice().sort();
|
|
110
|
-
const afterBag = afterStable.slice().sort();
|
|
111
|
-
if (beforeBag.length === afterBag.length && beforeBag.every((value, index) => value === afterBag[index])) throw new PatchLiteError("ArrayReorder", path, `Cannot reorder array elements at ${formatPath(path)}`);
|
|
112
|
-
}
|
|
113
|
-
const changes = [];
|
|
114
|
-
for (let index = 0; index < before.length; index++) {
|
|
115
|
-
const sub = diffLite(before[index], after[index], path.concat(index));
|
|
116
|
-
for (const change of sub) changes.push(change);
|
|
117
|
-
}
|
|
118
|
-
return changes;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
//#endregion
|
|
122
|
-
export { PatchLiteError, diffLite as default, formatPath };
|
|
123
|
-
//# sourceMappingURL=diff-lite.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"diff-lite.js","names":[],"sources":["../../src/diff-lite.ts"],"sourcesContent":["import { isObject, datesEqual, stableStringify, sameValue, isNegativeNan } from './utils';\n\n/**\n * A path into a JavaScript object, mixing object keys and array indices.\n */\nexport type Path = Array<string | number>;\n\n/**\n * The only change patch-lite can apply: replacing an existing value in place.\n */\nexport interface Edit {\n type: 'Edit';\n path: Path;\n}\n\n/**\n * The unsupported operations patch-lite rejects. The code is part of the\n * public error contract so callers can branch on it without parsing messages.\n */\nexport type PatchLiteErrorCode =\n | 'AddedKey'\n | 'RemovedKey'\n | 'ArrayLengthChange'\n | 'ArrayReorder'\n | 'TypeChange'\n | 'UnsupportedValue'\n | 'UnsupportedOption';\n\nexport class PatchLiteError extends Error {\n readonly code: PatchLiteErrorCode;\n readonly path: Path;\n\n constructor(code: PatchLiteErrorCode, path: Path, message: string) {\n super(message);\n this.name = 'PatchLiteError';\n this.code = code;\n this.path = path;\n }\n}\n\n/**\n * Formats a path as `a.b[0].c` for error messages.\n */\nexport function formatPath(path: Path): string {\n let out = '';\n for (const segment of path) {\n if (typeof segment === 'number') out += `[${segment}]`;\n else out += out ? `.${segment}` : segment;\n }\n return out || '(root)';\n}\n\nfunction isSupportedLeaf(value: any): boolean {\n const type = typeof value;\n return type === 'string' || type === 'boolean' || type === 'number' || type === 'bigint';\n}\n\nfunction describe(value: any): string {\n if (value === null) return 'null';\n if (Array.isArray(value)) return 'array';\n if (value instanceof Date) return 'date';\n const type = typeof value;\n return type === 'object' ? 'table' : type;\n}\n\nfunction sameNanSign(a: number, b: number): boolean {\n return isNegativeNan(a) === isNegativeNan(b);\n}\n\n/**\n * Classifies a Date subclass by the shape of its TOML rendering, duck-typed so\n * the lite bundle needs neither toml-patch's nor smol-toml's date module. The\n * two packages have distinct class objects, so `instanceof` cannot be used to\n * recognise them anyway.\n */\nfunction dateKind(value: Date): 'date' | 'time' | 'datetime' | 'offset' {\n const iso = value.toISOString();\n if (/^\\d{4}-\\d{2}-\\d{2}$/.test(iso)) return 'date';\n if (/^\\d{2}:\\d{2}:\\d{2}(?:\\.\\d+)?$/.test(iso)) return 'time';\n if (/(?:[Zz]|[+-]\\d{2}:\\d{2})$/.test(iso)) return 'offset';\n return 'datetime';\n}\n\n/**\n * Dates are unchanged when they hold the same instant and the same TOML kind.\n * Fractional seconds must not be compared by their rendered digit count:\n * smol-toml always writes three digits (`.250`, `.000`) while toml-patch keeps\n * the source precision (`.25`, or no suffix), so `getTime()` plus kind is used\n * instead of `toISOString()`.\n */\nfunction sameDateValue(a: Date, b: Date): boolean {\n return a.getTime() === b.getTime() && dateKind(a) === dateKind(b);\n}\n\n/**\n * Compares the existing and updated JavaScript values and returns the list of\n * in-place edits. Any structural difference throws a `PatchLiteError` before\n * the source is mutated.\n */\nexport default function diffLite(before: any, after: any, path: Path = []): Edit[] {\n if (datesEqual(before, after)) return [];\n\n if (before instanceof Date && after instanceof Date && sameDateValue(before, after)) {\n return [];\n }\n\n if (sameValue(before, after)) return [];\n\n if (\n typeof before === 'number' &&\n typeof after === 'number' &&\n Number.isNaN(before) &&\n Number.isNaN(after) &&\n sameNanSign(before, after)\n ) {\n return [];\n }\n\n const beforeIsArray = Array.isArray(before);\n const afterIsArray = Array.isArray(after);\n\n if (beforeIsArray && afterIsArray) {\n return compareArrays(before, after, path);\n }\n\n if (beforeIsArray || afterIsArray) {\n throw new PatchLiteError(\n 'TypeChange',\n path,\n `Cannot change a ${beforeIsArray ? 'array' : describe(before)} to a ${\n afterIsArray ? 'array' : describe(after)\n } at ${formatPath(path)}`\n );\n }\n\n const beforeIsObject = isObject(before);\n const afterIsObject = isObject(after);\n\n if (beforeIsObject && afterIsObject) {\n return compareObjects(before, after, path);\n }\n\n if (beforeIsObject || afterIsObject) {\n throw new PatchLiteError(\n 'TypeChange',\n path,\n `Cannot change a ${beforeIsObject ? 'table' : describe(before)} to a ${\n afterIsObject ? 'table' : describe(after)\n } at ${formatPath(path)}`\n );\n }\n\n if (before instanceof Date && after instanceof Date) {\n return [{ type: 'Edit', path }];\n }\n\n if (before instanceof Date || after instanceof Date) {\n throw new PatchLiteError(\n 'TypeChange',\n path,\n `Cannot change a ${describe(before)} to a ${describe(after)} at ${formatPath(path)}`\n );\n }\n\n if (!isSupportedLeaf(before) || !isSupportedLeaf(after)) {\n throw new PatchLiteError(\n 'UnsupportedValue',\n path,\n `Unsupported value type ${describe(after)} at ${formatPath(path)}`\n );\n }\n\n return [{ type: 'Edit', path }];\n}\n\nfunction compareObjects(before: any, after: any, path: Path): Edit[] {\n // Only key membership matters, not key order: patch-lite edits values by\n // path and never reorders keys, so the key order of `after` does not affect\n // the output and is intentionally ignored.\n const beforeKeys = Object.keys(before);\n const afterKeys = Object.keys(after);\n\n const afterKeySet = new Set(afterKeys);\n for (const key of beforeKeys) {\n if (!afterKeySet.has(key)) {\n throw new PatchLiteError(\n 'RemovedKey',\n path.concat(key),\n `Cannot remove key ${formatPath(path.concat(key))}`\n );\n }\n }\n\n const beforeKeySet = new Set(beforeKeys);\n for (const key of afterKeys) {\n if (!beforeKeySet.has(key)) {\n throw new PatchLiteError(\n 'AddedKey',\n path.concat(key),\n `Cannot add key ${formatPath(path.concat(key))}`\n );\n }\n }\n\n const changes: Edit[] = [];\n for (const key of beforeKeys) {\n const sub = diffLite(before[key], after[key], path.concat(key));\n for (const change of sub) changes.push(change);\n }\n return changes;\n}\n\nfunction compareArrays(before: any[], after: any[], path: Path): Edit[] {\n if (before.length !== after.length) {\n throw new PatchLiteError(\n 'ArrayLengthChange',\n path,\n `Cannot change the length of array at ${formatPath(path)} (${before.length} to ${after.length})`\n );\n }\n\n // A reorder keeps the same multiset of elements but changes their order.\n // Detect it via stable deep forms so genuine element edits still pass.\n const beforeStable = before.map(stableStringify);\n const afterStable = after.map(stableStringify);\n\n const positionsDiffer = !beforeStable.every((value, index) => value === afterStable[index]);\n if (positionsDiffer) {\n const beforeBag = beforeStable.slice().sort();\n const afterBag = afterStable.slice().sort();\n const sameElements =\n beforeBag.length === afterBag.length &&\n beforeBag.every((value, index) => value === afterBag[index]);\n\n if (sameElements) {\n throw new PatchLiteError(\n 'ArrayReorder',\n path,\n `Cannot reorder array elements at ${formatPath(path)}`\n );\n }\n }\n\n const changes: Edit[] = [];\n for (let index = 0; index < before.length; index++) {\n const sub = diffLite(before[index], after[index], path.concat(index));\n for (const change of sub) changes.push(change);\n }\n return changes;\n}\n"],"mappings":";;;;AA4BA,IAAa,iBAAb,cAAoC,MAAM;CACxC,AAAS;CACT,AAAS;CAET,YAAY,MAA0B,MAAY,SAAiB;EACjE,MAAM,OAAO;EACb,KAAK,OAAO;EACZ,KAAK,OAAO;EACZ,KAAK,OAAO;CACd;AACF;;;;AAKA,SAAgB,WAAW,MAAoB;CAC7C,IAAI,MAAM;CACV,KAAK,MAAM,WAAW,MACpB,IAAI,OAAO,YAAY,UAAU,OAAO,IAAI,QAAQ;MAC/C,OAAO,MAAM,IAAI,YAAY;CAEpC,OAAO,OAAO;AAChB;AAEA,SAAS,gBAAgB,OAAqB;CAC5C,MAAM,OAAO,OAAO;CACpB,OAAO,SAAS,YAAY,SAAS,aAAa,SAAS,YAAY,SAAS;AAClF;AAEA,SAAS,SAAS,OAAoB;CACpC,IAAI,UAAU,MAAM,OAAO;CAC3B,IAAI,MAAM,QAAQ,KAAK,GAAG,OAAO;CACjC,IAAI,iBAAiB,MAAM,OAAO;CAClC,MAAM,OAAO,OAAO;CACpB,OAAO,SAAS,WAAW,UAAU;AACvC;AAEA,SAAS,YAAY,GAAW,GAAoB;CAClD,OAAO,cAAc,CAAC,MAAM,cAAc,CAAC;AAC7C;;;;;;;AAQA,SAAS,SAAS,OAAsD;CACt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