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

Implement toCsv #1

Merged
merged 6 commits into from
Sep 2, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"@angular/platform-browser-dynamic": "^6.1.4",
"@angular/router": "^6.1.4",
"core-js": "^2.5.4",
"micro-dash": "^4.1.0",
"micro-dash": "^4.2.0",
"rxjs": "^6.0.0",
"zone.js": "~0.8.26"
},
Expand Down
21 changes: 21 additions & 0 deletions projects/s-js-utils/src/lib/to-csv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { toString } from "micro-dash";

export function toCsv(data: any[][]) {
Copy link
Member

Choose a reason for hiding this comment

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

[green]
Maybe "content" instead of "data"?

return data
.map((row) => row.map((cell) => escape(cell)).join(","))
.join("\n");
}

function escape(value: any) {
const string = toString(value);

if (/["|,|\n|\r]/.test(string)) {
return wrapWithQuotes(string.replace(/"/g, `""`));
Copy link
Member

Choose a reason for hiding this comment

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

[yellow]
Should these regular expressions be extracted into constants outside the function? Or doesn't that matter in javascript?

} else {
return string;
}
}

function wrapWithQuotes(string: string) {
return `"${string}"`;
Copy link
Member

Choose a reason for hiding this comment

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

[green]
This implementation is shorter than the name of the function. Maybe inline it?

}
72 changes: 72 additions & 0 deletions projects/s-js-utils/src/lib/write-csv.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { toCsv } from "./to-csv";

describe("toCsv()", () => {
it("works", () => {
expect(toCsv([["a", "b", "c"], ["d", "e", "f"], ["g", "h", "i"]])).toBe(
"a,b,c\nd,e,f\ng,h,i",
);
});

it("handles empty cells properly", () => {
expect(
toCsv([
["middle", "", "empty"],
["", "start", "empty"],
["end", "empty", ""],
]),
).toBe("middle,,empty\n,start,empty\nend,empty,");
});

it("handles empty rows properly", () => {
expect(toCsv([["row"], [], ["another row"], []])).toBe(
"row\n\nanother row\n",
);
});

it("properly escapes double quotes", () => {
expect(toCsv([["escape", "csv", `"quotes"`]])).toBe(
`escape,csv,"""quotes"""`,
);
});

it("properly escapes commas", () => {
expect(
toCsv([["eats shoots and leaves", "eats, shoots, and leaves"]]),
).toBe(`eats shoots and leaves,"eats, shoots, and leaves"`);
});

it("properly escapes new lines", () => {
expect(
toCsv([["one", "1"], ["two", "1\n2"], ["three", "1\n2\n3"]]),
).toBe(`one,1\ntwo,"1\n2"\nthree,"1\n2\n3"`);
expect(
toCsv([["one", "1"], ["two", "1\r2"], ["three", "1\r2\r3"]]),
).toBe(`one,1\ntwo,"1\r2"\nthree,"1\r2\r3"`);
expect(
toCsv([["one", "1"], ["two", "1\r\n2"], ["three", "1\r\n2\r\n3"]]),
).toBe(`one,1\ntwo,"1\r\n2"\nthree,"1\r\n2\r\n3"`);
});

it("escapes properly if there are multiple special things", () => {
expect(
toCsv([
["one", "with, comma"],
["another", "with\nnewline"],
["last", `with "quotes"`],
]),
).toBe(
`one,"with, comma"\nanother,"with\nnewline"\nlast,"with ""quotes"""`,
);
});

it("handles things that aren't strings", () => {
expect(
toCsv([
[undefined, null],
[true, false],
[1, 2, 3],
[{}, { hi: "there" }],
]),
).toBe(",\ntrue,false\n1,2,3\n[object Object],[object Object]");
});
});
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4377,9 +4377,9 @@ methods@~1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/methods/-/methods-1.1.2.tgz#5529a4d67654134edcc5266656835b0f851afcee"

micro-dash@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/micro-dash/-/micro-dash-4.1.0.tgz#6b69d9440d60ba83d6bc3295fa34ce4240936030"
micro-dash@^4.2.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/micro-dash/-/micro-dash-4.2.0.tgz#84da0a853dc6a869cb365b7349518d1b4ed8548b"
dependencies:
tslib "^1.9.0"

Expand Down