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

calculations-json

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

calculations-json - npm Package Compare versions

Comparing version 1.2.1 to 1.3.0

report.xls

18

index.js

@@ -11,2 +11,3 @@ /**

* options: {
* headerAsKey: false // if want to get selected header as key
* headerLine: 0, // line number

@@ -20,2 +21,17 @@ * contentStartsAt: 1 // line number

const libs = require('./libs');
exports = module.exports = libs;
exports = module.exports = libs;
libs.xlsToJson({
input: {
type: 'file',
path: __dirname + '/report.xls',
},
options: {
headerAsKey: true,
headerLine: 4,
contentStartsAt: 6
},
excludedLines: [0,1,2,3]
}).then((res) => console.log(res.reverse()));

10

libs/index.js
const xlsx = require('xlsx');
const parser = require('./parser');
module.exports.xlsToJson = (config) => {
module.exports.xlsToJson = (config = {input: {type: 'file', path: null, delimiter: ';'}, options: {headerLine: 0, contentStartsAt: 1}, excludedLines: []}) => {
const rawFile = xlsx.readFile(config.input.path);
const sheetId = rawFile.Sheets[rawFile.SheetNames[0]];
const csvString = xlsx.utils.sheet_to_csv(sheetId, {RS: '\r\n', FS: ';'});
return parser.parseFileFromString(csvString, config);
return parser.parseFileFromBuffer(csvString, config);
};
module.exports.xlsxToJson = (config) => {
module.exports.xlsxToJson = (config = {input: {type: 'file', path: null, delimiter: ';'}, options: {headerLine: 0, contentStartsAt: 1}, excludedLines: []}) => {
const rawFile = xlsx.readFile(config.input.path);
const sheetId = rawFile.Sheets[rawFile.SheetNames[0]];
const csvString = xlsx.utils.sheet_to_csv(sheetId, {RS: '\r\n', FS: ';'});
return parser.parseFileFromString(csvString, config);
return parser.parseFileFromBuffer(csvString, config);
};
module.exports.csvToJson = (config) => {
module.exports.csvToJson = (config = {input: {type: 'file', path: null, delimiter: ';'}, options: {headerLine: 0, contentStartsAt: 1}, excludedLines: []}) => {
return parser.parseFile(config);
};

@@ -6,32 +6,11 @@ const fs = require('fs');

/**
* Parser for the csv
*/
exports = module.exports.parseFile = (
{
input = {type: 'file', path: null, delimiter: ';'},
options = {headerLine: 0, contentStartsAt: 1},
excludedLines = []
}) => {
if (path) return parseFile({input, options, excludedLines});
console.error('Provide input.path object!');
process.exit(1);
};
exports = module.exports.parseFileFromString = (inputStream, {options = {headerLine: 0, contentStartsAt: 1}, excludedLines = []}) => {
const parser = csv({delimiter: ';'});
return parseNormalised(parser, Buffer.from(inputStream, 'utf8'), excludedLines, options.headerLine, options.contentStartsAt);
}
/**
* Parser for any type of the file (stream / file)
* @param {Object} config Config for the parser
*/
function parseFile({input, excludedLines = [], options = { headerLine: 0, contentStartsAt: 1}}) {
const parser = csv({delimiter: input.delimiter ? input.delimiter : ';'});
function parseFile(config = {input, excludedLines: [], options: { headerLine: 0, contentStartsAt: 1, headerAsKey: false }}) {
const parser = csv({delimiter: config.input.delimiter ? config.input.delimiter : ';'});
if (input.type === 'file') {
const buffer = fs.readFileSync(path.normalize(input.path));
return parseNormalised(parser, buffer, excludedLines, options.headerLine, options.contentStartsAt);
const buffer = fs.readFileSync(path.normalize(config.input.path));
return parseNormalised(parser, buffer, excludedLines, config.options);
}

@@ -41,3 +20,3 @@

const buffsArray = [];
const stream = fs.createReadStream(input.path);
const stream = fs.createReadStream(config.input.path);
stream.on('data', (data) => buffsArray.push(data));

@@ -48,5 +27,3 @@

buffer.push(Buffer.concat(buffsArray));
const headerLine = options.headerLine;
const contentStartsAt = options.contentStartsAt;
parseNormalised(parser, buffer[0], excludedLines, headerLine, contentStartsAt)
parseNormalised(parser, buffer[0], excludedLines, config.options)
.then((result) => resolve(result));

@@ -63,7 +40,7 @@ });

