@ohos-ports/curlconverter 4.12.0-beta.0

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.
Files changed (270) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +153 -0
  3. package/dist/src/Headers.d.ts +23 -0
  4. package/dist/src/Headers.js +287 -0
  5. package/dist/src/Headers.js.map +1 -0
  6. package/dist/src/Query.d.ts +8 -0
  7. package/dist/src/Query.js +136 -0
  8. package/dist/src/Query.js.map +1 -0
  9. package/dist/src/Request.d.ts +200 -0
  10. package/dist/src/Request.js +1191 -0
  11. package/dist/src/Request.js.map +1 -0
  12. package/dist/src/Warnings.d.ts +15 -0
  13. package/dist/src/Warnings.js +111 -0
  14. package/dist/src/Warnings.js.map +1 -0
  15. package/dist/src/cli.d.ts +2 -0
  16. package/dist/src/cli.js +333 -0
  17. package/dist/src/cli.js.map +1 -0
  18. package/dist/src/curl/auth.d.ts +12 -0
  19. package/dist/src/curl/auth.js +40 -0
  20. package/dist/src/curl/auth.js.map +1 -0
  21. package/dist/src/curl/form.d.ts +28 -0
  22. package/dist/src/curl/form.js +218 -0
  23. package/dist/src/curl/form.js.map +1 -0
  24. package/dist/src/curl/opts.d.ts +2052 -0
  25. package/dist/src/curl/opts.js +1000 -0
  26. package/dist/src/curl/opts.js.map +1 -0
  27. package/dist/src/curl/url.d.ts +14 -0
  28. package/dist/src/curl/url.js +218 -0
  29. package/dist/src/curl/url.js.map +1 -0
  30. package/dist/src/generators/ansible.d.ts +5 -0
  31. package/dist/src/generators/ansible.js +282 -0
  32. package/dist/src/generators/ansible.js.map +1 -0
  33. package/dist/src/generators/c.d.ts +12 -0
  34. package/dist/src/generators/c.js +1230 -0
  35. package/dist/src/generators/c.js.map +1 -0
  36. package/dist/src/generators/cfml.d.ts +5 -0
  37. package/dist/src/generators/cfml.js +134 -0
  38. package/dist/src/generators/cfml.js.map +1 -0
  39. package/dist/src/generators/clojure.d.ts +7 -0
  40. package/dist/src/generators/clojure.js +436 -0
  41. package/dist/src/generators/clojure.js.map +1 -0
  42. package/dist/src/generators/csharp.d.ts +8 -0
  43. package/dist/src/generators/csharp.js +356 -0
  44. package/dist/src/generators/csharp.js.map +1 -0
  45. package/dist/src/generators/dart.d.ts +5 -0
  46. package/dist/src/generators/dart.js +273 -0
  47. package/dist/src/generators/dart.js.map +1 -0
  48. package/dist/src/generators/elixir.d.ts +8 -0
  49. package/dist/src/generators/elixir.js +296 -0
  50. package/dist/src/generators/elixir.js.map +1 -0
  51. package/dist/src/generators/go.d.ts +5 -0
  52. package/dist/src/generators/go.js +228 -0
  53. package/dist/src/generators/go.js.map +1 -0
  54. package/dist/src/generators/har.d.ts +7 -0
  55. package/dist/src/generators/har.js +115 -0
  56. package/dist/src/generators/har.js.map +1 -0
  57. package/dist/src/generators/http.d.ts +7 -0
  58. package/dist/src/generators/http.js +165 -0
  59. package/dist/src/generators/http.js.map +1 -0
  60. package/dist/src/generators/httpie.d.ts +5 -0
  61. package/dist/src/generators/httpie.js +446 -0
  62. package/dist/src/generators/httpie.js.map +1 -0
  63. package/dist/src/generators/java/httpurlconnection.d.ts +5 -0
  64. package/dist/src/generators/java/httpurlconnection.js +123 -0
  65. package/dist/src/generators/java/httpurlconnection.js.map +1 -0
  66. package/dist/src/generators/java/java.d.ts +8 -0
  67. package/dist/src/generators/java/java.js +247 -0
  68. package/dist/src/generators/java/java.js.map +1 -0
  69. package/dist/src/generators/java/jsoup.d.ts +5 -0
  70. package/dist/src/generators/java/jsoup.js +152 -0
  71. package/dist/src/generators/java/jsoup.js.map +1 -0
  72. package/dist/src/generators/java/okhttp.d.ts +5 -0
  73. package/dist/src/generators/java/okhttp.js +232 -0
  74. package/dist/src/generators/java/okhttp.js.map +1 -0
  75. package/dist/src/generators/javascript/axios.d.ts +5 -0
  76. package/dist/src/generators/javascript/axios.js +332 -0
  77. package/dist/src/generators/javascript/axios.js.map +1 -0
  78. package/dist/src/generators/javascript/got.d.ts +5 -0
  79. package/dist/src/generators/javascript/got.js +277 -0
  80. package/dist/src/generators/javascript/got.js.map +1 -0
  81. package/dist/src/generators/javascript/http.d.ts +8 -0
  82. package/dist/src/generators/javascript/http.js +183 -0
  83. package/dist/src/generators/javascript/http.js.map +1 -0
  84. package/dist/src/generators/javascript/javascript.d.ts +37 -0
  85. package/dist/src/generators/javascript/javascript.js +676 -0
  86. package/dist/src/generators/javascript/javascript.js.map +1 -0
  87. package/dist/src/generators/javascript/jquery.d.ts +15 -0
  88. package/dist/src/generators/javascript/jquery.js +297 -0
  89. package/dist/src/generators/javascript/jquery.js.map +1 -0
  90. package/dist/src/generators/javascript/ky.d.ts +7 -0
  91. package/dist/src/generators/javascript/ky.js +245 -0
  92. package/dist/src/generators/javascript/ky.js.map +1 -0
  93. package/dist/src/generators/javascript/request.d.ts +5 -0
  94. package/dist/src/generators/javascript/request.js +113 -0
  95. package/dist/src/generators/javascript/request.js.map +1 -0
  96. package/dist/src/generators/javascript/superagent.d.ts +10 -0
  97. package/dist/src/generators/javascript/superagent.js +301 -0
  98. package/dist/src/generators/javascript/superagent.js.map +1 -0
  99. package/dist/src/generators/javascript/xhr.d.ts +8 -0
  100. package/dist/src/generators/javascript/xhr.js +147 -0
  101. package/dist/src/generators/javascript/xhr.js.map +1 -0
  102. package/dist/src/generators/json.d.ts +45 -0
  103. package/dist/src/generators/json.js +197 -0
  104. package/dist/src/generators/json.js.map +1 -0
  105. package/dist/src/generators/julia.d.ts +8 -0
  106. package/dist/src/generators/julia.js +365 -0
  107. package/dist/src/generators/julia.js.map +1 -0
  108. package/dist/src/generators/kotlin.d.ts +8 -0
  109. package/dist/src/generators/kotlin.js +325 -0
  110. package/dist/src/generators/kotlin.js.map +1 -0
  111. package/dist/src/generators/lua.d.ts +5 -0
  112. package/dist/src/generators/lua.js +192 -0
  113. package/dist/src/generators/lua.js.map +1 -0
  114. package/dist/src/generators/matlab/common.d.ts +16 -0
  115. package/dist/src/generators/matlab/common.js +238 -0
  116. package/dist/src/generators/matlab/common.js.map +1 -0
  117. package/dist/src/generators/matlab/httpinterface.d.ts +2 -0
  118. package/dist/src/generators/matlab/httpinterface.js +211 -0
  119. package/dist/src/generators/matlab/httpinterface.js.map +1 -0
  120. package/dist/src/generators/matlab/matlab.d.ts +5 -0
  121. package/dist/src/generators/matlab/matlab.js +29 -0
  122. package/dist/src/generators/matlab/matlab.js.map +1 -0
  123. package/dist/src/generators/matlab/webservices.d.ts +2 -0
  124. package/dist/src/generators/matlab/webservices.js +191 -0
  125. package/dist/src/generators/matlab/webservices.js.map +1 -0
  126. package/dist/src/generators/objectivec.d.ts +8 -0
  127. package/dist/src/generators/objectivec.js +269 -0
  128. package/dist/src/generators/objectivec.js.map +1 -0
  129. package/dist/src/generators/ocaml.d.ts +8 -0
  130. package/dist/src/generators/ocaml.js +150 -0
  131. package/dist/src/generators/ocaml.js.map +1 -0
  132. package/dist/src/generators/perl.d.ts +9 -0
  133. package/dist/src/generators/perl.js +209 -0
  134. package/dist/src/generators/perl.js.map +1 -0
  135. package/dist/src/generators/php/guzzle.d.ts +5 -0
  136. package/dist/src/generators/php/guzzle.js +328 -0
  137. package/dist/src/generators/php/guzzle.js.map +1 -0
  138. package/dist/src/generators/php/php.d.ts +8 -0
  139. package/dist/src/generators/php/php.js +217 -0
  140. package/dist/src/generators/php/php.js.map +1 -0
  141. package/dist/src/generators/php/requests.d.ts +5 -0
  142. package/dist/src/generators/php/requests.js +89 -0
  143. package/dist/src/generators/php/requests.js.map +1 -0
  144. package/dist/src/generators/powershell.d.ts +11 -0
  145. package/dist/src/generators/powershell.js +377 -0
  146. package/dist/src/generators/powershell.js.map +1 -0
  147. package/dist/src/generators/python/http.d.ts +5 -0
  148. package/dist/src/generators/python/http.js +128 -0
  149. package/dist/src/generators/python/http.js.map +1 -0
  150. package/dist/src/generators/python/python.d.ts +22 -0
  151. package/dist/src/generators/python/python.js +1693 -0
  152. package/dist/src/generators/python/python.js.map +1 -0
  153. package/dist/src/generators/r/httr.d.ts +10 -0
  154. package/dist/src/generators/r/httr.js +298 -0
  155. package/dist/src/generators/r/httr.js.map +1 -0
  156. package/dist/src/generators/r/httr2.d.ts +5 -0
  157. package/dist/src/generators/r/httr2.js +214 -0
  158. package/dist/src/generators/r/httr2.js.map +1 -0
  159. package/dist/src/generators/ruby/httparty.d.ts +6 -0
  160. package/dist/src/generators/ruby/httparty.js +308 -0
  161. package/dist/src/generators/ruby/httparty.js.map +1 -0
  162. package/dist/src/generators/ruby/ruby.d.ts +14 -0
  163. package/dist/src/generators/ruby/ruby.js +515 -0
  164. package/dist/src/generators/ruby/ruby.js.map +1 -0
  165. package/dist/src/generators/rust.d.ts +8 -0
  166. package/dist/src/generators/rust.js +177 -0
  167. package/dist/src/generators/rust.js.map +1 -0
  168. package/dist/src/generators/swift.d.ts +8 -0
  169. package/dist/src/generators/swift.js +219 -0
  170. package/dist/src/generators/swift.js.map +1 -0
  171. package/dist/src/generators/wget.d.ts +8 -0
  172. package/dist/src/generators/wget.js +472 -0
  173. package/dist/src/generators/wget.js.map +1 -0
  174. package/dist/src/index.d.ts +53 -0
  175. package/dist/src/index.js +54 -0
  176. package/dist/src/index.js.map +1 -0
  177. package/dist/src/parse.d.ts +13 -0
  178. package/dist/src/parse.js +43 -0
  179. package/dist/src/parse.js.map +1 -0
  180. package/dist/src/shell/Parser.d.ts +4 -0
  181. package/dist/src/shell/Parser.js +6 -0
  182. package/dist/src/shell/Parser.js.map +1 -0
  183. package/dist/src/shell/Word.d.ts +48 -0
  184. package/dist/src/shell/Word.js +486 -0
  185. package/dist/src/shell/Word.js.map +1 -0
  186. package/dist/src/shell/tokenizer.d.ts +3 -0
  187. package/dist/src/shell/tokenizer.js +610 -0
  188. package/dist/src/shell/tokenizer.js.map +1 -0
  189. package/dist/src/shell/webParser.d.ts +4 -0
  190. package/dist/src/shell/webParser.js +15 -0
  191. package/dist/src/shell/webParser.js.map +1 -0
  192. package/dist/src/utils.d.ts +6 -0
  193. package/dist/src/utils.js +21 -0
  194. package/dist/src/utils.js.map +1 -0
  195. package/dist/tools/compare-requests.d.ts +2 -0
  196. package/dist/tools/compare-requests.js +448 -0
  197. package/dist/tools/compare-requests.js.map +1 -0
  198. package/dist/tools/gen-test.d.ts +2 -0
  199. package/dist/tools/gen-test.js +114 -0
  200. package/dist/tools/gen-test.js.map +1 -0
  201. package/dist/tree-sitter-bash.wasm +0 -0
  202. package/index.d.ts +3 -0
  203. package/package.json +97 -0
  204. package/src/Headers.ts +334 -0
  205. package/src/Query.ts +147 -0
  206. package/src/Request.ts +1649 -0
  207. package/src/Warnings.ts +154 -0
  208. package/src/cli.ts +561 -0
  209. package/src/curl/auth.ts +45 -0
  210. package/src/curl/form.ts +297 -0
  211. package/src/curl/opts.ts +1417 -0
  212. package/src/curl/url.ts +241 -0
  213. package/src/generators/ansible.ts +364 -0
  214. package/src/generators/c.ts +1371 -0
  215. package/src/generators/cfml.ts +155 -0
  216. package/src/generators/clojure.ts +521 -0
  217. package/src/generators/csharp.ts +383 -0
  218. package/src/generators/dart.ts +293 -0
  219. package/src/generators/elixir.ts +349 -0
  220. package/src/generators/go.ts +269 -0
  221. package/src/generators/har.ts +147 -0
  222. package/src/generators/http.ts +194 -0
  223. package/src/generators/httpie.ts +506 -0
  224. package/src/generators/java/httpurlconnection.ts +151 -0
  225. package/src/generators/java/java.ts +280 -0
  226. package/src/generators/java/jsoup.ts +172 -0
  227. package/src/generators/java/okhttp.ts +257 -0
  228. package/src/generators/javascript/axios.ts +395 -0
  229. package/src/generators/javascript/got.ts +362 -0
  230. package/src/generators/javascript/http.ts +232 -0
  231. package/src/generators/javascript/javascript.ts +825 -0
  232. package/src/generators/javascript/jquery.ts +360 -0
  233. package/src/generators/javascript/ky.ts +315 -0
  234. package/src/generators/javascript/request.ts +152 -0
  235. package/src/generators/javascript/superagent.ts +373 -0
  236. package/src/generators/javascript/xhr.ts +192 -0
  237. package/src/generators/json.ts +293 -0
  238. package/src/generators/julia.ts +398 -0
  239. package/src/generators/kotlin.ts +355 -0
  240. package/src/generators/lua.ts +209 -0
  241. package/src/generators/matlab/common.ts +281 -0
  242. package/src/generators/matlab/httpinterface.ts +284 -0
  243. package/src/generators/matlab/matlab.ts +39 -0
  244. package/src/generators/matlab/webservices.ts +263 -0
  245. package/src/generators/objectivec.ts +297 -0
  246. package/src/generators/ocaml.ts +168 -0
  247. package/src/generators/perl.ts +237 -0
  248. package/src/generators/php/guzzle.ts +368 -0
  249. package/src/generators/php/php.ts +239 -0
  250. package/src/generators/php/requests.ts +103 -0
  251. package/src/generators/powershell.ts +430 -0
  252. package/src/generators/python/http.ts +167 -0
  253. package/src/generators/python/python.ts +1934 -0
  254. package/src/generators/r/httr.ts +328 -0
  255. package/src/generators/r/httr2.ts +268 -0
  256. package/src/generators/ruby/httparty.ts +351 -0
  257. package/src/generators/ruby/ruby.ts +564 -0
  258. package/src/generators/rust.ts +243 -0
  259. package/src/generators/swift.ts +235 -0
  260. package/src/generators/wget.ts +533 -0
  261. package/src/index.ts +89 -0
  262. package/src/parse.ts +72 -0
  263. package/src/shell/Parser.ts +8 -0
  264. package/src/shell/Word.ts +531 -0
  265. package/src/shell/tokenizer.ts +736 -0
  266. package/src/shell/webParser.ts +18 -0
  267. package/src/utils.ts +25 -0
  268. package/tools/compare-requests.ts +571 -0
  269. package/tools/extract_curl_args.py +511 -0
  270. package/tools/gen-test.ts +129 -0
@@ -0,0 +1,531 @@
1
+ // Words act like strings. They're lists of characters, except some
2
+ // characters can be shell variables or expressions.
3
+ // They're implemented like this:
4
+ // ["foobar", {type: "variable", value: "baz", text: "$baz"}, "qux"]
5
+ // Except for the empty string [""], there should be no empty strings in the array.
6
+
7
+ import type { Parser } from "./Parser.js";
8
+ import { CCError } from "../utils.js";
9
+
10
+ export interface ShellToken {
11
+ type: "variable" | "command";
12
+ value: string;
13
+ text: string;
14
+ syntaxNode: Parser.SyntaxNode; // For error reporting
15
+ }
16
+ export type Token = string | ShellToken;
17
+
18
+ // TODO: Words should keep a list of operations that happened to them
19
+ // like .replace() so that we can generate code that also does that operation
20
+ // on the contents of the environment variable or the output of the command.
21
+ export class Word implements Iterable<Token> {
22
+ readonly tokens: Token[];
23
+
24
+ constructor(tokens?: string);
25
+ constructor(tokens?: Token[]);
26
+ constructor(tokens?: string | Token[]) {
27
+ if (typeof tokens === "string") {
28
+ tokens = [tokens];
29
+ }
30
+ if (tokens === undefined || tokens.length === 0) {
31
+ tokens = [""];
32
+ }
33
+
34
+ this.tokens = [];
35
+ for (const t of tokens) {
36
+ if (typeof t === "string") {
37
+ if (
38
+ this.tokens.length > 0 &&
39
+ typeof this.tokens[this.tokens.length - 1] === "string"
40
+ ) {
41
+ // If we have 2+ strings in a row, merge them
42
+ this.tokens[this.tokens.length - 1] += t;
43
+ } else if (t) {
44
+ // skip empty strings
45
+ this.tokens.push(t);
46
+ }
47
+ } else {
48
+ this.tokens.push(t);
49
+ }
50
+ }
51
+ if (this.tokens.length === 0) {
52
+ this.tokens.push("");
53
+ }
54
+ }
55
+
56
+ get length(): number {
57
+ let len = 0;
58
+ for (const t of this.tokens) {
59
+ if (typeof t === "string") {
60
+ len += t.length;
61
+ } else {
62
+ len += 1;
63
+ }
64
+ }
65
+ return len;
66
+ }
67
+
68
+ *[Symbol.iterator](): Iterator<Token> {
69
+ for (const t of this.tokens) {
70
+ if (typeof t === "string") {
71
+ for (const c of t) {
72
+ yield c;
73
+ }
74
+ } else {
75
+ yield t;
76
+ }
77
+ }
78
+ }
79
+
80
+ // TODO: do we need this function?
81
+ get(index: number): Token {
82
+ let i = 0;
83
+ for (const t of this.tokens) {
84
+ if (typeof t === "string") {
85
+ if (i + t.length > index) {
86
+ return t[index - i];
87
+ }
88
+ i += t.length;
89
+ } else {
90
+ if (i === index) {
91
+ return t;
92
+ }
93
+ i += 1;
94
+ }
95
+ }
96
+ throw new CCError("Index out of bounds");
97
+ }
98
+
99
+ charAt(index = 0): Token {
100
+ try {
101
+ return this.get(index);
102
+ } catch {}
103
+ return "";
104
+ }
105
+
106
+ indexOf(search: string, start?: number): number {
107
+ if (start === undefined) {
108
+ start = 0;
109
+ }
110
+ let i = 0;
111
+ for (const t of this.tokens) {
112
+ if (typeof t === "string") {
113
+ if (i + t.length > start) {
114
+ const index = t.indexOf(search, start - i);
115
+ if (index !== -1) {
116
+ return i + index;
117
+ }
118
+ }
119
+ i += t.length;
120
+ } else {
121
+ i += 1;
122
+ }
123
+ }
124
+ return -1;
125
+ }
126
+
127
+ // Like indexOf() but accepts a string of characters and returns the index of the first one
128
+ // it finds
129
+ indexOfFirstChar(search: string): number {
130
+ let i = 0;
131
+ for (const t of this.tokens) {
132
+ if (typeof t === "string") {
133
+ for (const c of t) {
134
+ if (search.includes(c)) {
135
+ return i;
136
+ }
137
+ i += 1;
138
+ }
139
+ } else {
140
+ i += 1;
141
+ }
142
+ }
143
+ return -1;
144
+ }
145
+
146
+ removeFirstChar(c: string): Word {
147
+ if (this.length === 0) {
148
+ return new Word();
149
+ }
150
+ if (this.charAt(0) === c) {
151
+ return this.slice(1);
152
+ }
153
+ return this.copy();
154
+ }
155
+
156
+ copy(): Word {
157
+ return new Word(this.tokens);
158
+ }
159
+
160
+ slice(indexStart?: number, indexEnd?: number): Word {
161
+ if (indexStart === undefined) {
162
+ indexStart = this.length;
163
+ }
164
+ if (indexEnd === undefined) {
165
+ indexEnd = this.length;
166
+ }
167
+ if (indexStart >= this.length) {
168
+ return new Word();
169
+ }
170
+ if (indexStart < 0) {
171
+ indexStart = Math.max(indexStart + this.length, 0);
172
+ }
173
+ if (indexEnd < 0) {
174
+ indexEnd = Math.max(indexEnd + this.length, 0);
175
+ }
176
+ if (indexEnd <= indexStart) {
177
+ return new Word();
178
+ }
179
+
180
+ const ret = [];
181
+ let i = 0;
182
+ for (const t of this.tokens) {
183
+ if (typeof t === "string") {
184
+ if (i + t.length > indexStart) {
185
+ if (i < indexEnd) {
186
+ ret.push(t.slice(Math.max(indexStart - i, 0), indexEnd - i));
187
+ }
188
+ }
189
+ i += t.length;
190
+ } else {
191
+ if (i >= indexStart && i < indexEnd) {
192
+ ret.push(t);
193
+ }
194
+ i += 1;
195
+ }
196
+ }
197
+ return new Word(ret);
198
+ }
199
+
200
+ // TODO: check
201
+ includes(search: string, start?: number): boolean {
202
+ if (start === undefined) {
203
+ start = 0;
204
+ }
205
+ let i = 0;
206
+ for (const t of this.tokens) {
207
+ if (typeof t === "string") {
208
+ if (i + t.length > start) {
209
+ if (t.includes(search, start - i)) {
210
+ return true;
211
+ }
212
+ }
213
+ i += t.length;
214
+ } else {
215
+ i += 1;
216
+ }
217
+ }
218
+ return false;
219
+ }
220
+
221
+ test(search: RegExp): boolean {
222
+ for (const t of this.tokens) {
223
+ if (typeof t === "string") {
224
+ if (search.test(t)) {
225
+ return true;
226
+ }
227
+ }
228
+ }
229
+ return false;
230
+ }
231
+
232
+ prepend(c: string): Word {
233
+ const ret = this.copy();
234
+ if (ret.tokens.length && typeof ret.tokens[0] === "string") {
235
+ ret.tokens[0] = c + ret.tokens[0];
236
+ } else {
237
+ ret.tokens.unshift(c);
238
+ }
239
+ return ret;
240
+ }
241
+
242
+ append(c: string): Word {
243
+ const ret = this.copy();
244
+ if (
245
+ ret.tokens.length &&
246
+ typeof ret.tokens[ret.tokens.length - 1] === "string"
247
+ ) {
248
+ ret.tokens[ret.tokens.length - 1] += c;
249
+ } else {
250
+ ret.tokens.push(c);
251
+ }
252
+ return ret;
253
+ }
254
+
255
+ // Merges two Words
256
+ add(other: Word): Word {
257
+ return new Word([...this.tokens, ...other.tokens]);
258
+ }
259
+
260
+ // Returns the first match, searches each string independently
261
+ // TODO: improve this
262
+ match(regex: RegExp): RegExpMatchArray | null {
263
+ for (const t of this.tokens) {
264
+ if (typeof t === "string") {
265
+ const match = t.match(regex);
266
+ if (match) {
267
+ return match;
268
+ }
269
+ }
270
+ }
271
+ return null;
272
+ }
273
+
274
+ search(regex: RegExp): number {
275
+ let offset = 0;
276
+ for (const t of this.tokens) {
277
+ if (typeof t === "string") {
278
+ const match = t.search(regex);
279
+ if (match !== -1) {
280
+ return offset + match;
281
+ }
282
+ offset += t.length;
283
+ }
284
+ }
285
+ return -1;
286
+ }
287
+
288
+ // .replace() is called per-string, so it won't work through shell variables
289
+ replace(search: string | RegExp, replacement: string): Word {
290
+ const ret: Token[] = [];
291
+ for (const t of this.tokens) {
292
+ if (typeof t === "string") {
293
+ ret.push(t.replace(search, replacement));
294
+ } else {
295
+ ret.push(t);
296
+ }
297
+ }
298
+ return new Word(ret);
299
+ }
300
+
301
+ // splits correctly, not like String.split()
302
+ // The last entry can contain the separator if limit entries has been reached
303
+ split(separator: string, limit?: number): Word[] {
304
+ const ret: Word[] = [];
305
+ let i = 0;
306
+ let start = 0;
307
+ while (i < this.length) {
308
+ let match = true;
309
+ for (let j = 0; j < separator.length; j++) {
310
+ if (this.get(i + j) !== separator.charAt(j)) {
311
+ match = false;
312
+ break;
313
+ }
314
+ }
315
+ if (match) {
316
+ ret.push(this.slice(start, i));
317
+ i += separator.length;
318
+ start = i;
319
+ if (limit !== undefined && ret.length === limit - 1) {
320
+ break;
321
+ }
322
+ } else {
323
+ i += 1;
324
+ }
325
+ }
326
+ if (start <= this.length) {
327
+ ret.push(this.slice(start));
328
+ }
329
+ return ret;
330
+ }
331
+
332
+ toLowerCase(): Word {
333
+ return new Word(
334
+ this.tokens.map((t) => (typeof t === "string" ? t.toLowerCase() : t)),
335
+ );
336
+ }
337
+
338
+ toUpperCase(): Word {
339
+ return new Word(
340
+ this.tokens.map((t) => (typeof t === "string" ? t.toUpperCase() : t)),
341
+ );
342
+ }
343
+
344
+ trimStart(): Word {
345
+ const ret: Token[] = [];
346
+ let i, t;
347
+ for ([i, t] of this.tokens.entries()) {
348
+ if (typeof t === "string") {
349
+ if (i === 0) {
350
+ t = t.trimStart();
351
+ }
352
+ if (t) {
353
+ ret.push(t);
354
+ }
355
+ } else {
356
+ ret.push(t);
357
+ }
358
+ }
359
+ if (ret.length === 0) {
360
+ return new Word();
361
+ }
362
+ return new Word(ret);
363
+ }
364
+
365
+ trimEnd(): Word {
366
+ const ret: Token[] = [];
367
+ let i, t;
368
+ for ([i, t] of this.tokens.entries()) {
369
+ if (typeof t === "string") {
370
+ if (i === this.tokens.length - 1) {
371
+ t = t.trimEnd();
372
+ }
373
+ if (t) {
374
+ ret.push(t);
375
+ }
376
+ } else {
377
+ ret.push(t);
378
+ }
379
+ }
380
+ if (ret.length === 0) {
381
+ return new Word();
382
+ }
383
+ return new Word(ret);
384
+ }
385
+
386
+ trim(): Word {
387
+ const ret: Token[] = [];
388
+ let i, t;
389
+ for ([i, t] of this.tokens.entries()) {
390
+ if (typeof t === "string") {
391
+ if (i === 0) {
392
+ t = t.trimStart();
393
+ }
394
+ if (i === this.tokens.length - 1) {
395
+ t = t.trimEnd();
396
+ }
397
+ if (t) {
398
+ ret.push(t);
399
+ }
400
+ } else {
401
+ ret.push(t);
402
+ }
403
+ }
404
+ if (ret.length === 0) {
405
+ return new Word();
406
+ }
407
+ return new Word(ret);
408
+ }
409
+
410
+ isEmpty(): boolean {
411
+ if (this.tokens.length === 0) {
412
+ return true;
413
+ }
414
+ if (this.tokens.length === 1 && typeof this.tokens[0] === "string") {
415
+ return this.tokens[0].length === 0;
416
+ }
417
+ return false;
418
+ }
419
+
420
+ toBool(): boolean {
421
+ return !this.isEmpty();
422
+ }
423
+
424
+ // Returns true if .tokens contains no variables/commands
425
+ isString(): boolean {
426
+ for (const t of this.tokens) {
427
+ if (typeof t !== "string") {
428
+ return false;
429
+ }
430
+ }
431
+ return true;
432
+ }
433
+
434
+ firstShellToken(): ShellToken | null {
435
+ for (const t of this.tokens) {
436
+ if (typeof t !== "string") {
437
+ return t;
438
+ }
439
+ }
440
+ return null;
441
+ }
442
+
443
+ startsWith(prefix: string): boolean {
444
+ if (this.tokens.length === 0) {
445
+ return false;
446
+ }
447
+ if (typeof this.tokens[0] === "string") {
448
+ return this.tokens[0].startsWith(prefix);
449
+ }
450
+ return false;
451
+ }
452
+
453
+ endsWith(suffix: string): boolean {
454
+ if (this.tokens.length === 0) {
455
+ return false;
456
+ }
457
+ const lastToken = this.tokens[this.tokens.length - 1];
458
+ if (typeof lastToken === "string") {
459
+ return lastToken.endsWith(suffix);
460
+ }
461
+ return false;
462
+ }
463
+
464
+ // This destroys the information about the original tokenization
465
+ toString() {
466
+ return this.tokens
467
+ .map((t) => (typeof t === "string" ? t : t.text))
468
+ .join("");
469
+ }
470
+ valueOf = Word.toString;
471
+ }
472
+
473
+ export function eq(
474
+ it: Word | undefined | null,
475
+ other: string | Word | undefined | null,
476
+ ): boolean {
477
+ if (
478
+ it === undefined ||
479
+ it === null ||
480
+ other === undefined ||
481
+ other === null
482
+ ) {
483
+ return it === other;
484
+ }
485
+ if (typeof other === "string") {
486
+ return (
487
+ it.tokens.length === 1 &&
488
+ typeof it.tokens[0] === "string" &&
489
+ it.tokens[0] === other
490
+ );
491
+ }
492
+ return (
493
+ it.tokens.length === other.tokens.length &&
494
+ it.tokens.every((itToken, i) => {
495
+ const otherToken = other.tokens[i];
496
+ if (typeof itToken === "string") {
497
+ return itToken === otherToken;
498
+ } else if (typeof otherToken !== "string") {
499
+ return itToken.text === otherToken.text;
500
+ }
501
+ return false;
502
+ })
503
+ );
504
+ }
505
+ export function firstShellToken(word: string | Word): ShellToken | null {
506
+ if (typeof word === "string") {
507
+ return null;
508
+ }
509
+ return word.firstShellToken();
510
+ }
511
+ export function mergeWords(...words: (Word | string)[]): Word {
512
+ const ret: Token[] = [];
513
+ for (const w of words) {
514
+ if (w instanceof Word) {
515
+ ret.push(...w.tokens);
516
+ } else {
517
+ ret.push(w);
518
+ }
519
+ }
520
+ return new Word(ret);
521
+ }
522
+ export function joinWords(words: Word[], joinChar: string): Word {
523
+ const ret: Token[] = [];
524
+ for (const w of words) {
525
+ if (ret.length) {
526
+ ret.push(joinChar);
527
+ }
528
+ ret.push(...w.tokens);
529
+ }
530
+ return new Word(ret);
531
+ }