Add arrayToCSV

This commit is contained in:
Angelos Chalaris
2018-06-27 20:26:43 +03:00
parent a6e3fcf287
commit de42015a01
7 changed files with 1443 additions and 1407 deletions

16
snippets/arrayToCSV.md Normal file
View File

@ -0,0 +1,16 @@
### arrayToCSV
Converts a 2D array to a comma-separated values (CSV) string.
Use `Array.map()` and `String.join(delimiter)` to combine individual 1D arrays (rows) into strings.
Use `String.join('\n')` to combine all rows into a CSV string, separating each row with a newline.
Omit the second argument, `delimiter` to use a default delimiter of `,`.
```js
const arrayToCSV = (arr, delimiter = ',') => arr.map(v => v.join(delimiter)).join('\n');
```
```js
arrayToCSV([['a','b'],['c','d']]); // 'a,b\nc,d'
arrayToCSV([['a','b'],['c','d']], ';'); // 'a;b\nc;d'
```

View File

@ -1,6 +1,7 @@
all:array,function
any:array,function
approximatelyEqual:math
arrayToCSV:array,string,utility
arrayToHtmlList:browser,array
ary:adapter,function
atob:node,string,utility

View File

@ -0,0 +1,2 @@
const arrayToCSV = (arr, delimiter = ',') => arr.map(v => v.join(delimiter)).join('\n');
module.exports = arrayToCSV;

View File

@ -0,0 +1,12 @@
const expect = require('expect');
const arrayToCSV = require('./arrayToCSV.js');
test('arrayToCSV is a Function', () => {
expect(arrayToCSV).toBeInstanceOf(Function);
});
test('arrayToCSV works with default delimiter', () => {
expect(arrayToCSV([['a','b'],['c','d']])).toBe('a,b\nc,d');
});
test('arrayToCSV works with custom delimiter', () => {
expect(arrayToCSV([['a','b'],['c','d']], ';')).toBe('a;b\nc;d');
});

View File

@ -5,13 +5,12 @@ const newRating = (rating, i) =>
(selfRating || rating) + kFactor * (i - expectedScore(i ? a : b, i ? b : a));
if (ratings.length === 2) {
return [newRating(a, 1), newRating(b, 0)];
} else {
for (let i = 0; i < ratings.length; i++) {
let j = i;
while (j < ratings.length - 1) {
[ratings[i], ratings[j + 1]] = elo([ratings[i], ratings[j + 1]], kFactor);
j++;
}
for (let i = 0, len = ratings.length; i < len; i++) {
let j = i;
while (j < len - 1) {
j++;
[ratings[i], ratings[j]] = elo([ratings[i], ratings[j]], kFactor);
}
}
return ratings;

View File

@ -1,7 +1,7 @@
const equals = (a, b) => {
if (a === b) return true;
if (a instanceof Date && b instanceof Date) return a.getTime() === b.getTime();
if (!a || !b || (typeof a != 'object' && typeof b !== 'object')) return a === b;
if (!a || !b || (typeof a !== 'object' && typeof b !== 'object')) return a === b;
if (a === null || a === undefined || b === null || b === undefined) return false;
if (a.prototype !== b.prototype) return false;
let keys = Object.keys(a);

File diff suppressed because it is too large Load Diff