Skip to content

Commit 64fbc56

Browse files
authored
Merge pull request #18 from darkriszty/feature/EscapeSeparator
Feature/escape separator
2 parents 6b3ab19 + 54869db commit 64fbc56

6 files changed

Lines changed: 80 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ All notable changes to the `markdowntableprettify` extension will be documented
55
The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).
66

77
## [Unreleased]
8-
8+
### Added
9+
- Issue #12: Full rewrite for refactoring
10+
- Issue #11: Support escaping of separators with backslash
911

1012
## 1.1.1 - 2017-05-27
1113
### Fixed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
11
export class SelectionInterpreter {
22
public allRows(selection: string): string[][] {
33
return selection.split(/\r\n|\r|\n/)
4-
.map(l => l.split("|"))
5-
.filter(arr => !(arr.length == 1 && /^\s*$/.test(arr[0])));
4+
.map(this.splitLine, this)
5+
.filter(arr => arr.length > 0 && !(arr.length == 1 && /^\s*$/.test(arr[0])));
66
}
77

88
public separator(selection: string): string[] {
99
return this.allRows(selection).filter((v, i) => i == 1)[0];
1010
}
11+
12+
private splitLine(line: string): string[] {
13+
if (line == null || line.length == 0) return [];
14+
15+
let result:string[] = [],
16+
index = -1,
17+
previousSplitIndex = -1;
18+
while ((index = line.indexOf("|", index + 1)) > -1) {
19+
if (line[index - 1] != "\\") {
20+
result.push(line.substring(previousSplitIndex + 1, index));
21+
previousSplitIndex = index;
22+
}
23+
}
24+
result.push(line.substring(previousSplitIndex + 1));
25+
26+
return result;
27+
}
1128
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
x | y | x&y | x\|y
2+
--------|---------|---------|-------------
3+
`true` | `true` | `true` | `true`
4+
`true` | `false` | `false` | `true`
5+
`true` | `null` | `null` | `null`
6+
`false` | `true` | `false` | `true`
7+
`false` | `false` | `false` | `false`
8+
`false` | `null` | `false` | `null`
9+
`null` | `true` | `null` | `true`
10+
`null` | `false` | `false` | `null`
11+
`null` | `null` | `null` | `null`
12+
f\|oo | | |
13+
| | | b **\|** im
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
x|y|x&y|x\|y
2+
-|-|-|-
3+
`true` | `true` | `true` | `true`
4+
`true` | `false` | `false` | `true`
5+
`true` | `null` | `null` | `null`
6+
`false`| `true` | `false` | `true`
7+
`false`| `false` | `false` | `false`
8+
`false`| `null` | `false` | `null`
9+
`null` | `true` | `null` | `true`
10+
`null` | `false` | `false` | `null`
11+
`null` | `null` | `null` | `null`
12+
f\|oo | | |
13+
| | | b **\|** im

test/unitTests/modelFactory/selectionInterpreter.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,37 @@ suite("SelectionInterpreter tests", () => {
4444
assert.equal(rows[1][1], "v2");
4545
});
4646

47+
test("allRows() line starting and ending with pipe is correctly splitted", () => {
48+
const text = "|h1|h2|h3|h4|";
49+
const sut = createSut();
50+
51+
const rows = sut.allRows(text);
52+
53+
assert.equal(rows.length, 1);
54+
assert.equal(rows[0].length, 6);
55+
assert.equal(rows[0][0], "");
56+
assert.equal(rows[0][1], "h1");
57+
assert.equal(rows[0][2], "h2");
58+
assert.equal(rows[0][3], "h3");
59+
assert.equal(rows[0][4], "h4");
60+
assert.equal(rows[0][5], "");
61+
});
62+
63+
test("allRows() doesn't consider \\| as separator ", () => {
64+
const text = "h1|h2\\|still\\|h2\nv1|v2";
65+
const sut = createSut();
66+
67+
const rows = sut.allRows(text);
68+
69+
assert.equal(rows.length, 2);
70+
assert.equal(rows[0].length, 2);
71+
assert.equal(rows[1].length, 2);
72+
assert.equal(rows[0][0], "h1");
73+
assert.equal(rows[0][1], "h2\\|still\\|h2");
74+
assert.equal(rows[1][0], "v1");
75+
assert.equal(rows[1][1], "v2");
76+
});
77+
4778
test("separator() returns the first row", () => {
4879
const text = "h1|h2\r\n:-|-\r\nv1|v2";
4980
const sut = createSut();

0 commit comments

Comments
 (0)