fix seg num padding

fixes #1446
pull/1617/head
Mikael Finstad 2023-04-23 10:51:28 +02:00
rodzic 17a2a8cf53
commit 391dce22bd
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 25AB36E3E81CBC26
5 zmienionych plików z 19 dodań i 13 usunięć

Wyświetl plik

@ -1,7 +1,8 @@
import dataUriToBuffer from 'data-uri-to-buffer';
import pMap from 'p-map';
import { getSuffixedOutPath, getOutDir, transferTimestamps, getSuffixedFileName, getOutPath, escapeRegExp, getNumDigits } from '../util';
import { getSuffixedOutPath, getOutDir, transferTimestamps, getSuffixedFileName, getOutPath, escapeRegExp } from '../util';
import { getNumDigits } from '../segments';
import { captureFrame as ffmpegCaptureFrame, captureFrames as ffmpegCaptureFrames } from '../ffmpeg';

Wyświetl plik

@ -232,3 +232,10 @@ export function playOnlyCurrentSegment({ playbackMode, currentTime, playingSegme
return {};
}
export const getNumDigits = (value) => Math.floor(value > 0 ? Math.log10(value) : 0) + 1;
export function formatSegNum(segIndex, numSegments) {
const numDigits = getNumDigits(numSegments);
return `${segIndex + 1}`.padStart(numDigits, '0');
}

Wyświetl plik

@ -1,6 +1,6 @@
import { it, expect } from 'vitest';
import { test, it, expect } from 'vitest';
import { convertSegmentsToChapters, partitionIntoOverlappingRanges, getSegApparentStart, getSegApparentEnd } from './segments';
import { convertSegmentsToChapters, partitionIntoOverlappingRanges, getSegApparentStart, getSegApparentEnd, formatSegNum } from './segments';
it('converts segments to chapters with gaps', () => {
expect(convertSegmentsToChapters([
@ -109,3 +109,8 @@ it('detects overlapping segments, undefined end', () => {
[{ start: 1, end: undefined }, { start: 1.5, end: undefined }],
]);
});
test('formatSegNum', () => {
expect(formatSegNum(0, 9)).toBe('1');
expect(formatSegNum(0, 10)).toBe('01');
});

Wyświetl plik

@ -300,8 +300,6 @@ export function shuffleArray(arrayIn) {
return array;
}
export const getNumDigits = (value) => Math.floor(value > 0 ? Math.log10(value) : 0) + 1;
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping
export function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string

Wyświetl plik

@ -1,9 +1,9 @@
import i18n from 'i18next';
import lodashTemplate from 'lodash/template';
import { isMac, isWindows, hasDuplicates, filenamify, getOutFileExtension, getNumDigits } from '../util';
import { isMac, isWindows, hasDuplicates, filenamify, getOutFileExtension } from '../util';
import isDev from '../isDev';
import { getSegmentTags } from '../segments';
import { getSegmentTags, formatSegNum } from '../segments';
const { parse: parsePath, sep: pathSep, join: pathJoin, normalize: pathNormalize } = window.require('path');
@ -85,15 +85,10 @@ function interpolateSegmentFileName({ template, inputFileNameWithoutExt, segSuff
return compiled(data);
}
function formatSegNum(segIndex, segments) {
const numDigits = getNumDigits(segments);
return `${segIndex + 1}`.padStart(numDigits, '0');
}
export function generateOutSegFileNames({ segments, template, forceSafeOutputFileName, formatTimecode, isCustomFormatSelected, fileFormat, filePath, safeOutputFileName, maxLabelLength }) {
return segments.map((segment, i) => {
const { start, end, name = '' } = segment;
const segNum = formatSegNum(i, segments);
const segNum = formatSegNum(i, segments.length);
// Fields that did not come from the source file's name must be sanitized, because they may contain characters that are not supported by the target operating/file system
// however we disable this when the user has chosen to (safeOutputFileName === false)