smol-toml 1.3.2 → 1.3.3

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/dist/date.d.ts ADDED
@@ -0,0 +1,41 @@
1
+ /*!
2
+ * Copyright (c) Squirrel Chat et al., All rights reserved.
3
+ * SPDX-License-Identifier: BSD-3-Clause
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions are met:
7
+ *
8
+ * 1. Redistributions of source code must retain the above copyright notice, this
9
+ * list of conditions and the following disclaimer.
10
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
11
+ * this list of conditions and the following disclaimer in the
12
+ * documentation and/or other materials provided with the distribution.
13
+ * 3. Neither the name of the copyright holder nor the names of its contributors
14
+ * may be used to endorse or promote products derived from this software without
15
+ * specific prior written permission.
16
+ *
17
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
+ */
28
+ export declare class TomlDate extends Date {
29
+ #private;
30
+ constructor(date: string | Date);
31
+ isDateTime(): boolean;
32
+ isLocal(): boolean;
33
+ isDate(): boolean;
34
+ isTime(): boolean;
35
+ isValid(): boolean;
36
+ toISOString(): string;
37
+ static wrapAsOffsetDateTime(jsDate: Date, offset?: string): TomlDate;
38
+ static wrapAsLocalDateTime(jsDate: Date): TomlDate;
39
+ static wrapAsLocalDate(jsDate: Date): TomlDate;
40
+ static wrapAsLocalTime(jsDate: Date): TomlDate;
41
+ }
package/dist/date.js ADDED
@@ -0,0 +1,125 @@
1
+ /*!
2
+ * Copyright (c) Squirrel Chat et al., All rights reserved.
3
+ * SPDX-License-Identifier: BSD-3-Clause
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions are met:
7
+ *
8
+ * 1. Redistributions of source code must retain the above copyright notice, this
9
+ * list of conditions and the following disclaimer.
10
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
11
+ * this list of conditions and the following disclaimer in the
12
+ * documentation and/or other materials provided with the distribution.
13
+ * 3. Neither the name of the copyright holder nor the names of its contributors
14
+ * may be used to endorse or promote products derived from this software without
15
+ * specific prior written permission.
16
+ *
17
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
+ */
28
+ let DATE_TIME_RE = /^(\d{4}-\d{2}-\d{2})?[T ]?(?:(\d{2}):\d{2}:\d{2}(?:\.\d+)?)?(Z|[-+]\d{2}:\d{2})?$/i;
29
+ export class TomlDate extends Date {
30
+ #hasDate = false;
31
+ #hasTime = false;
32
+ #offset = null;
33
+ constructor(date) {
34
+ let hasDate = true;
35
+ let hasTime = true;
36
+ let offset = 'Z';
37
+ if (typeof date === 'string') {
38
+ let match = date.match(DATE_TIME_RE);
39
+ if (match) {
40
+ if (!match[1]) {
41
+ hasDate = false;
42
+ date = `0000-01-01T${date}`;
43
+ }
44
+ hasTime = !!match[2];
45
+ // Do not allow rollover hours
46
+ if (match[2] && +match[2] > 23) {
47
+ date = '';
48
+ }
49
+ else {
50
+ offset = match[3] || null;
51
+ date = date.toUpperCase();
52
+ if (!offset && hasTime)
53
+ date += 'Z';
54
+ }
55
+ }
56
+ else {
57
+ date = '';
58
+ }
59
+ }
60
+ super(date);
61
+ if (!isNaN(this.getTime())) {
62
+ this.#hasDate = hasDate;
63
+ this.#hasTime = hasTime;
64
+ this.#offset = offset;
65
+ }
66
+ }
67
+ isDateTime() {
68
+ return this.#hasDate && this.#hasTime;
69
+ }
70
+ isLocal() {
71
+ return !this.#hasDate || !this.#hasTime || !this.#offset;
72
+ }
73
+ isDate() {
74
+ return this.#hasDate && !this.#hasTime;
75
+ }
76
+ isTime() {
77
+ return this.#hasTime && !this.#hasDate;
78
+ }
79
+ isValid() {
80
+ return this.#hasDate || this.#hasTime;
81
+ }
82
+ toISOString() {
83
+ let iso = super.toISOString();
84
+ // Local Date
85
+ if (this.isDate())
86
+ return iso.slice(0, 10);
87
+ // Local Time
88
+ if (this.isTime())
89
+ return iso.slice(11, 23);
90
+ // Local DateTime
91
+ if (this.#offset === null)
92
+ return iso.slice(0, -1);
93
+ // Offset DateTime
94
+ if (this.#offset === 'Z')
95
+ return iso;
96
+ // This part is quite annoying: JS strips the original timezone from the ISO string representation
97
+ // Instead of using a "modified" date and "Z", we restore the representation "as authored"
98
+ let offset = (+(this.#offset.slice(1, 3)) * 60) + +(this.#offset.slice(4, 6));
99
+ offset = this.#offset[0] === '-' ? offset : -offset;
100
+ let offsetDate = new Date(this.getTime() - (offset * 60e3));
101
+ return offsetDate.toISOString().slice(0, -1) + this.#offset;
102
+ }
103
+ static wrapAsOffsetDateTime(jsDate, offset = 'Z') {
104
+ let date = new TomlDate(jsDate);
105
+ date.#offset = offset;
106
+ return date;
107
+ }
108
+ static wrapAsLocalDateTime(jsDate) {
109
+ let date = new TomlDate(jsDate);
110
+ date.#offset = null;
111
+ return date;
112
+ }
113
+ static wrapAsLocalDate(jsDate) {
114
+ let date = new TomlDate(jsDate);
115
+ date.#hasTime = false;
116
+ date.#offset = null;
117
+ return date;
118
+ }
119
+ static wrapAsLocalTime(jsDate) {
120
+ let date = new TomlDate(jsDate);
121
+ date.#hasDate = false;
122
+ date.#offset = null;
123
+ return date;
124
+ }
125
+ }
@@ -0,0 +1,38 @@
1
+ /*!
2
+ * Copyright (c) Squirrel Chat et al., All rights reserved.
3
+ * SPDX-License-Identifier: BSD-3-Clause
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions are met:
7
+ *
8
+ * 1. Redistributions of source code must retain the above copyright notice, this
9
+ * list of conditions and the following disclaimer.
10
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
11
+ * this list of conditions and the following disclaimer in the
12
+ * documentation and/or other materials provided with the distribution.
13
+ * 3. Neither the name of the copyright holder nor the names of its contributors
14
+ * may be used to endorse or promote products derived from this software without
15
+ * specific prior written permission.
16
+ *
17
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
+ */
28
+ type TomlErrorOptions = ErrorOptions & {
29
+ toml: string;
30
+ ptr: number;
31
+ };
32
+ export declare class TomlError extends Error {
33
+ line: number;
34
+ column: number;
35
+ codeblock: string;
36
+ constructor(message: string, options: TomlErrorOptions);
37
+ }
38
+ export {};
package/dist/error.js ADDED
@@ -0,0 +1,63 @@
1
+ /*!
2
+ * Copyright (c) Squirrel Chat et al., All rights reserved.
3
+ * SPDX-License-Identifier: BSD-3-Clause
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions are met:
7
+ *
8
+ * 1. Redistributions of source code must retain the above copyright notice, this
9
+ * list of conditions and the following disclaimer.
10
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
11
+ * this list of conditions and the following disclaimer in the
12
+ * documentation and/or other materials provided with the distribution.
13
+ * 3. Neither the name of the copyright holder nor the names of its contributors
14
+ * may be used to endorse or promote products derived from this software without
15
+ * specific prior written permission.
16
+ *
17
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
+ */
28
+ function getLineColFromPtr(string, ptr) {
29
+ let lines = string.slice(0, ptr).split(/\r\n|\n|\r/g);
30
+ return [lines.length, lines.pop().length + 1];
31
+ }
32
+ function makeCodeBlock(string, line, column) {
33
+ let lines = string.split(/\r\n|\n|\r/g);
34
+ let codeblock = '';
35
+ let numberLen = (Math.log10(line + 1) | 0) + 1;
36
+ for (let i = line - 1; i <= line + 1; i++) {
37
+ let l = lines[i - 1];
38
+ if (!l)
39
+ continue;
40
+ codeblock += i.toString().padEnd(numberLen, ' ');
41
+ codeblock += ': ';
42
+ codeblock += l;
43
+ codeblock += '\n';
44
+ if (i === line) {
45
+ codeblock += ' '.repeat(numberLen + column + 2);
46
+ codeblock += '^\n';
47
+ }
48
+ }
49
+ return codeblock;
50
+ }
51
+ export class TomlError extends Error {
52
+ line;
53
+ column;
54
+ codeblock;
55
+ constructor(message, options) {
56
+ const [line, column] = getLineColFromPtr(options.toml, options.ptr);
57
+ const codeblock = makeCodeBlock(options.toml, line, column);
58
+ super(`Invalid TOML document: ${message}\n\n${codeblock}`, options);
59
+ this.line = line;
60
+ this.column = column;
61
+ this.codeblock = codeblock;
62
+ }
63
+ }
@@ -0,0 +1,29 @@
1
+ /*!
2
+ * Copyright (c) Squirrel Chat et al., All rights reserved.
3
+ * SPDX-License-Identifier: BSD-3-Clause
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions are met:
7
+ *
8
+ * 1. Redistributions of source code must retain the above copyright notice, this
9
+ * list of conditions and the following disclaimer.
10
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
11
+ * this list of conditions and the following disclaimer in the
12
+ * documentation and/or other materials provided with the distribution.
13
+ * 3. Neither the name of the copyright holder nor the names of its contributors
14
+ * may be used to endorse or promote products derived from this software without
15
+ * specific prior written permission.
16
+ *
17
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
+ */
28
+ import { type TomlPrimitive } from './util.js';
29
+ export declare function extractValue(str: string, ptr: number, end?: string | undefined, depth?: number): [TomlPrimitive, number];
@@ -0,0 +1,109 @@
1
+ /*!
2
+ * Copyright (c) Squirrel Chat et al., All rights reserved.
3
+ * SPDX-License-Identifier: BSD-3-Clause
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions are met:
7
+ *
8
+ * 1. Redistributions of source code must retain the above copyright notice, this
9
+ * list of conditions and the following disclaimer.
10
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
11
+ * this list of conditions and the following disclaimer in the
12
+ * documentation and/or other materials provided with the distribution.
13
+ * 3. Neither the name of the copyright holder nor the names of its contributors
14
+ * may be used to endorse or promote products derived from this software without
15
+ * specific prior written permission.
16
+ *
17
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
+ */
28
+ import { parseString, parseValue } from './primitive.js';
29
+ import { parseArray, parseInlineTable } from './struct.js';
30
+ import { indexOfNewline, skipVoid, skipUntil, skipComment, getStringEnd } from './util.js';
31
+ import { TomlError } from './error.js';
32
+ function sliceAndTrimEndOf(str, startPtr, endPtr, allowNewLines) {
33
+ let value = str.slice(startPtr, endPtr);
34
+ let commentIdx = value.indexOf('#');
35
+ if (commentIdx > -1) {
36
+ // The call to skipComment allows to "validate" the comment
37
+ // (absence of control characters)
38
+ skipComment(str, commentIdx);
39
+ value = value.slice(0, commentIdx);
40
+ }
41
+ let trimmed = value.trimEnd();
42
+ if (!allowNewLines) {
43
+ let newlineIdx = value.indexOf('\n', trimmed.length);
44
+ if (newlineIdx > -1) {
45
+ throw new TomlError('newlines are not allowed in inline tables', {
46
+ toml: str,
47
+ ptr: startPtr + newlineIdx
48
+ });
49
+ }
50
+ }
51
+ return [trimmed, commentIdx];
52
+ }
53
+ export function extractValue(str, ptr, end, depth = -1) {
54
+ if (depth === 0) {
55
+ throw new TomlError('document contains excessively nested structures. aborting.', {
56
+ toml: str,
57
+ ptr: ptr
58
+ });
59
+ }
60
+ let c = str[ptr];
61
+ if (c === '[' || c === '{') {
62
+ let [value, endPtr] = c === '['
63
+ ? parseArray(str, ptr, depth)
64
+ : parseInlineTable(str, ptr, depth);
65
+ let newPtr = skipUntil(str, endPtr, ',', end);
66
+ if (end === '}') {
67
+ let nextNewLine = indexOfNewline(str, endPtr, newPtr);
68
+ if (nextNewLine > -1) {
69
+ throw new TomlError('newlines are not allowed in inline tables', {
70
+ toml: str,
71
+ ptr: nextNewLine
72
+ });
73
+ }
74
+ }
75
+ return [value, newPtr];
76
+ }
77
+ let endPtr;
78
+ if (c === '"' || c === "'") {
79
+ endPtr = getStringEnd(str, ptr);
80
+ let parsed = parseString(str, ptr, endPtr);
81
+ if (end) {
82
+ endPtr = skipVoid(str, endPtr, end !== ']');
83
+ if (str[endPtr] && str[endPtr] !== ',' && str[endPtr] !== end && str[endPtr] !== '\n' && str[endPtr] !== '\r') {
84
+ throw new TomlError('unexpected character encountered', {
85
+ toml: str,
86
+ ptr: endPtr,
87
+ });
88
+ }
89
+ endPtr += (+(str[endPtr] === ','));
90
+ }
91
+ return [parsed, endPtr];
92
+ }
93
+ endPtr = skipUntil(str, ptr, ',', end);
94
+ let slice = sliceAndTrimEndOf(str, ptr, endPtr - (+(str[endPtr - 1] === ',')), end === ']');
95
+ if (!slice[0]) {
96
+ throw new TomlError('incomplete key-value declaration: no value specified', {
97
+ toml: str,
98
+ ptr: ptr
99
+ });
100
+ }
101
+ if (end && slice[1] > -1) {
102
+ endPtr = skipVoid(str, ptr + slice[1]);
103
+ endPtr += +(str[endPtr] === ',');
104
+ }
105
+ return [
106
+ parseValue(slice[0], str, ptr),
107
+ endPtr,
108
+ ];
109
+ }