New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@ukon1990/js-utilities

Package Overview
Dependencies
Maintainers
1
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ukon1990/js-utilities - npm Package Compare versions

Comparing version 1.4.0 to 1.5.0

9

dist/utils/text.util.d.ts

@@ -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', () => {

5

package.json
{
"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",

2

README.md

@@ -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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc