Add arrayToCSV
This commit is contained in:
16
snippets/arrayToCSV.md
Normal file
16
snippets/arrayToCSV.md
Normal 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'
|
||||
```
|
||||
@ -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
|
||||
|
||||
2
test/arrayToCSV/arrayToCSV.js
Normal file
2
test/arrayToCSV/arrayToCSV.js
Normal file
@ -0,0 +1,2 @@
|
||||
const arrayToCSV = (arr, delimiter = ',') => arr.map(v => v.join(delimiter)).join('\n');
|
||||
module.exports = arrayToCSV;
|
||||
12
test/arrayToCSV/arrayToCSV.test.js
Normal file
12
test/arrayToCSV/arrayToCSV.test.js
Normal 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');
|
||||
});
|
||||
@ -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;
|
||||
|
||||
@ -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);
|
||||
|
||||
2806
test/testlog
2806
test/testlog
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user