From 40cd948522a9a8808caebf2f97ebbfa331548ef0 Mon Sep 17 00:00:00 2001 From: Tommy Date: Mon, 20 Mar 2023 11:44:10 -0500 Subject: [PATCH] Meta tweaks (#11) --- .github/workflows/main.yml | 6 +-- index.d.ts | 80 ++++++++++++++++++-------------------- index.js | 10 ++--- index.test-d.ts | 6 +-- package.json | 6 +-- test.js | 44 ++++++++++----------- 6 files changed, 74 insertions(+), 78 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 3b8aa86..d50ada6 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,12 +10,12 @@ jobs: fail-fast: false matrix: node-version: + - 18 - 16 - 14 - - 12 steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v2 + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 with: node-version: ${{ matrix.node-version }} - run: npm install diff --git a/index.d.ts b/index.d.ts index 0aa9fbd..c5fdb78 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,42 +1,38 @@ -declare const trimNewlines: { - /** - Trim from the start and end of a string. - - @example - ```js - import trimNewlines from 'trim-newlines'; - - trimNewlines('\nšŸ¦„\r\n'); - //=> 'šŸ¦„' - ``` - */ - (string: string): string; - - /** - Trim from the start of a string. - - @example - ```js - import trimNewlines from 'trim-newlines'; - - trimNewlines.start('\nšŸ¦„\r\n'); - //=> 'šŸ¦„\r\n' - ``` - */ - start(string: string): string; - - /** - Trim from the end of a string. - - @example - ```js - import trimNewlines from 'trim-newlines'; - - trimNewlines.end('\nšŸ¦„\r\n'); - //=> '\nšŸ¦„' - ``` - */ - end(string: string): string; -}; - -export default trimNewlines; +/** +Trim from the start and end of a string. + +@example +```js +import trimNewlines from 'trim-newlines'; + +trimNewlines('\nšŸ¦„\r\n'); +//=> 'šŸ¦„' +``` +*/ +export function trimNewlines(string: string): string; + +/** +Trim from the start of a string. + +@example +```js +import trimNewlines from 'trim-newlines'; + +trimNewlines.start('\nšŸ¦„\r\n'); +//=> 'šŸ¦„\r\n' +``` +*/ +export function trimNewlinesStart(string: string): string; + +/** +Trim from the end of a string. + +@example +```js +import trimNewlines from 'trim-newlines'; + +trimNewlines.end('\nšŸ¦„\r\n'); +//=> '\nšŸ¦„' +``` +*/ +export function trimNewlinesEnd(string: string): string; diff --git a/index.js b/index.js index 628f96a..d6f0c57 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,4 @@ -export default function trimNewlines(string) { +export function trimNewlines(string) { let start = 0; let end = string.length; @@ -13,7 +13,7 @@ export default function trimNewlines(string) { return (start > 0 || end < string.length) ? string.slice(start, end) : string; } -trimNewlines.start = string => { +export function trimNewlinesStart(string) { const end = string.length; let start = 0; @@ -22,9 +22,9 @@ trimNewlines.start = string => { } return start > 0 ? string.slice(start, end) : string; -}; +} -trimNewlines.end = string => { +export function trimNewlinesEnd(string) { let end = string.length; while (end > 0 && (string[end - 1] === '\r' || string[end - 1] === '\n')) { @@ -32,4 +32,4 @@ trimNewlines.end = string => { } return end < string.length ? string.slice(0, end) : string; -}; +} diff --git a/index.test-d.ts b/index.test-d.ts index e941958..49b07a1 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,6 +1,6 @@ import {expectType} from 'tsd'; -import trimNewlines from './index.js'; +import {trimNewlines, trimNewlinesStart, trimNewlinesEnd} from './index.js'; expectType(trimNewlines('\nšŸ¦„\r\n')); -expectType(trimNewlines.start('\n\nšŸ¦„\n')); -expectType(trimNewlines.end('\nšŸ¦„\n\n')); +expectType(trimNewlinesStart('\n\nšŸ¦„\n')); +expectType(trimNewlinesEnd('\nšŸ¦„\n\n')); diff --git a/package.json b/package.json index 56cebe8..7edde50 100644 --- a/package.json +++ b/package.json @@ -39,8 +39,8 @@ "strip" ], "devDependencies": { - "ava": "^3.15.0", - "tsd": "^0.14.0", - "xo": "^0.39.1" + "ava": "^5.2.0", + "tsd": "^0.28.0", + "xo": "^0.53.1" } } diff --git a/test.js b/test.js index 898d15b..f452673 100644 --- a/test.js +++ b/test.js @@ -1,5 +1,5 @@ import test from 'ava'; -import trimNewlines from './index.js'; +import {trimNewlines, trimNewlinesStart, trimNewlinesEnd} from './index.js'; test('main', t => { t.is(trimNewlines(''), ''); @@ -12,29 +12,29 @@ test('main', t => { }); test('start', t => { - t.is(trimNewlines.start(''), ''); - t.is(trimNewlines.start(' '), ' '); - t.is(trimNewlines.start('\n\n\r'), ''); - t.is(trimNewlines.start('\nx'), 'x'); - t.is(trimNewlines.start('\r\nx'), 'x'); - t.is(trimNewlines.start('\n\n\n\nx'), 'x'); - t.is(trimNewlines.start('\n\n\r\n\nx'), 'x'); - t.is(trimNewlines.start('x\n\n\r\n\n'), 'x\n\n\r\n\n'); + t.is(trimNewlinesStart(''), ''); + t.is(trimNewlinesStart(' '), ' '); + t.is(trimNewlinesStart('\n\n\r'), ''); + t.is(trimNewlinesStart('\nx'), 'x'); + t.is(trimNewlinesStart('\r\nx'), 'x'); + t.is(trimNewlinesStart('\n\n\n\nx'), 'x'); + t.is(trimNewlinesStart('\n\n\r\n\nx'), 'x'); + t.is(trimNewlinesStart('x\n\n\r\n\n'), 'x\n\n\r\n\n'); }); test('end', t => { - t.is(trimNewlines.end(''), ''); - t.is(trimNewlines.end(' '), ' '); - t.is(trimNewlines.end('\n\n\r'), ''); - t.is(trimNewlines.end('x\n'), 'x'); - t.is(trimNewlines.end('x\r\n'), 'x'); - t.is(trimNewlines.end('x\n\n\n\n'), 'x'); - t.is(trimNewlines.end('x\n\n\r\n\n'), 'x'); - t.is(trimNewlines.end('\n\n\r\n\nx'), '\n\n\r\n\nx'); + t.is(trimNewlinesEnd(''), ''); + t.is(trimNewlinesEnd(' '), ' '); + t.is(trimNewlinesEnd('\n\n\r'), ''); + t.is(trimNewlinesEnd('x\n'), 'x'); + t.is(trimNewlinesEnd('x\r\n'), 'x'); + t.is(trimNewlinesEnd('x\n\n\n\n'), 'x'); + t.is(trimNewlinesEnd('x\n\n\r\n\n'), 'x'); + t.is(trimNewlinesEnd('\n\n\r\n\nx'), '\n\n\r\n\nx'); }); test('main - does not have exponential performance', t => { - for (let index = 0; index < 45000; index += 1000) { + for (let index = 0; index < 45_000; index += 1000) { const string = 'a' + String(Array.from({length: index}).fill('\n').join('')) + String(Array.from({length: index}).fill('\n').join('')) + 'a'; const start = Date.now(); trimNewlines(string); @@ -48,10 +48,10 @@ test('main - does not have exponential performance', t => { }); test('start - does not have exponential performance', t => { - for (let index = 0; index < 45000; index += 1000) { + for (let index = 0; index < 45_000; index += 1000) { const string = 'a' + String(Array.from({length: index}).fill('\n').join('')); const start = Date.now(); - trimNewlines.start(string); + trimNewlinesStart(string); const difference = Date.now() - start; t.true(difference < 10, `Execution time: ${difference}`); @@ -62,10 +62,10 @@ test('start - does not have exponential performance', t => { }); test('end - does not have exponential performance', t => { - for (let index = 0; index < 45000; index += 1000) { + for (let index = 0; index < 45_000; index += 1000) { const string = String(Array.from({length: index}).fill('\n').join('')) + 'a'; const start = Date.now(); - trimNewlines.end(string); + trimNewlinesEnd(string); const difference = Date.now() - start; t.true(difference < 10, `Execution time: ${difference}`);