Skip to content

Commit 653945b

Browse files
Merge pull request #219 from beakerandjake/feature/218-input-string-provided-to-puzzles-is-not-raw-it-is-trimmed-at-the-end
Feature/218 input string provided to puzzles is not raw it is trimmed at the end
2 parents 06ad802 + d8ceb6d commit 653945b

File tree

6 files changed

+31
-29
lines changed

6 files changed

+31
-29
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## [Unreleased]
9+
### Fixed
10+
- `downloadInput()` no longer trims the end of the text ([#218](https://github.com/beakerandjake/advent-of-code-runner/issues/218))
911

1012
## [1.3.4] - 2023-02-14
1113
### Fixed

src/api/api.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,14 @@ export const downloadInput = async (year, day, authenticationToken) => {
5353
}
5454

5555
// expect text of response is the input.
56-
const text = (await response.text())?.trimEnd() || '';
57-
logger.debug('downloaded: %s', sizeOfStringInKb(text));
56+
const text = await response.text();
5857

5958
if (!text) {
6059
throw new Error('Advent of code returned empty input');
6160
}
6261

62+
logger.debug('downloaded: %s', sizeOfStringInKb(text));
63+
6364
return text;
6465
};
6566

src/api/mockApi.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { logger } from '../logger.js';
1111
*/
1212
export const downloadInput = async (year, day, authenticationToken) => {
1313
logger.debug('downloading mock input file for year: %s, day: %s', year, day);
14-
return [...Array(100).keys()].map((x) => x + 1).join('\n');
14+
return `${[...Array(100).keys()].map((x) => x + 1).join('\n')}\n`;
1515
};
1616

1717
/**

src/inputs/parseInput.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ export const splitLines = (input) => {
1111
throw new TypeError('Expected argument of type String');
1212
}
1313

14-
return input.split('\n');
14+
return input.trimEnd().split('\n');
1515
};

tests/api/api.test.js

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -70,21 +70,16 @@ describe('api', () => {
7070
);
7171
});
7272

73-
test.each([undefined, null, '', '\t'])(
74-
'throws on empty input: "%s"',
75-
async (value) => {
76-
mockFetch.mockImplementation(() =>
77-
Promise.resolve({
78-
ok: true,
79-
status: 200,
80-
text: () => Promise.resolve(value),
81-
})
82-
);
83-
await expect(async () => downloadInput(2022, 1, 'ASDF')).rejects.toThrow(
84-
/empty/i
85-
);
86-
}
87-
);
73+
test.each([undefined, null, ''])('throws on empty input: "%s"', async (value) => {
74+
mockFetch.mockImplementation(() =>
75+
Promise.resolve({
76+
ok: true,
77+
status: 200,
78+
text: () => Promise.resolve(value),
79+
})
80+
);
81+
await expect(async () => downloadInput(2022, 1, 'ASDF')).rejects.toThrow(/empty/i);
82+
});
8883

8984
test('returns input on success', async () => {
9085
const expected = '1234\n5678\n9101112';
@@ -112,13 +107,13 @@ describe('api', () => {
112107
expect(result).toBe(expected);
113108
});
114109

115-
test('trims end of input', async () => {
116-
const expected = '1234\n5678\n9101112';
110+
test('does not trim end of input', async () => {
111+
const expected = '1234\n5678\n9101112\n';
117112
mockFetch.mockImplementation(() =>
118113
Promise.resolve({
119114
ok: true,
120115
status: 200,
121-
text: () => Promise.resolve(`${expected}\t\n`),
116+
text: () => Promise.resolve(expected),
122117
})
123118
);
124119
const result = await downloadInput(2022, 1, 'ASDF');

tests/inputs/parseInput.test.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { describe, test } from '@jest/globals';
22
import { splitLines } from '../../src/inputs/parseInput.js';
33

4+
const addEndingLineBreak = (input) => `${input}\n`;
5+
const inputStringFromArray = (lines) => addEndingLineBreak(lines.join('\n'));
6+
47
describe('parseInput', () => {
58
describe('splitLines()', () => {
69
test.each([null, undefined])('returns [] if input is "%s"', (input) => {
@@ -16,16 +19,17 @@ describe('parseInput', () => {
1619
);
1720

1821
test('returns array with one element if no line breaks', async () => {
19-
const input = 'ASDFQWERRTY';
20-
const result = splitLines(input);
21-
expect(result).toEqual([input]);
22+
const expected = ['ASDFQWERRTY'];
23+
const result = splitLines(inputStringFromArray(expected));
24+
expect(result).toEqual(expected);
2225
});
2326

2427
test('returns each line as array element', async () => {
25-
const lines = ['1234', 'ASDF', 'QWER', '@#$', 'asdf', '', 'zxcgv'];
26-
const input = lines.join('\n');
27-
const result = splitLines(input);
28-
expect(result).toEqual(lines);
28+
const expected = ['1234', 'ASDF', 'QWER', '@#$', 'asdf', '', 'zxcgv'];
29+
const result = splitLines(inputStringFromArray(expected));
30+
expect(result).toEqual(expected);
2931
});
32+
33+
// test('does not include empty ');
3034
});
3135
});

0 commit comments

Comments
 (0)