@lp-labs/timecode-toolkit 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Libin Prasanth
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,72 @@
1
- # Temporary Holding Version
1
+ # @lp-labs/timecode-toolkit
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Rational frame-rate timecode conversion. Exact integer-frame conversion for rational frame rates, including validated SMPTE 29.97/59.94 drop-frame.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ npm install @lp-labs/timecode-toolkit
9
+ ```
10
+
11
+ ## Quick start
12
+
13
+ ```js
14
+ import { framesToTimecode } from '@lp-labs/timecode-toolkit';
15
+ console.log(framesToTimecode(17982, { numerator: 30000, denominator: 1001 }, { dropFrame: true })); // 00:10:00;00
16
+ ```
17
+
18
+ ## Runtime exports
19
+
20
+ - `framesToTimecode`
21
+ - `timecodeToFrames`
22
+
23
+ The package supplies independent ESM and CommonJS builds, bundled declarations, and no runtime or peer dependencies.
24
+
25
+ ## Complete public TypeScript declarations
26
+
27
+ ```ts
28
+ export interface FrameRate {
29
+ numerator: number;
30
+ denominator: number;
31
+ }
32
+ export interface TimecodeOptions {
33
+ dropFrame?: boolean;
34
+ rollover24Hours?: boolean;
35
+ }
36
+ export interface Timecode {
37
+ hours: number;
38
+ minutes: number;
39
+ seconds: number;
40
+ frames: number;
41
+ }
42
+ export declare function framesToTimecode(
43
+ frameNumber: number,
44
+ rate: FrameRate,
45
+ options?: TimecodeOptions,
46
+ ): string;
47
+ export declare function timecodeToFrames(
48
+ value: string,
49
+ rate: FrameRate,
50
+ options?: TimecodeOptions,
51
+ ): number;
52
+ ```
53
+
54
+ ## Scope and limits
55
+
56
+ Converts safe non-negative integer frame indices to/from HH:MM:SS:FF labels. Non-drop mode supports integer frame rates from 1 through 120 and standard 24000/1001, 30000/1001, and 60000/1001 rates. Drop-frame mode is true label skipping for 30000/1001 (2 labels/minute) and 60000/1001 (4 labels/minute), except each tenth minute; skipped labels are rejected.
57
+
58
+ ## Errors and bounds
59
+
60
+ Malformed labels/rates raise TypeError; out-of-range fields, dropped labels, mismatched separator/mode, unsupported rates, and unsafe indices raise RangeError.
61
+
62
+ ## Customization
63
+
64
+ Use { numerator, denominator } for an exact rational rate. Set { dropFrame: true } only for the two listed drop-frame rates. Set rollover24Hours explicitly if timecode must wrap daily.
65
+
66
+ ## Support
67
+
68
+ If this library is useful to you, consider supporting its maintenance with a [donation via PayPal](https://www.paypal.me/LibinPrasanth).
69
+
70
+ ## Author
71
+
72
+ Created by [Libin Prasanth](https://www.linkedin.com/in/libinprasanth/). Connect on LinkedIn for updates and discussion.
@@ -0,0 +1,16 @@
1
+ export interface FrameRate {
2
+ numerator: number;
3
+ denominator: number;
4
+ }
5
+ export interface TimecodeOptions {
6
+ dropFrame?: boolean;
7
+ rollover24Hours?: boolean;
8
+ }
9
+ export interface Timecode {
10
+ hours: number;
11
+ minutes: number;
12
+ seconds: number;
13
+ frames: number;
14
+ }
15
+ export declare function framesToTimecode(frameNumber: number, rate: FrameRate, options?: TimecodeOptions): string;
16
+ export declare function timecodeToFrames(value: string, rate: FrameRate, options?: TimecodeOptions): number;
@@ -0,0 +1,111 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.framesToTimecode = framesToTimecode;
4
+ exports.timecodeToFrames = timecodeToFrames;
5
+ const MAX_HOURS = 99_999;
6
+ function rateInfo(rate, dropFrame) {
7
+ if (!rate ||
8
+ !Number.isSafeInteger(rate.numerator) ||
9
+ !Number.isSafeInteger(rate.denominator) ||
10
+ rate.numerator <= 0 ||
11
+ rate.denominator <= 0) {
12
+ throw new TypeError('Frame rate must use positive integer numerator and denominator.');
13
+ }
14
+ const nominal = Math.round(rate.numerator / rate.denominator);
15
+ const exactRate = rate.numerator / rate.denominator;
16
+ const standardFractionalRate = rate.denominator === 1_001 &&
17
+ (rate.numerator === 24_000 || rate.numerator === 30_000 || rate.numerator === 60_000);
18
+ if (nominal < 1 ||
19
+ nominal > 120 ||
20
+ (!standardFractionalRate && Math.abs(exactRate - nominal) > 0.01))
21
+ throw new RangeError('Unsupported frame rate.');
22
+ let drop = 0;
23
+ if (dropFrame) {
24
+ if (rate.numerator === 30_000 && rate.denominator === 1_001)
25
+ drop = 2;
26
+ else if (rate.numerator === 60_000 && rate.denominator === 1_001)
27
+ drop = 4;
28
+ else
29
+ throw new RangeError('Drop-frame supports only 30000/1001 or 60000/1001.');
30
+ }
31
+ return { nominal, drop };
32
+ }
33
+ function framesToTimecode(frameNumber, rate, options = {}) {
34
+ if (!Number.isSafeInteger(frameNumber) || frameNumber < 0)
35
+ throw new RangeError('Frame number must be a non-negative safe integer.');
36
+ if (options.dropFrame !== undefined && typeof options.dropFrame !== 'boolean')
37
+ throw new TypeError('dropFrame must be boolean.');
38
+ if (options.rollover24Hours !== undefined && typeof options.rollover24Hours !== 'boolean')
39
+ throw new TypeError('rollover24Hours must be boolean.');
40
+ const { nominal, drop } = rateInfo(rate, options.dropFrame ?? false);
41
+ if (options.rollover24Hours) {
42
+ const dayFrames = nominal * 86_400 - drop * (1_440 - 144);
43
+ frameNumber %= dayFrames;
44
+ }
45
+ let labelFrame = frameNumber;
46
+ if (drop > 0) {
47
+ const blockSize = nominal * 600 - drop * 9;
48
+ const blocks = Math.floor(frameNumber / blockSize);
49
+ let remainder = frameNumber % blockSize;
50
+ labelFrame = blocks * nominal * 600;
51
+ const fullMinute = nominal * 60;
52
+ if (remainder >= fullMinute) {
53
+ remainder -= fullMinute;
54
+ labelFrame += fullMinute;
55
+ for (let minute = 1; minute < 10; minute += 1) {
56
+ labelFrame += drop;
57
+ const minuteFrames = fullMinute - drop;
58
+ if (remainder < minuteFrames) {
59
+ labelFrame += remainder;
60
+ remainder = 0;
61
+ break;
62
+ }
63
+ remainder -= minuteFrames;
64
+ labelFrame += minuteFrames;
65
+ }
66
+ if (remainder > 0)
67
+ labelFrame += remainder;
68
+ }
69
+ else
70
+ labelFrame += remainder;
71
+ }
72
+ const hours = Math.floor(labelFrame / (nominal * 3600));
73
+ if (hours > MAX_HOURS)
74
+ throw new RangeError('Timecode exceeds supported hours.');
75
+ const minutes = Math.floor(labelFrame / (nominal * 60)) % 60;
76
+ const seconds = Math.floor(labelFrame / nominal) % 60;
77
+ const frames = labelFrame % nominal;
78
+ return `${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}${drop ? ';' : ':'}${String(frames).padStart(2, '0')}`;
79
+ }
80
+ function timecodeToFrames(value, rate, options = {}) {
81
+ const { nominal, drop } = rateInfo(rate, options.dropFrame ?? false);
82
+ if (typeof value !== 'string' || value.length > 32)
83
+ throw new TypeError('Timecode must be a string up to 32 characters.');
84
+ if (options.dropFrame !== undefined && typeof options.dropFrame !== 'boolean')
85
+ throw new TypeError('dropFrame must be boolean.');
86
+ if (options.rollover24Hours !== undefined && typeof options.rollover24Hours !== 'boolean')
87
+ throw new TypeError('rollover24Hours must be boolean.');
88
+ const match = /^(\d{2,5}):([0-5]\d):([0-5]\d)([:;])(\d{2})(?![\s\S])/.exec(value);
89
+ if (!match)
90
+ throw new TypeError('Expected HH:MM:SS:FF or drop-frame HH:MM:SS;FF.');
91
+ const [, hourText, minuteText, secondText, separator, frameText] = match;
92
+ const hours = Number(hourText);
93
+ const minutes = Number(minuteText);
94
+ const seconds = Number(secondText);
95
+ const frames = Number(frameText);
96
+ if (hours > MAX_HOURS || frames >= nominal)
97
+ throw new RangeError('Timecode field is out of range.');
98
+ if (options.rollover24Hours && hours >= 24)
99
+ throw new RangeError('Rollover timecode labels must be within one 24-hour day.');
100
+ if (drop > 0 !== (separator === ';'))
101
+ throw new RangeError('Timecode separator does not match drop-frame mode.');
102
+ if (drop > 0 && seconds === 0 && minutes % 10 !== 0 && frames < drop)
103
+ throw new RangeError('Timecode names a dropped frame label.');
104
+ const totalMinutes = hours * 60 + minutes;
105
+ let result = (hours * 3600 + minutes * 60 + seconds) * nominal + frames;
106
+ if (drop > 0)
107
+ result -= drop * (totalMinutes - Math.floor(totalMinutes / 10));
108
+ if (!Number.isSafeInteger(result))
109
+ throw new RangeError('Frame number exceeds safe integer range.');
110
+ return result;
111
+ }
@@ -0,0 +1 @@
1
+ {"type":"commonjs"}
@@ -0,0 +1,16 @@
1
+ export interface FrameRate {
2
+ numerator: number;
3
+ denominator: number;
4
+ }
5
+ export interface TimecodeOptions {
6
+ dropFrame?: boolean;
7
+ rollover24Hours?: boolean;
8
+ }
9
+ export interface Timecode {
10
+ hours: number;
11
+ minutes: number;
12
+ seconds: number;
13
+ frames: number;
14
+ }
15
+ export declare function framesToTimecode(frameNumber: number, rate: FrameRate, options?: TimecodeOptions): string;
16
+ export declare function timecodeToFrames(value: string, rate: FrameRate, options?: TimecodeOptions): number;
@@ -0,0 +1,107 @@
1
+ const MAX_HOURS = 99_999;
2
+ function rateInfo(rate, dropFrame) {
3
+ if (!rate ||
4
+ !Number.isSafeInteger(rate.numerator) ||
5
+ !Number.isSafeInteger(rate.denominator) ||
6
+ rate.numerator <= 0 ||
7
+ rate.denominator <= 0) {
8
+ throw new TypeError('Frame rate must use positive integer numerator and denominator.');
9
+ }
10
+ const nominal = Math.round(rate.numerator / rate.denominator);
11
+ const exactRate = rate.numerator / rate.denominator;
12
+ const standardFractionalRate = rate.denominator === 1_001 &&
13
+ (rate.numerator === 24_000 || rate.numerator === 30_000 || rate.numerator === 60_000);
14
+ if (nominal < 1 ||
15
+ nominal > 120 ||
16
+ (!standardFractionalRate && Math.abs(exactRate - nominal) > 0.01))
17
+ throw new RangeError('Unsupported frame rate.');
18
+ let drop = 0;
19
+ if (dropFrame) {
20
+ if (rate.numerator === 30_000 && rate.denominator === 1_001)
21
+ drop = 2;
22
+ else if (rate.numerator === 60_000 && rate.denominator === 1_001)
23
+ drop = 4;
24
+ else
25
+ throw new RangeError('Drop-frame supports only 30000/1001 or 60000/1001.');
26
+ }
27
+ return { nominal, drop };
28
+ }
29
+ export function framesToTimecode(frameNumber, rate, options = {}) {
30
+ if (!Number.isSafeInteger(frameNumber) || frameNumber < 0)
31
+ throw new RangeError('Frame number must be a non-negative safe integer.');
32
+ if (options.dropFrame !== undefined && typeof options.dropFrame !== 'boolean')
33
+ throw new TypeError('dropFrame must be boolean.');
34
+ if (options.rollover24Hours !== undefined && typeof options.rollover24Hours !== 'boolean')
35
+ throw new TypeError('rollover24Hours must be boolean.');
36
+ const { nominal, drop } = rateInfo(rate, options.dropFrame ?? false);
37
+ if (options.rollover24Hours) {
38
+ const dayFrames = nominal * 86_400 - drop * (1_440 - 144);
39
+ frameNumber %= dayFrames;
40
+ }
41
+ let labelFrame = frameNumber;
42
+ if (drop > 0) {
43
+ const blockSize = nominal * 600 - drop * 9;
44
+ const blocks = Math.floor(frameNumber / blockSize);
45
+ let remainder = frameNumber % blockSize;
46
+ labelFrame = blocks * nominal * 600;
47
+ const fullMinute = nominal * 60;
48
+ if (remainder >= fullMinute) {
49
+ remainder -= fullMinute;
50
+ labelFrame += fullMinute;
51
+ for (let minute = 1; minute < 10; minute += 1) {
52
+ labelFrame += drop;
53
+ const minuteFrames = fullMinute - drop;
54
+ if (remainder < minuteFrames) {
55
+ labelFrame += remainder;
56
+ remainder = 0;
57
+ break;
58
+ }
59
+ remainder -= minuteFrames;
60
+ labelFrame += minuteFrames;
61
+ }
62
+ if (remainder > 0)
63
+ labelFrame += remainder;
64
+ }
65
+ else
66
+ labelFrame += remainder;
67
+ }
68
+ const hours = Math.floor(labelFrame / (nominal * 3600));
69
+ if (hours > MAX_HOURS)
70
+ throw new RangeError('Timecode exceeds supported hours.');
71
+ const minutes = Math.floor(labelFrame / (nominal * 60)) % 60;
72
+ const seconds = Math.floor(labelFrame / nominal) % 60;
73
+ const frames = labelFrame % nominal;
74
+ return `${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}${drop ? ';' : ':'}${String(frames).padStart(2, '0')}`;
75
+ }
76
+ export function timecodeToFrames(value, rate, options = {}) {
77
+ const { nominal, drop } = rateInfo(rate, options.dropFrame ?? false);
78
+ if (typeof value !== 'string' || value.length > 32)
79
+ throw new TypeError('Timecode must be a string up to 32 characters.');
80
+ if (options.dropFrame !== undefined && typeof options.dropFrame !== 'boolean')
81
+ throw new TypeError('dropFrame must be boolean.');
82
+ if (options.rollover24Hours !== undefined && typeof options.rollover24Hours !== 'boolean')
83
+ throw new TypeError('rollover24Hours must be boolean.');
84
+ const match = /^(\d{2,5}):([0-5]\d):([0-5]\d)([:;])(\d{2})(?![\s\S])/.exec(value);
85
+ if (!match)
86
+ throw new TypeError('Expected HH:MM:SS:FF or drop-frame HH:MM:SS;FF.');
87
+ const [, hourText, minuteText, secondText, separator, frameText] = match;
88
+ const hours = Number(hourText);
89
+ const minutes = Number(minuteText);
90
+ const seconds = Number(secondText);
91
+ const frames = Number(frameText);
92
+ if (hours > MAX_HOURS || frames >= nominal)
93
+ throw new RangeError('Timecode field is out of range.');
94
+ if (options.rollover24Hours && hours >= 24)
95
+ throw new RangeError('Rollover timecode labels must be within one 24-hour day.');
96
+ if (drop > 0 !== (separator === ';'))
97
+ throw new RangeError('Timecode separator does not match drop-frame mode.');
98
+ if (drop > 0 && seconds === 0 && minutes % 10 !== 0 && frames < drop)
99
+ throw new RangeError('Timecode names a dropped frame label.');
100
+ const totalMinutes = hours * 60 + minutes;
101
+ let result = (hours * 3600 + minutes * 60 + seconds) * nominal + frames;
102
+ if (drop > 0)
103
+ result -= drop * (totalMinutes - Math.floor(totalMinutes / 10));
104
+ if (!Number.isSafeInteger(result))
105
+ throw new RangeError('Frame number exceeds safe integer range.');
106
+ return result;
107
+ }
package/package.json CHANGED
@@ -1,6 +1,65 @@
1
1
  {
2
2
  "name": "@lp-labs/timecode-toolkit",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Exact integer-frame conversion for rational frame rates, including validated SMPTE 29.97/59.94 drop-frame.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "main": "./dist/cjs/index.js",
8
+ "module": "./dist/esm/index.js",
9
+ "types": "./dist/esm/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "import": {
13
+ "types": "./dist/esm/index.d.ts",
14
+ "default": "./dist/esm/index.js"
15
+ },
16
+ "require": {
17
+ "types": "./dist/cjs/index.d.ts",
18
+ "default": "./dist/cjs/index.js"
19
+ }
20
+ },
21
+ "./package.json": "./package.json"
22
+ },
23
+ "files": [
24
+ "dist",
25
+ "README.md",
26
+ "LICENSE"
27
+ ],
28
+ "sideEffects": false,
29
+ "engines": {
30
+ "node": ">=18"
31
+ },
32
+ "scripts": {
33
+ "build": "node scripts/build.mjs",
34
+ "typecheck": "npm run build && node scripts/check-types.mjs",
35
+ "test": "npm run build && node --test tests/*.test.mjs",
36
+ "format": "prettier --write src scripts tests *.json README.md",
37
+ "format:check": "prettier --check src scripts tests *.json README.md",
38
+ "prepack": "npm run build"
39
+ },
40
+ "devDependencies": {
41
+ "typescript": "^5.9.3",
42
+ "prettier": "^3.6.2"
43
+ },
44
+ "prettier": {
45
+ "singleQuote": true,
46
+ "semi": true,
47
+ "trailingComma": "all",
48
+ "printWidth": 100,
49
+ "endOfLine": "lf"
50
+ },
51
+ "publishConfig": {
52
+ "access": "public",
53
+ "registry": "https://registry.npmjs.org/"
54
+ },
55
+ "author": {
56
+ "name": "Libin Prasanth",
57
+ "url": "https://www.linkedin.com/in/libinprasanth/"
58
+ },
59
+ "funding": {
60
+ "type": "paypal",
61
+ "url": "https://www.paypal.me/LibinPrasanth"
62
+ },
63
+ "dependencies": {},
64
+ "peerDependencies": {}
65
+ }