|
116 | 116 | <key>argumenttype</key> |
117 | 117 | <integer>1</integer> |
118 | 118 | <key>escaping</key> |
119 | | - <integer>68</integer> |
| 119 | + <integer>0</integer> |
120 | 120 | <key>keyword</key> |
121 | 121 | <string>{var:keyword}</string> |
122 | 122 | <key>queuedelaycustom</key> |
|
130 | 130 | <key>runningsubtext</key> |
131 | 131 | <string></string> |
132 | 132 | <key>script</key> |
133 | | - <string>function run(argv) { |
134 | | - const input = argv[0]; |
135 | | -
|
136 | | - const WORD = /[a-zA-Z0-9]+/g; |
137 | | - const COMMAND_SEPARATOR = '/'; |
138 | | -
|
139 | | - let items = []; |
140 | | -
|
141 | | - const toPascalCase = (string = '') => { |
142 | | - const words = string.match(WORD); |
143 | | - return (words || []) |
144 | | - .map((word) => `${word.charAt(0).toUpperCase()}${word.slice(1)}`) |
145 | | - .join(''); |
146 | | - }; |
147 | | -
|
148 | | - const toLowerCase = (string = '') => { |
149 | | - return string.toLowerCase(); |
150 | | - }; |
151 | | -
|
152 | | - const toUpperCase = (string = '') => { |
153 | | - return string.toUpperCase(); |
154 | | - }; |
155 | | -
|
156 | | - const toCamelCase = (string = '') => { |
157 | | - const words = string.match(WORD); |
158 | | - return (words || []).map((word, index) => { |
159 | | - if (index === 0) { |
160 | | - return `${word.charAt(0).toLowerCase()}${word.slice(1)}`; |
161 | | - } |
162 | | - return `${word.charAt(0).toUpperCase()}${word.slice(1)}`; |
163 | | - }).join(''); |
164 | | - }; |
165 | | -
|
166 | | - const toKebapCase = (string = '') => { |
167 | | - const words = string.match(WORD); |
168 | | - return (words || []).join('-'); |
169 | | - }; |
170 | | -
|
171 | | - const toSnakeCase = (string = '') => { |
172 | | - const words = string.match(WORD); |
173 | | - return (words || []).join('_'); |
174 | | - }; |
175 | | -
|
176 | | - const toTrimmed = (string = '') => { |
177 | | - return string.trim(); |
178 | | - }; |
179 | | -
|
180 | | - const toPrettyJSON = (string = '') => { |
181 | | - try { |
182 | | - return JSON.stringify(JSON.parse(string), null, 2); |
183 | | - } catch { |
184 | | - throw new Error('Invalid JSON'); |
185 | | - } |
186 | | - }; |
187 | | -
|
188 | | - const commands = { |
189 | | - l: { |
190 | | - name: 'Lowercase', |
191 | | - transform: toLowerCase, |
192 | | - }, |
193 | | - u: { |
194 | | - name: 'Uppercase', |
195 | | - transform: toUpperCase, |
196 | | - }, |
197 | | - c: { |
198 | | - name: 'Camelcase', |
199 | | - transform: toCamelCase, |
200 | | - }, |
201 | | - p: { |
202 | | - name: 'Pascalcase', |
203 | | - transform: toPascalCase, |
204 | | - }, |
205 | | - k: { |
206 | | - name: 'Kebapcase', |
207 | | - transform: toKebapCase, |
208 | | - }, |
209 | | - s: { |
210 | | - name: 'Snakecase', |
211 | | - transform: toSnakeCase, |
212 | | - }, |
213 | | - t: { |
214 | | - name: 'Trim', |
215 | | - transform: toTrimmed, |
216 | | - }, |
217 | | - j: { |
218 | | - name: 'Prettify JSON', |
219 | | - transform: toPrettyJSON, |
220 | | - }, |
221 | | - }; |
222 | | -
|
223 | | - const runTransforms = (input, commandsSequence) => { |
224 | | - if (Array.isArray(commandsSequence) && commandsSequence.length > 0) { |
225 | | - try { |
226 | | - const transformed = commandsSequence.reduce((result, command) => { |
227 | | - const transformer = commands[command]; |
228 | | - if (transformer) { |
229 | | - return transformer.transform(result); |
230 | | - } |
231 | | - return result |
232 | | - }, input); |
233 | | - return transformed; |
234 | | - } catch { |
235 | | - |
236 | | - } |
237 | | - } |
238 | | -
|
239 | | - return [input, []]; |
240 | | - }; |
241 | | -
|
242 | | - const getCommandSequencePath = (commandsSequence) => { |
243 | | - if (Array.isArray(commandsSequence) && commandsSequence.length > 0) { |
244 | | - return commandsSequence.reduce((result, command) => { |
245 | | - const transformer = commands[command]; |
246 | | - if (transformer) { |
247 | | - result.push(transformer.name); |
248 | | - } |
249 | | - return result |
250 | | - }, []); |
251 | | - } |
252 | | - }; |
253 | | -
|
254 | | - const isMultilined = (string = '') => /\n+/.test(string); |
255 | | -
|
256 | | - const inputSplitted = (input || '').split(COMMAND_SEPARATOR); |
257 | | -
|
258 | | - const string = inputSplitted.length > 2 |
259 | | - ? inputSplitted.slice(0, inputSplitted.length - 1).join(COMMAND_SEPARATOR) |
260 | | - : inputSplitted[0]; |
261 | | - const commandsSequence = inputSplitted[1] ? inputSplitted[1].split('') : undefined; |
262 | | -
|
263 | | - if (commandsSequence) { |
264 | | - const path = getCommandSequencePath(commandsSequence); |
265 | | - const subtitle = path.join('→'); |
266 | | - const icon = { |
267 | | - path: './Chained.png', |
268 | | - }; |
269 | | - try { |
270 | | - const chainResult = runTransforms(string, commandsSequence); |
271 | | - items = [{ |
272 | | - uid: 'chained', |
273 | | - title: isMultilined(chainResult) ? 'Multiline output' : chainResult, |
274 | | - subtitle, |
275 | | - arg: chainResult, |
276 | | - icon, |
277 | | - }]; |
278 | | - } catch { |
279 | | - items = [{ |
280 | | - uid: 'error', |
281 | | - title: 'Error', |
282 | | - subtitle, |
283 | | - arg: string, |
284 | | - icon, |
285 | | - }]; |
286 | | - } |
287 | | - } else { |
288 | | - items = Object.values(commands).map((command) => { |
289 | | - try { |
290 | | - const transformed = command.transform(string); |
291 | | -
|
292 | | - return { |
293 | | - uid: command.name.toLowerCase(), |
294 | | - title: isMultilined(transformed) ? 'Multiline output' : transformed, |
295 | | - subtitle: command.name, |
296 | | - arg: transformed, |
297 | | - icon: { |
298 | | - path: `./${command.name}.png`, |
299 | | - }, |
300 | | - }; |
301 | | - } catch { |
302 | | - return {}; |
303 | | - } |
304 | | - }); |
305 | | - } |
306 | | -
|
307 | | - return JSON.stringify({ |
308 | | - items, |
309 | | - }); |
310 | | -} |
311 | | -</string> |
| 133 | + <string></string> |
312 | 134 | <key>scriptargtype</key> |
313 | 135 | <integer>1</integer> |
314 | 136 | <key>scriptfile</key> |
315 | | - <string>./transform.js</string> |
| 137 | + <string>bundle/transform.js</string> |
316 | 138 | <key>subtext</key> |
317 | 139 | <string></string> |
318 | 140 | <key>title</key> |
|
0 commit comments