Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Meta tweaks #11

Merged
merged 3 commits into from Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/main.yml
Expand Up @@ -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
Expand Down
80 changes: 38 additions & 42 deletions 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');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed this too, but you forgot to update the docs (already done in main branch now).

//=> '\n🦄'
```
*/
export function trimNewlinesEnd(string: string): string;
10 changes: 5 additions & 5 deletions index.js
@@ -1,4 +1,4 @@
export default function trimNewlines(string) {
export function trimNewlines(string) {
let start = 0;
let end = string.length;

Expand All @@ -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;

Expand All @@ -22,14 +22,14 @@ 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')) {
end--;
}

return end < string.length ? string.slice(0, end) : string;
};
}
6 changes: 3 additions & 3 deletions 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<string>(trimNewlines('\n🦄\r\n'));
expectType<string>(trimNewlines.start('\n\n🦄\n'));
expectType<string>(trimNewlines.end('\n🦄\n\n'));
expectType<string>(trimNewlinesStart('\n\n🦄\n'));
expectType<string>(trimNewlinesEnd('\n🦄\n\n'));
6 changes: 3 additions & 3 deletions package.json
Expand Up @@ -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"
}
}
44 changes: 22 additions & 22 deletions 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(''), '');
Expand All @@ -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);
Expand All @@ -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}`);

Expand All @@ -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}`);

Expand Down