Skip to content

Commit

Permalink
util: countLines func
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <[email protected]>
  • Loading branch information
crazy-max committed Jun 20, 2024
1 parent 680ab5b commit b56d977
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
36 changes: 36 additions & 0 deletions __tests__/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()}`;
Expand Down
4 changes: 4 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit b56d977

Please sign in to comment.