Banana.Converter

This documentation is outdated

The most complete and up-to-date documentation is the one of Banana Accounting Plus: Try it now

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.csvToArray(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);

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.csvToArray(text);
var value = table[0][1];
value == '2'; // true

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.flvToArray(text, [6,20,8]);
var value = table[0][2];
value == '00002345'; // true

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
Banana.Converter.naturalCompare(a,b);

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.
var csvText = Banana.Converter.objectArrayToCsv(headers, objArray, ";");

stringToCamelCase(string)

Converts a text to camel case, where only the first letter every word is upper case.

Banana.Converter.stringToCamelCase("this is an example"); 
// returns "This Is An Example"

stringToLines(string)

Convert a text in an array of lines. The end line character can be '\n', \r' or a combination of both.

Banana.Converter.stringToLines("this is\nan\nexample"); 
//returns ["this is", "an", "example"]

stringToTitleCase(string)

Converts a text to title case, where only the first letter of the text is upper case.

Banana.Converter.stringToTitleCase("this is an example"); 
// returns "This is an example"

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.

Banana.Converter.arrayToTsv(table);

arrayToCsv(table)

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.

Banana.Converter.arrayToCsv(table);

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].

Banana.Converter.toDate("2015-12-31");
Banana.Converter.toDate("20151231");

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"
Banana.Converter.toInternalDateFormat(new Date());
// return current date in format "yyyy-mm-dd"

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"

toInternalTimeFormat(string)

Converts a time to the internal format: HH:MM:SS.ZZZ.

Banana.Converter.toInternalTimeFormat("11:24"); 
// returns "11:24:00"

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.2014"

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.toLocaleNumberFormat("1200.25") 
// returns "1'200,25"

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.

Banana.Converter.toLocaleTimeFormat("11:24:42");
// returns "11:24"

 

 

Share this article: Twitter | Facebook | LinkedIn | Email