@ukon1990/js-utilities
Advanced tools
Comparing version 1.4.0 to 1.5.0
@@ -50,2 +50,11 @@ import { Match } from '../models/match.model'; | ||
/** | ||
* Converts an array of objects to a CSV string. | ||
* @param list an array of objects | ||
* @param delimiter the separation character to use. If none are provided, comma will be used. | ||
* @param useKeys The keys from the object to use. If not provided, all the keys will be used. | ||
* Providing a list of keys will improve performance. | ||
*/ | ||
static objectsToCSV(list: any[], delimiter?: string, useKeys?: string[]): string; | ||
private static handleObjectToCSVRow; | ||
/** | ||
* | ||
@@ -52,0 +61,0 @@ * @param input The input string |
@@ -135,2 +135,30 @@ import { Match } from '../models/match.model'; | ||
/** | ||
* Converts an array of objects to a CSV string. | ||
* @param list an array of objects | ||
* @param delimiter the separation character to use. If none are provided, comma will be used. | ||
* @param useKeys The keys from the object to use. If not provided, all the keys will be used. | ||
* Providing a list of keys will improve performance. | ||
*/ | ||
static objectsToCSV(list, delimiter = ',', useKeys) { | ||
let body = ''; | ||
const keyMap = {}; | ||
if (useKeys) { | ||
useKeys.forEach(key => keyMap[key] = true); | ||
} | ||
list.forEach((obj) => body = this.handleObjectToCSVRow(useKeys, obj, keyMap, delimiter, body)); | ||
useKeys = Object.keys(keyMap).map(key => this.camelCaseToSentence(key)); | ||
return `${useKeys.join(delimiter)}\n\r${body}`; | ||
} | ||
static handleObjectToCSVRow(useKeys, obj, keyMap, delimiter, body) { | ||
if (!useKeys) { | ||
Object.keys(obj).forEach(key => keyMap[key] = true); | ||
} | ||
let row = ''; | ||
row += Object.keys(keyMap) | ||
.map((key) => obj[key]) | ||
.join(delimiter); | ||
body += row + '\n\r'; | ||
return body; | ||
} | ||
/** | ||
* | ||
@@ -137,0 +165,0 @@ * @param input The input string |
@@ -93,2 +93,18 @@ import { TextUtil } from './text.util'; | ||
}); | ||
describe('objectsToCSV', () => { | ||
it('can convert a list of objects with identical keys', () => { | ||
const list = [ | ||
{ name: 'John', age: 90 }, | ||
{ name: 'Aga', age: 12 } | ||
]; | ||
expect(TextUtil.objectsToCSV(list, ';')) | ||
.toEqual('Name;Age\n\r' + | ||
'John;90\n\r' + | ||
'Aga;12\n\r'); | ||
expect(TextUtil.objectsToCSV(list, ';', ['name'])) | ||
.toEqual('Name\n\r' + | ||
'John\n\r' + | ||
'Aga\n\r'); | ||
}); | ||
}); | ||
describe('csvToObjects', () => { | ||
@@ -95,0 +111,0 @@ it('can generate object from csv', () => { |
{ | ||
"name": "@ukon1990/js-utilities", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"description": "A light weight package for object and array manipulation. As well as some utilities for matching text.", | ||
@@ -22,3 +22,4 @@ "main": "dist/index.js", | ||
"Equality", | ||
"isEmpty" | ||
"isEmpty", | ||
"CSV" | ||
], | ||
@@ -25,0 +26,0 @@ "author": "Jonas Munthe Flønes", |
@@ -39,2 +39,4 @@ # Javascript utilities | ||
Generates an array of objects from a CSV string. | ||
* `objectsToCSV(list, delimiter?: string = ',', useKeys?: string[])` - Converts a list of objects to a CSV string. If provided with the | ||
keys that is supposed to be used from the objects, it will increase performance. | ||
@@ -41,0 +43,0 @@ ## EmptyUtil |
@@ -119,2 +119,19 @@ import {TextUtil} from './text.util'; | ||
describe('objectsToCSV', () => { | ||
it('can convert a list of objects with identical keys', () => { | ||
const list = [ | ||
{name: 'John', age: 90}, | ||
{name: 'Aga', age: 12} | ||
]; | ||
expect(TextUtil.objectsToCSV(list, ';')) | ||
.toEqual('Name;Age\n\r' + | ||
'John;90\n\r' + | ||
'Aga;12\n\r'); | ||
expect(TextUtil.objectsToCSV(list, ';', ['name'])) | ||
.toEqual('Name\n\r' + | ||
'John\n\r' + | ||
'Aga\n\r'); | ||
}); | ||
}); | ||
describe('csvToObjects', () => { | ||
@@ -121,0 +138,0 @@ it('can generate object from csv', () => { |
@@ -160,2 +160,45 @@ import {Match} from '../models/match.model'; | ||
/** | ||
* Converts an array of objects to a CSV string. | ||
* @param list an array of objects | ||
* @param delimiter the separation character to use. If none are provided, comma will be used. | ||
* @param useKeys The keys from the object to use. If not provided, all the keys will be used. | ||
* Providing a list of keys will improve performance. | ||
*/ | ||
public static objectsToCSV(list: any[], delimiter: string = ',', useKeys?: string[]): string { | ||
let body = ''; | ||
const keyMap = {}; | ||
if (useKeys) { | ||
useKeys.forEach(key => | ||
keyMap[key] = true); | ||
} | ||
list.forEach((obj) => | ||
body = this.handleObjectToCSVRow(useKeys, obj, keyMap, delimiter, body)); | ||
useKeys = Object.keys(keyMap).map(key => | ||
this.camelCaseToSentence(key)); | ||
return `${ | ||
useKeys.join(delimiter) | ||
}\n\r${ | ||
body}`; | ||
} | ||
private static handleObjectToCSVRow(useKeys: string[], obj, keyMap, delimiter: string, body: string) { | ||
if (!useKeys) { | ||
Object.keys(obj).forEach(key => | ||
keyMap[key] = true); | ||
} | ||
let row = ''; | ||
row += Object.keys(keyMap) | ||
.map((key: string) => | ||
obj[key]) | ||
.join(delimiter); | ||
body += row + '\n\r'; | ||
return body; | ||
} | ||
/** | ||
* | ||
@@ -162,0 +205,0 @@ * @param input The input string |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
203386
3048
54