@xsai/stream-object 0.5.0-beta.1 → 0.5.0-beta.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/index.d.ts +25 -0
- package/dist/index.js +124 -1
- package/package.json +5 -5
package/dist/index.d.ts
CHANGED
|
@@ -603,6 +603,31 @@ type SimpleMerge<Destination, Source> = Simplify<{
|
|
|
603
603
|
/**
|
|
604
604
|
Merge two types into a new type. Keys of the second type overrides keys of the first type.
|
|
605
605
|
|
|
606
|
+
This is different from the TypeScript `&` (intersection) operator. With `&`, conflicting property types are intersected, which often results in `never`. For example, `{a: string} & {a: number}` makes `a` become `string & number`, which resolves to `never`. With `Merge`, the second type's keys cleanly override the first, so `Merge<{a: string}, {a: number}>` gives `{a: number}` as expected. `Merge` also produces a flattened type (via `Simplify`), making it more readable in IDE tooltips compared to `A & B`.
|
|
607
|
+
|
|
608
|
+
@example
|
|
609
|
+
```
|
|
610
|
+
import type {Merge} from 'type-fest';
|
|
611
|
+
|
|
612
|
+
type Foo = {
|
|
613
|
+
a: string;
|
|
614
|
+
b: number;
|
|
615
|
+
};
|
|
616
|
+
|
|
617
|
+
type Bar = {
|
|
618
|
+
a: number; // Conflicts with Foo['a']
|
|
619
|
+
c: boolean;
|
|
620
|
+
};
|
|
621
|
+
|
|
622
|
+
// With `&`, `a` becomes `string & number` which is `never`. Not what you want.
|
|
623
|
+
type WithIntersection = (Foo & Bar)['a'];
|
|
624
|
+
//=> never
|
|
625
|
+
|
|
626
|
+
// With `Merge`, `a` is cleanly overridden to `number`.
|
|
627
|
+
type WithMerge = Merge<Foo, Bar>['a'];
|
|
628
|
+
//=> number
|
|
629
|
+
```
|
|
630
|
+
|
|
606
631
|
@example
|
|
607
632
|
```
|
|
608
633
|
import type {Merge} from 'type-fest';
|
package/dist/index.js
CHANGED
|
@@ -21,7 +21,7 @@ function requireParse () {
|
|
|
21
21
|
hasRequiredParse = 1;
|
|
22
22
|
(function (exports$1) {
|
|
23
23
|
Object.defineProperty(exports$1, "__esModule", { value: true });
|
|
24
|
-
exports$1.parse = exports$1.enableErrorLogging = exports$1.disableErrorLogging = exports$1.setErrorLogger = void 0;
|
|
24
|
+
exports$1.parse = exports$1.stripComments = exports$1.enableErrorLogging = exports$1.disableErrorLogging = exports$1.setErrorLogger = void 0;
|
|
25
25
|
let logError = console.error;
|
|
26
26
|
function setErrorLogger(logger) {
|
|
27
27
|
logError = logger;
|
|
@@ -36,6 +36,99 @@ function requireParse () {
|
|
|
36
36
|
logError = console.error;
|
|
37
37
|
}
|
|
38
38
|
exports$1.enableErrorLogging = enableErrorLogging;
|
|
39
|
+
function stripComments(text) {
|
|
40
|
+
const buffer = [];
|
|
41
|
+
let in_string = false;
|
|
42
|
+
let string_char = "";
|
|
43
|
+
let string_escaped = false;
|
|
44
|
+
let in_inline_comment = false;
|
|
45
|
+
let in_block_comment = false;
|
|
46
|
+
let saw_star = false;
|
|
47
|
+
let in_html_comment = false;
|
|
48
|
+
let saw_hyphen = 0;
|
|
49
|
+
for (const char of text) {
|
|
50
|
+
if (in_string) {
|
|
51
|
+
if (string_escaped) {
|
|
52
|
+
string_escaped = false;
|
|
53
|
+
buffer.push(char);
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
if (char === "\\") {
|
|
57
|
+
string_escaped = true;
|
|
58
|
+
buffer.push(char);
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (char === string_char) {
|
|
62
|
+
in_string = false;
|
|
63
|
+
buffer.push(char);
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
buffer.push(char);
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
if (in_inline_comment) {
|
|
70
|
+
if (char === "\n") {
|
|
71
|
+
in_inline_comment = false;
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
const buffer_length = buffer.length;
|
|
77
|
+
const last_char = buffer_length === 0 ? "" : buffer[buffer_length - 1];
|
|
78
|
+
if (in_block_comment) {
|
|
79
|
+
if (char === "*") {
|
|
80
|
+
saw_star = true;
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
if (saw_star && char === "/") {
|
|
84
|
+
in_block_comment = false;
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
saw_star = false;
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
if (in_html_comment) {
|
|
91
|
+
if (char === "-") {
|
|
92
|
+
saw_hyphen++;
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
if (saw_hyphen >= 2 && char === ">") {
|
|
96
|
+
in_html_comment = false;
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
saw_hyphen = 0;
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
if (last_char === "/" && char === "/") {
|
|
103
|
+
buffer.pop();
|
|
104
|
+
in_inline_comment = true;
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
if (last_char === "/" && char === "*") {
|
|
108
|
+
buffer.pop();
|
|
109
|
+
in_block_comment = true;
|
|
110
|
+
saw_star = false;
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
if (char === '"' || char === "'" || char === "`") {
|
|
114
|
+
in_string = true;
|
|
115
|
+
string_char = char;
|
|
116
|
+
string_escaped = false;
|
|
117
|
+
buffer.push(char);
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
if (buffer_length >= 4 && char === "-" && buffer[buffer_length - 1] === "-" && buffer[buffer_length - 2] === "!" && buffer[buffer_length - 3] === "<") {
|
|
121
|
+
in_html_comment = true;
|
|
122
|
+
buffer.pop();
|
|
123
|
+
buffer.pop();
|
|
124
|
+
buffer.pop();
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
buffer.push(char);
|
|
128
|
+
}
|
|
129
|
+
return buffer.join("");
|
|
130
|
+
}
|
|
131
|
+
exports$1.stripComments = stripComments;
|
|
39
132
|
function parse(s) {
|
|
40
133
|
if (s === void 0) {
|
|
41
134
|
return void 0;
|
|
@@ -46,6 +139,7 @@ function requireParse () {
|
|
|
46
139
|
if (s === "") {
|
|
47
140
|
return "";
|
|
48
141
|
}
|
|
142
|
+
s = stripComments(s);
|
|
49
143
|
s = s.replace(/\\+$/, (match) => match.length % 2 === 0 ? match : match.slice(0, -1));
|
|
50
144
|
try {
|
|
51
145
|
return JSON.parse(s);
|
|
@@ -87,6 +181,9 @@ function requireParse () {
|
|
|
87
181
|
if (s[0] === "'") {
|
|
88
182
|
return parseSingleQuoteString(s);
|
|
89
183
|
}
|
|
184
|
+
if (s[0] === "`") {
|
|
185
|
+
return parseBacktickString(s);
|
|
186
|
+
}
|
|
90
187
|
return parseStringWithoutQuote(s, e, delimiters);
|
|
91
188
|
}
|
|
92
189
|
const parsers = {};
|
|
@@ -182,6 +279,32 @@ function requireParse () {
|
|
|
182
279
|
}
|
|
183
280
|
return [JSON.parse('"' + fixEscapedCharacters(s.slice(1)) + '"'), ""];
|
|
184
281
|
}
|
|
282
|
+
parsers["`"] = parseBacktickString;
|
|
283
|
+
function parseBacktickString(s) {
|
|
284
|
+
const buffer = [];
|
|
285
|
+
let is_escaped = false;
|
|
286
|
+
let escape_count = 0;
|
|
287
|
+
for (let i = 1; i < s.length; i++) {
|
|
288
|
+
const c = s[i];
|
|
289
|
+
if (is_escaped) {
|
|
290
|
+
buffer.push(c);
|
|
291
|
+
is_escaped = false;
|
|
292
|
+
continue;
|
|
293
|
+
}
|
|
294
|
+
if (c === "\\") {
|
|
295
|
+
is_escaped = true;
|
|
296
|
+
escape_count++;
|
|
297
|
+
continue;
|
|
298
|
+
}
|
|
299
|
+
if (c === "`") {
|
|
300
|
+
const str = buffer.join("");
|
|
301
|
+
s = s.substring(str.length + escape_count + 2);
|
|
302
|
+
return [str, s];
|
|
303
|
+
}
|
|
304
|
+
buffer.push(c);
|
|
305
|
+
}
|
|
306
|
+
return [buffer.join(""), s.slice(1)];
|
|
307
|
+
}
|
|
185
308
|
function parseStringWithoutQuote(s, e, delimiters = [" "]) {
|
|
186
309
|
const index = Math.min(...delimiters.map((delimiter) => {
|
|
187
310
|
const index2 = s.indexOf(delimiter);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xsai/stream-object",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.5.0-beta.
|
|
4
|
+
"version": "0.5.0-beta.3",
|
|
5
5
|
"description": "extra-small AI SDK.",
|
|
6
6
|
"author": "Moeru AI",
|
|
7
7
|
"license": "MIT",
|
|
@@ -29,13 +29,13 @@
|
|
|
29
29
|
"dist"
|
|
30
30
|
],
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@xsai/stream-text": "
|
|
33
|
-
"xsschema": "
|
|
32
|
+
"@xsai/stream-text": "0.5.0-beta.3",
|
|
33
|
+
"xsschema": "0.5.0-beta.3"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"@valibot/to-json-schema": "^1.0.0",
|
|
37
|
-
"best-effort-json-parser": "^1.
|
|
38
|
-
"type-fest": "^5.
|
|
37
|
+
"best-effort-json-parser": "^1.4.0",
|
|
38
|
+
"type-fest": "^5.5.0",
|
|
39
39
|
"valibot": "^1.0.0"
|
|
40
40
|
},
|
|
41
41
|
"scripts": {
|