All files / services/notebook notebook-cell-slugs.ts

100% Statements 10/10
100% Branches 6/6
100% Functions 1/1
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28                5x   5x   5x 10x 10x   10x 5x   5x 4x         5x    
import * as vscode from 'vscode';
 
/**
 * Returns the highest auto-generated slug number (`cell-<n>`) in the notebook.
 * @param notebook The notebook document.
 * @returns The highest auto-generated slug number, or 0 if none exist.
 */
export function getMaxCellSlugNumber(notebook: vscode.NotebookDocument): number {
	const slugPattern = /^cell-(\d+)$/;
 
	let maxNumber = 0;
 
	for (const c of notebook.getCells()) {
		const slug: string | undefined = c.metadata?.slug;
		const match = typeof slug === 'string' ? slug.match(slugPattern) : null;
 
		if (match) {
			const n = parseInt(match[1], 10);
 
			if (n > maxNumber) {
				maxNumber = n;
			}
		}
	}
 
	return maxNumber;
}