From b56d9771d7d0deaeb5497b120c988730ca6815cf Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Thu, 20 Jun 2024 13:27:40 +0200 Subject: [PATCH] util: countLines func Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com> --- __tests__/util.test.ts | 36 ++++++++++++++++++++++++++++++++++++ src/util.ts | 4 ++++ 2 files changed, 40 insertions(+) diff --git a/__tests__/util.test.ts b/__tests__/util.test.ts index 9305a93..6889c1b 100644 --- a/__tests__/util.test.ts +++ b/__tests__/util.test.ts @@ -380,6 +380,42 @@ describe('stringToUnicodeEntities', () => { }); }); +describe('countLines', () => { + it('counts total number of lines correctly', () => { + const text = `This + +is +a +sample + +text +with +multiple +lines`; + + const result = Util.countLines(text); + expect(result).toEqual(10); // Including empty lines + }); + it('handles edge case with empty string', () => { + const text = ''; + + const result = Util.countLines(text); + expect(result).toEqual(1); // Empty string should have 1 line + }); + it('handles edge case with single line', () => { + const text = 'Single line text'; + + const result = Util.countLines(text); + expect(result).toEqual(1); // Single line should have 1 line + }); + it('handles multiple types of line breaks', () => { + const text = `Line 1\r\nLine 2\rLine 3\nLine 4`; + + const result = Util.countLines(text); + expect(result).toEqual(4); // Different line break types should be counted correctly + }); +}); + // See: https://github.com/actions/toolkit/blob/a1b068ec31a042ff1e10a522d8fdf0b8869d53ca/packages/core/src/core.ts#L89 function getInputName(name: string): string { return `INPUT_${name.replace(/ /g, '_').toUpperCase()}`; diff --git a/src/util.ts b/src/util.ts index c781ab5..91fc2f6 100644 --- a/src/util.ts +++ b/src/util.ts @@ -185,4 +185,8 @@ export class Util { .map(char => `&#x${char.charCodeAt(0).toString(16)};`) .join(''); } + + public static countLines(input: string): number { + return input.split(/\r\n|\r|\n/).length; + } }