In diesem Artikel
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 arrayOfObjects = Banana.Converter.arrayToObject(headers, ppData, true);
// you can now access the data with a the column name used in the csv header
let name = arrayOfObjects[0]["name"]
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);
textToHash(text, algorithmName)
Converts a text string into a hash using the algorithm passed as a parameter, to see the available algorithms see QCryptographicHash::Algorithm.
Banana.Converter.textToHash(text,algorithmName);
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.
- Possibly specify exactly the input format and also the separator used in the input date, or else use the "-" as a separator.
- For example:
- date "31.12.2024" inputFormat "dd.mm.yyyy" convert to "2024-12-31"
- date "31-12-24" inputFormat "dd.mm.yy"convert to "2024-12-31"
- date "12/31/24" inputFormat "mm/dd/yy"convert to "2024-12-31"
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 localized number into the internal number format used by the system.
The function strip all character that are not numeric or minus "-" or decimalSeparator.
Parameters:
value
(string | number):
The number to be converted. This can be a string or a number object and may include locale-specific formatting (e.g., thousand separators and a localized decimal separator).decimalSeparator
(optional, string):
Specifies the character used as the decimal separator in the input. If not provided, the function will use the decimal separator of the current locale.
The first occurrence of decimalSeparator is replaced by the "." and the others are discarded.
Returns:
- A string representing the number in the internal format, with a period (
.
) as the decimal separator and no thousand separators.
Example:
Banana.Converter.toInternalNumberFormat("1200,25", ",");
// returns "1200.25";
toInternalTimeFormat(string)
Converts a time value to the system's internal format, represented as HH:MM:SS.ZZZ
. If the milliseconds are zero, they are omitted from the returned string.
Parameters:
value
(string | Date):- The input time value to be converted.
- Can be a string representing a time (e.g., "12:34:56.789") or a JavaScript
Date
object.
Returns:
- A string in the internal time format:
HH:MM:SS.ZZZ
. - If the milliseconds portion is
0
, it will return the format asHH:MM:SS
without the.ZZZ
part.
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 format is taken from the operating system settings.
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";