* @param {Number[]} excludedLines Excluded lines array
* @param {Number} headerLine Header line number
* @param {Number} contentStartsAt Content starts at given number
* @param {Object} options Options object
*/
function parseNormalised(parser, buffer, excludedLines = [], headerLine = 0, contentStartsAt = 1) {
function parseNormalised(parser, buffer, excludedLines = [], options = {headerLine: 0, contentStartsAt: 1, headerAsKey: false}) {
let data = [];
let headers = [];
let iterator = 0;

@@ -75,4 +52,8 @@

if (!isExcluded(excludedLines, iterator)) {
const included = include(row, headerLine, contentStartsAt, iterator);
if (included) data.push(included);
const included = include(row, iterator, options);
if (included) {
if (options.headerAsKey && included.header) headers = included.data;
if (options.headerAsKey) data.push(mapPerKey(included, headers));
else data.push(included.data);
}
}

@@ -91,3 +72,20 @@ iterator++;

function mapPerKey(row = {data: []}, headers = []) {
let index = 0;
const mapped = [];
if (row.data[0]) headers.forEach(header => mapped[prepareKey(header)] = row.data[index++]);
return mapped;
}
/**
* Prepares key for iteration
* @param {String} key Current array key
*/
function prepareKey(key) {
const keysArray = (key + '').toLowerCase().replace('%', '').replace('.', '').split(' ');
const keys = keysArray.filter((key) => key.length > 0);
return keys.join('_');
}
/**
* Checks if the current line is excluded or not

@@ -105,10 +103,30 @@ * @param {Number[]} excludedLines Excluded lines array

* @param {String[]} row Current item row
* @param {Number} headerLine Line from header is presented
* @param {Number} contentStartsAt Line from the content is being parsed
* @param {Number} iterator Iterator (defaults 0)
* @param {Object} options Options object
*/
function include(row, headerLine = 0, contentStartsAt = 1, iterator = 0) {
if (iterator === headerLine) return row;
if (iterator >= contentStartsAt) return row;
function include(row, iterator = 0, options = {headerLine: 0, contentStartsAt: 1, headerAsKey: false}) {
if (iterator === options.headerLine) return {header: true, data: row};
if (iterator >= options.contentStartsAt) return {data: row};
return;
}
/**
* Parser for the csv
*/
exports = module.exports.parseFile = (
{
input = {type: 'file', path: null, delimiter: ';'},
options = {headerLine: 0, contentStartsAt: 1},
excludedLines = []
}) => {
if (path) return parseFile({input, options, excludedLines});
console.error('Provide input.path object!');
process.exit(1);
};
exports = module.exports.parseFileFromBuffer = (inputStream, {options = {headerLine: 0, contentStartsAt: 1, headerAsKey: false}, excludedLines = []}) => {
const parser = csv({delimiter: ';'});
return parseNormalised(parser, Buffer.from(inputStream, 'utf8'), excludedLines, options);
}
{
"name": "calculations-json",
"version": "1.2.1",
"version": "1.3.0",
"description": "Parser module for the XLS / XLSX / CSV formats into plan JSON",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -57,2 +57,3 @@ # CALCULATIONS JSON

contentStartsAt: 1 // line from where the content starts
headerAsKey: false // if want selected header to be the keys
},

@@ -59,0 +60,0 @@ excludedLines: [0,1] // lines to exclude

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