Banana.Converter

Dans cet article

The class Banana.Converter is a collection of methods useful to convert various formats to and from data tables (array of arrays).

Methods

arrayToObject( headers, arrData, skipVoid)

Converts an array of arrays of 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 arrays 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 arrays.

The parameter string contains the text to convert. The parameter separator specifies the character that separates the values, default is a comma ','. The parameter textDelim specifies 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 arrays.

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 a mt940 file to a table (array of array).

naturalCompare(a, b [, caseSensitive])

Compares two strings 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 of 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 arrays) 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 arrays) to a csv file (comma separated values). Double quotes in text are replaced by apos. Texts containing commas are inserted in doubles quotes.


Banana.Converter.arrayToCsv(table);

toDate(date[, time])

Converts 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 uses 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";

toInternalTimeFormat(value)

From version 9.1 Experimental

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

The parameter value can be a String or a Date object.

Banana.Converter.toInternalTimeFormat("11:24")
// returns "11:24:00";
Banana.Converter.toInternalTimeFormat(new Date(2020, 1, 1, 10, 24, 0, 0))
// returns "11:24:00";
Banana.Converter.toInternalTimeFormat(new Date());

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 sets the number of decimals.

The parameter convZero sets 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";

toLocalePeriodFormat(startDate , endDate [, format])

Converts a period defined by startDate and endDate to a readable string.

The parameter startDate specifies the start date of the preriod, can be a date object or a string.
The parameter endDate specifies the end date of the preriod, can be a date object or a string.
The parameter format can be empty or one of the following strings: 'short', 'long'. Default is 'long'.

Return the period as a readable string in the current language of the application.


Banana.Converter.toLocalePeriodFormat("2017-01-01", "2017-01-31"); // returns "January '17"
Banana.Converter.toLocalePeriodFormat("2017-01-01", "2017-01-31", "short"); // returns "Jan '17" 
Banana.Converter.toLocalePeriodFormat("2017-01-01", "2017-01-31", "long"); // returns "January '17"

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";

 

 

This documentation is outdated

La documentation la plus complète et actualisée est celle de Banana Comptabilité Plus : Essayez-le maintenant!

Partager cet article: Twitter | Facebook | LinkedIn | Email