-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathpackage.scala
More file actions
85 lines (75 loc) · 2.97 KB
/
package.scala
File metadata and controls
85 lines (75 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/* Copyright 2015 Mario Pastorelli (pastorelli.mario@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package purecsv
import java.io.{File, PrintWriter}
import purecsv.unsafe.RecordSplitter.defaultFieldSeparatorStr
import purecsv.unsafe.converter.Converter
package object csviterable {
implicit class CSVRecord[A, R <: Converter[A, Seq[String]]](a: A)(implicit rfc: R) {
def toCSV(sep: String = defaultFieldSeparatorStr): String =
rfc.to(a).mkString(sep)
}
/**
* Helper class that adds methods for converting and writing an Iterable of [[A]] into CSV format.
*
* @param iter
* @param rfc
* The implicit class that allows converting [[A]] to a [[Seq]]
* @tparam A
* The type that can be converted to a CSV record
* @tparam R
* The type of the converter [[A]] <-> [[Seq]]
*/
implicit class CSVIterable[A, R <: Converter[A, Seq[String]]](iter: Iterable[A])(implicit rfc: R) {
/** Convert all the values in [[iter]] into CSV lines */
def toCSVLines(sep: String = defaultFieldSeparatorStr): Iterable[String] =
iter.map(a => rfc.to(a).mkString(sep))
/** Convert the values in [[iter]] into a CSV string */
def toCSV(sep: String = defaultFieldSeparatorStr): String = toCSVLines(sep).mkString(System.lineSeparator())
/**
* Convert [[iter]] to CSV lines and then write them into the [[PrintWriter]]
*
* @param writer
* Where to write the CSV lines
* @param sep
* The CSV separator
* @param header
* An optional header. If it is set, then it is printed as first line
*/
def writeCSVTo(writer: PrintWriter, sep: String, header: Option[Seq[String]]): Unit = {
header.foreach(h => writer.println(h.mkString(sep)))
toCSVLines(sep).foreach(writer.println)
}
/** @see [[writeCSVTo]] */
def writeCSVToFile(file: File,
sep: String = defaultFieldSeparatorStr,
header: Option[Seq[String]] = None
): Unit = {
val writer = new PrintWriter(file, "utf-8")
try {
this.writeCSVTo(writer, sep, header)
} finally {
writer.close()
}
}
/** @see [[writeCSVToFile(File,String,Option[Seq[String]]):Unit*]] */
def writeCSVToFileName(fileName: String,
sep: String = defaultFieldSeparatorStr,
header: Option[Seq[String]] = None
): Unit = {
writeCSVToFile(new File(fileName), sep, header)
}
}
}