Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,17 @@ Type: `String`
Default: `'original'`

The `lineMode` option specifies how the parsed line should be formatted. The following values are supported:
- `'original'`: Keeps the line unchanged, including comments and whitespace. (Default)
- `'minimal'`: Removes comments, trims leading and trailing whitespace, but preserves inner whitespace.
- `'compact'`: Removes both comments and all whitespace.
- `'original'`: Retains the line exactly as is, including comments and whitespace. (This is the default when `lineMode` is not specified.)
- `'stripped'`: Removes comments, trims leading and trailing whitespace (spaces and tabs), but keeps the inner whitespace between code elements.
- `'compact'`: Removes both comments and all whitespace characters.

Example usage:

```js
parser.parseLine('G0 X0 Y0 ; comment', { lineMode: 'original' });
// => { line: 'G0 X0 Y0 ; comment', words: [ [ 'G', 0 ], [ 'X', 0 ], [ 'Y', 0 ] ] }

parser.parseLine('G0 X0 Y0 ; comment', { lineMode: 'minimal' });
parser.parseLine('G0 X0 Y0 ; comment', { lineMode: 'stripped' });
// => { line: 'G0 X0 Y0', words: [ [ 'G', 0 ], [ 'X', 0 ], [ 'Y', 0 ] ] }

parser.parseLine('G0 X0 Y0 ; comment', { lineMode: 'compact' });
Expand Down
10 changes: 5 additions & 5 deletions src/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,22 @@ describe('Invalid G-code words', () => {
});
});

describe('Using the `lineMode` option', () => {
it('should return the original line with comments and whitespace in original mode', () => {
describe('The `lineMode` option', () => {
it('should retain the line exactly as is, including comments and whitespace for `lineMode="original"`', () => {
const line = 'M6 (tool change;) T1 ; comment';
const result = parseLine(line, { lineMode: 'original' });
expect(result.line).toBe('M6 (tool change;) T1 ; comment');
expect(result.words).toEqual([['M', 6], ['T', 1]]);
});

it('should return the line without comments but with whitespace in minimal mode', () => {
it('should remove comments, trims leading and trailing whitespace (spaces and tabs), but keeps the inner whitespace between code elements for `lineMode="stripped"`', () => {
const line = 'M6 (tool change;) T1 ; comment';
const result = parseLine(line, { lineMode: 'minimal' });
const result = parseLine(line, { lineMode: 'stripped' });
expect(result.line).toBe('M6 T1');
expect(result.words).toEqual([['M', 6], ['T', 1]]);
});

it('should return the line without comments and whitespace in compact mode', () => {
it('should remove both comments and all whitespace characters for `lineMode="compact"`', () => {
const line = 'M6 (tool change;) T1 ; comment';
const result = parseLine(line, { lineMode: 'compact' });
expect(result.line).toBe('M6T1');
Expand Down
14 changes: 7 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ const parseLine = (() => {
options.flatten = !!options?.flatten;

const validLineModes = [
'original', // Keeps the line unchanged, including comments and whitespace. (Default)
'minimal', // Removes comments, trims leading and trailing whitespace, but preserves inner whitespace.
'compact', // Removes both comments and all whitespace.
'original', // Retains the line exactly as is, including comments and whitespace. (This is the default when `lineMode` is not specified.)
'stripped', // Removes comments, trims leading and trailing whitespace (spaces and tabs), but keeps the inner whitespace between code elements.
'compact', // Removes both comments and all whitespace characters.
];
if (!validLineModes.includes(options?.lineMode)) {
options.lineMode = validLineModes[0];
Expand All @@ -119,13 +119,13 @@ const parseLine = (() => {
let ln; // Line number
let cs; // Checksum
const originalLine = line;
const [minimalLine, comments] = stripComments(line);
const compactLine = stripWhitespace(minimalLine);
const [strippedLine, comments] = stripComments(line);
const compactLine = stripWhitespace(strippedLine);

if (options.lineMode === 'compact') {
result.line = compactLine;
} else if (options.lineMode === 'minimal') {
result.line = minimalLine;
} else if (options.lineMode === 'stripped') {
result.line = strippedLine;
} else {
result.line = originalLine;
}
Expand Down