Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/FFmpeggy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import TypedEmitter from "typed-emitter";
import { parseInfo, parseWriting, parseProgress } from "./parsers";
import { FFmpeggyProgress } from "./types/FFmpeggyProgress";
import { FFprobeResult } from "./types/probeTypes";
import { parseOptions } from "./utils/parseOptions";

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

const args = [
...globalOptions.join(" ").split(" "),
...inputOptions.join(" ").split(" "),
...parseOptions(globalOptions),
...parseOptions(inputOptions),
...["-i", ffmpegInput],
...outputOptions.join(" ").split(" "),
...parseOptions(outputOptions),
ffmpegOutput,
].filter((a) => !!a);

Expand Down
46 changes: 46 additions & 0 deletions src/utils/__tests__/parseOptions.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { parseOptions } from "../parseOptions";

describe("parseOptions", () => {
it("should parse simple string array", () => {
const input = ["one two", "three"];
const expected = ["one", "two", "three"];
expect(parseOptions(input)).toEqual(expected);
});

it("should handle empty array", () => {
expect(parseOptions([])).toEqual([]);
});

it("should parse quoted strings correctly", () => {
const input = ['"hello world"', '"goodbye world"'];
const expected = ['"hello world"', '"goodbye world"'];
expect(parseOptions(input)).toEqual(expected);
});

it("should handle mixed quoted and unquoted strings", () => {
const input = ['simple "quoted value" unquoted'];
const expected = ["simple", '"quoted value"', "unquoted"];
expect(parseOptions(input)).toEqual(expected);
});

it("should handle empty strings", () => {
const input = [""];
expect(parseOptions(input)).toEqual([]);
});

it("should handle multiple spaces between options", () => {
const input = ["one two three"];
const expected = ["one", "two", "three"];
expect(parseOptions(input)).toEqual(expected);
});

it("should preserve quoted strings with spaces", () => {
const input = ['"this is one" "this is two"', "'this is three'"];
const expected = [
'"this is one"',
'"this is two"',
"'this is three'",
];
expect(parseOptions(input)).toEqual(expected);
});
});
15 changes: 15 additions & 0 deletions src/utils/parseOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Parses an array of option strings into an array of individual options.
*
* 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.
*
* @param options - An array of option strings to be parsed.
* @returns An array of individual options.
*/
const regex = /"[^"]*"|'[^']*'|\S+/g;
export function parseOptions(options: string[]): string[] {
return options.flatMap((option) => {
const matches = option.match(regex);
return matches || [];
});
}
Loading