From 391dce22bd627a4613673f6a36aedea080e5b7c9 Mon Sep 17 00:00:00 2001 From: Mikael Finstad Date: Sun, 23 Apr 2023 10:51:28 +0200 Subject: [PATCH] fix seg num padding fixes #1446 --- src/hooks/useFrameCapture.js | 3 ++- src/segments.js | 7 +++++++ src/segments.test.js | 9 +++++++-- src/util.js | 2 -- src/util/outputNameTemplate.js | 11 +++-------- 5 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/hooks/useFrameCapture.js b/src/hooks/useFrameCapture.js index 16921f9a..b5b20dd0 100644 --- a/src/hooks/useFrameCapture.js +++ b/src/hooks/useFrameCapture.js @@ -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'; diff --git a/src/segments.js b/src/segments.js index f6eeaaf2..b9d04a81 100644 --- a/src/segments.js +++ b/src/segments.js @@ -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'); +} diff --git a/src/segments.test.js b/src/segments.test.js index a905b846..a108c71b 100644 --- a/src/segments.test.js +++ b/src/segments.test.js @@ -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'); +}); diff --git a/src/util.js b/src/util.js index 9b99ac0f..41d44464 100644 --- a/src/util.js +++ b/src/util.js @@ -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 diff --git a/src/util/outputNameTemplate.js b/src/util/outputNameTemplate.js index acb7224f..f9e971e2 100644 --- a/src/util/outputNameTemplate.js +++ b/src/util/outputNameTemplate.js @@ -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)