In this article
The class Banana.Converter is a collection of methods useful to convert various formats to and from data tables (array of array).
Methods
arrayToObject( headers, arrData, skipVoid)
Converts an array of array string to an array of objects
- headers is an array of strings that will become the properties of the objects.
- arrData is an array containing array of strings
- skipVoid if true skip void lines, if not present is set to false
// read a CSV file var ppData = Banana.Converter.csvToTable(string, ','); // first line is header var headers = ppData[0]; // remove first line ppData.splice(0, 1); // convert in array of objects var arraOfObjects = Banana.Converter.arrayToObject(fileData.headers, ppData, true);
Introduced in 7.0.7
csvToArray(string [, separator, textdelim])
Convert a csv file (coma separated values) to an array of array.
The parameter string contains the text to convert. The parameter separator specify the character that separates the values, default is a comma ','. The parameter textDelim specify the delimiter character for text values, default is a double quote '"'.
Example:
var text = "1, 2, 3\n4, 5, 6\n7, 8, 9"; var table = Banana.Converter.csvToTable(text); var value = table[0][1]; value == '2'; // true
Introduced in 7.0.7
flvToArray(string, fieldLengths)
Convert a flv file (fixed length values) to an array of array.
The parameter string contains the text to convert. The parameter fieldLengths is an array with the lengths of the fields.
Example:
// 6 20 8 var text = "120608Phone 00002345"; var table = Banana.Converter.flvToTable(text, [6,20,8]); var value = table[0][2]; value == '00002345'; // true
Introduced in 7.0.7
mt940ToTable(string)
Converts mt940 file to a table (array of array).
naturalCompare(a, b [, caseSensitive])
Compare two string so that the string "2" is considered less then "100" as it would be with normal string compare.
This function can be passed to array.sort function.
- a first value to compare
- b second value to compare
- return value is -1 if a < b, 1 if a > b and 0 if a == b
Introduced in 7.0.7
objectArrayToCsv(headers, objArray, [separator])
Converts an array of objects (with identical schemas) into a CSV table.
- headers An array of strings with the list of properties to export
- objArray An array of objects. Each object in the array must have the same property list.
- separator The CSV column delimiter. Defaults to a comma (,) if omitted.
- return value a string containing the CSV text.
Introduced in 7.0.7
stringToCamelCase(string)
Converts a text to camel case, where only the first letter every word is upper case.
stringToLines(string)
Convert a text in an array of lines. The end line character can be '\n', \r' or a combination of both.
stringToTitleCase(string)
Converts a text to title case, where only the first letter of the text is upper case.
arrayToTsv(table [, defaultChar])
Converts a table (array of array) to a tsv file (tabulator separated values). If a string contains a tab it will be replaced with defaultChar or a space if defaultChar is undefined.
Introduced in 7.0.7
arrayToCsv(string)
Converts a table (array of array) to a csv file (coma separated values). Doubles quotes in text are replaced by apos. Texts containing comas are inserted in doubles quotes.
Introduced in 7.0.7
toDate(date[, time])
Convert a date and/or time to a javascript date object.
The parameter date is a string in the formats YYYYMMDD or YYYY-MM-DD.
The time parameter is a string in the fromats HHMM[SSZZZ] or HH:MM[:SS.ZZZ].
Introduced in 7.0.5
toInternalDateFormat(date [, inputFormat])
Converts a date to the internal format: YYYY-MM-DD.
The parameter date can be a string or a date object.
The parameter inputFormat specifies the date input format, if it is not specified the local date format is used.
Example:
Banana.Converter.toInternalDateFormat("31-12-13", "dd-mm-yy") // returns "2013-12-31"
Parameter inputFormat new in version 7.0.4
toInternalNumberFormat(value [, decimalSeparator])
Converts a number to the internal format: 123456.78.The internal number format use the character '.' as decimal separator, and doesn't contain a group separator.
The parameter value can be a string or a number object.
The parameter decimalSeparator specifies the character used to separate the decimals, if it is not specified the local decimal separator is used.
Example:
Banana.Converter.toInternalNumberFormat("1200,25", ",") // returns "1200.25"
Parameter decimalSeparator introduced in 7.0.4
toInternalTimeFormat(string)
Converts a time to the internal format: HH:MM:SS.ZZZ.
toLocaleDateFormat(date [, format])
Converts a date to the local format.
The parameter date can be a string or a date object.
The parameter format specifies the date ouptut format, if it is not specified the local date format is used.
Banana.Converter.toLocaleDateFormat("2014-02-24") // returns "24.02.20142
Introduced in 7.0.5
toLocaleNumberFormat(value [, decimals = 2, convZero = true])
Converts a number to the local format.
The parameter value can be a string or a number object.
The parameter decimals set the number of decimals.
The parameter convZero set the format returned for zero values. If false the method returns an empty string, if true it returns the zero value as string.
Example:
Banana.Converter.toLocalNumberFormat("1200.25") // returns "1'200,25"
Introduced in 7.0.5
toLocaleTimeFormat(string [, format])
Converts a time to the local format.
The parameter format specifies the time ouptut format, if it is not specified the local time format is used.
Introduced in 7.0.5
Deprecated methods
arrayToFormattedString(array)
Deprecated since 7.0.7.0. Use JSON.stringify(array, null, 3) to have a readable visualization of any kind of arrray or object.
csvToTable(string [, separator, textdelim])
Deprecated since 7.0.7.0. Use csvToArray instead.
flvToTable(string, fieldLengths)
Deprecated since 7.0.7.0. Use flvToArray instead.
toInternalAmountFormat(string)
Deprecated since 7.0.4, please use instead toInternalNumberFormat.
tableToTsv(table [, defaultChar])
Deprecated since 7.0.7.0. Use arrayToTsv instead.
tableToCsv(string)
Deprecated since 7.0.7.0. Use arrayToCsv instead.