Skip to content

Commit c6be64c

Browse files
authored
fix: preserve options with quoted strings (#565)
1 parent 738551b commit c6be64c

File tree

3 files changed

+65
-3
lines changed

3 files changed

+65
-3
lines changed

src/FFmpeggy.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import TypedEmitter from "typed-emitter";
88
import { parseInfo, parseWriting, parseProgress } from "./parsers";
99
import { FFmpeggyProgress } from "./types/FFmpeggyProgress";
1010
import { FFprobeResult } from "./types/probeTypes";
11+
import { parseOptions } from "./utils/parseOptions";
1112

1213
export interface FFmpeggyOptions {
1314
cwd?: string;
@@ -155,10 +156,10 @@ export class FFmpeggy extends (EventEmitter as new () => TypedEmitter<FFmpegEven
155156
}
156157

157158
const args = [
158-
...globalOptions.join(" ").split(" "),
159-
...inputOptions.join(" ").split(" "),
159+
...parseOptions(globalOptions),
160+
...parseOptions(inputOptions),
160161
...["-i", ffmpegInput],
161-
...outputOptions.join(" ").split(" "),
162+
...parseOptions(outputOptions),
162163
ffmpegOutput,
163164
].filter((a) => !!a);
164165

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { parseOptions } from "../parseOptions";
2+
3+
describe("parseOptions", () => {
4+
it("should parse simple string array", () => {
5+
const input = ["one two", "three"];
6+
const expected = ["one", "two", "three"];
7+
expect(parseOptions(input)).toEqual(expected);
8+
});
9+
10+
it("should handle empty array", () => {
11+
expect(parseOptions([])).toEqual([]);
12+
});
13+
14+
it("should parse quoted strings correctly", () => {
15+
const input = ['"hello world"', '"goodbye world"'];
16+
const expected = ['"hello world"', '"goodbye world"'];
17+
expect(parseOptions(input)).toEqual(expected);
18+
});
19+
20+
it("should handle mixed quoted and unquoted strings", () => {
21+
const input = ['simple "quoted value" unquoted'];
22+
const expected = ["simple", '"quoted value"', "unquoted"];
23+
expect(parseOptions(input)).toEqual(expected);
24+
});
25+
26+
it("should handle empty strings", () => {
27+
const input = [""];
28+
expect(parseOptions(input)).toEqual([]);
29+
});
30+
31+
it("should handle multiple spaces between options", () => {
32+
const input = ["one two three"];
33+
const expected = ["one", "two", "three"];
34+
expect(parseOptions(input)).toEqual(expected);
35+
});
36+
37+
it("should preserve quoted strings with spaces", () => {
38+
const input = ['"this is one" "this is two"', "'this is three'"];
39+
const expected = [
40+
'"this is one"',
41+
'"this is two"',
42+
"'this is three'",
43+
];
44+
expect(parseOptions(input)).toEqual(expected);
45+
});
46+
});

src/utils/parseOptions.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Parses an array of option strings into an array of individual options.
3+
*
4+
* The `parseOptions` function takes an array of option strings and returns an array of individual options. It uses a regular expression to split each option string into its constituent parts, handling quoted strings and other non-whitespace tokens.
5+
*
6+
* @param options - An array of option strings to be parsed.
7+
* @returns An array of individual options.
8+
*/
9+
const regex = /"[^"]*"|'[^']*'|\S+/g;
10+
export function parseOptions(options: string[]): string[] {
11+
return options.flatMap((option) => {
12+
const matches = option.match(regex);
13+
return matches || [];
14+
});
15+
}

0 commit comments

Comments
 (0)