@@ -34,44 +34,40 @@ export type FpsParseResult =
3434 | "ambiguous-decimal" ;
3535 } ;
3636
37+ type FpsFailure = Extract < FpsParseResult , { ok : false } > [ "reason" ] ;
38+ const fail = ( reason : FpsFailure ) : FpsParseResult => ( { ok : false , reason } ) ;
39+
40+ function rangedFps ( num : number , den : number ) : FpsParseResult {
41+ if ( num <= 0 ) return fail ( "non-positive" ) ;
42+ const decimal = num / den ;
43+ if ( decimal < 1 || decimal > 240 ) return fail ( "out-of-range" ) ;
44+ return { ok : true , value : { num, den } } ;
45+ }
46+
47+ function numericFps ( input : number ) : FpsParseResult {
48+ if ( ! Number . isFinite ( input ) ) return fail ( "not-a-number" ) ;
49+ if ( ! Number . isInteger ( input ) ) return fail ( "ambiguous-decimal" ) ;
50+ return rangedFps ( input , 1 ) ;
51+ }
52+
53+ function fractionFps ( raw : string ) : FpsParseResult {
54+ const parts = raw . split ( "/" ) ;
55+ if ( parts . length !== 2 ) return fail ( "invalid-fraction" ) ;
56+ const num = Number ( parts [ 0 ] ) ;
57+ const den = Number ( parts [ 1 ] ) ;
58+ if ( ! Number . isFinite ( num ) || ! Number . isFinite ( den ) ) return fail ( "not-a-number" ) ;
59+ if ( ! Number . isInteger ( num ) || ! Number . isInteger ( den ) || den <= 0 ) return fail ( "invalid-fraction" ) ;
60+ return rangedFps ( num , den ) ;
61+ }
62+
3763// Decimals like "29.97" are rejected: FFmpeg reads them as a different rate than 30000/1001.
3864export function parseFps ( input : string | number ) : FpsParseResult {
39- if ( typeof input === "number" ) {
40- if ( ! Number . isFinite ( input ) ) return { ok : false , reason : "not-a-number" } ;
41- if ( ! Number . isInteger ( input ) ) return { ok : false , reason : "ambiguous-decimal" } ;
42- if ( input <= 0 ) return { ok : false , reason : "non-positive" } ;
43- if ( input > 240 ) return { ok : false , reason : "out-of-range" } ;
44- return { ok : true , value : { num : input , den : 1 } } ;
45- }
65+ if ( typeof input === "number" ) return numericFps ( input ) ;
4666 const raw = input . trim ( ) ;
47- if ( raw === "" ) return { ok : false , reason : "empty" } ;
48-
49- if ( raw . includes ( "/" ) ) {
50- const parts = raw . split ( "/" ) ;
51- if ( parts . length !== 2 ) return { ok : false , reason : "invalid-fraction" } ;
52- const num = Number ( parts [ 0 ] ) ;
53- const den = Number ( parts [ 1 ] ) ;
54- if ( ! Number . isFinite ( num ) || ! Number . isFinite ( den ) ) {
55- return { ok : false , reason : "not-a-number" } ;
56- }
57- if ( ! Number . isInteger ( num ) || ! Number . isInteger ( den ) ) {
58- return { ok : false , reason : "invalid-fraction" } ;
59- }
60- if ( den <= 0 ) return { ok : false , reason : "invalid-fraction" } ;
61- if ( num <= 0 ) return { ok : false , reason : "non-positive" } ;
62- const decimal = num / den ;
63- if ( decimal < 1 || decimal > 240 ) return { ok : false , reason : "out-of-range" } ;
64- return { ok : true , value : { num, den } } ;
65- }
66-
67- if ( ! / ^ - ? \d + $ / . test ( raw ) ) {
68- if ( / ^ - ? \d * \. \d + $ / . test ( raw ) ) return { ok : false , reason : "ambiguous-decimal" } ;
69- return { ok : false , reason : "not-a-number" } ;
70- }
71- const n = Number ( raw ) ;
72- if ( n <= 0 ) return { ok : false , reason : "non-positive" } ;
73- if ( n > 240 ) return { ok : false , reason : "out-of-range" } ;
74- return { ok : true , value : { num : n , den : 1 } } ;
67+ if ( raw === "" ) return fail ( "empty" ) ;
68+ if ( raw . includes ( "/" ) ) return fractionFps ( raw ) ;
69+ if ( / ^ - ? \d + $ / . test ( raw ) ) return rangedFps ( Number ( raw ) , 1 ) ;
70+ return fail ( / ^ - ? \d * \. \d + $ / . test ( raw ) ? "ambiguous-decimal" : "not-a-number" ) ;
7571}
7672
7773export function parseFpsWithDefault ( input : string | number | undefined ) : FpsParseResult {
0 commit comments