+0
-0
@@ -0,0 +0,0 @@ language: node_js |
+116
-125
| var internals = {}; | ||
| var fs = require("fs"); | ||
| var moment = require("moment"); | ||
@@ -76,48 +75,43 @@ var Papa = require("papaparse"); | ||
| internals.parse = function(specs, input){ | ||
| try { | ||
| if(typeof(specs) !== "object") throw "specs is not an array"; | ||
| if(lodash.isEmpty(specs)) throw "specs is empty"; | ||
| if(lodash.isEmpty(specs.map)) throw "specs maps is empty"; | ||
| if(lodash.isEmpty(specs.options)) throw "specs options is empty"; | ||
| if(input === "") throw "input is empty"; | ||
| var array_output = []; | ||
| var object_output = {}; | ||
| var split_input = input.replace(/\r\n/g,'\n').split("\n"); | ||
| if(split_input.indexOf("") !== -1){ | ||
| split_input.splice(split_input.indexOf(""), 1); | ||
| } | ||
| lodash.forEach(split_input, function(i, idx){ | ||
| if(i.length === specs.options.fullwidth && !specs.options.levels){ | ||
| if(specs.options.skiplines !== null){ | ||
| if(specs.options.skiplines.indexOf(parseInt(idx) + 1) === -1){ | ||
| array_output.push(parseCol(i, specs.map, specs.options.format)); | ||
| } | ||
| } | ||
| else{ | ||
| if(typeof(specs) !== "object") throw "specs is not an array"; | ||
| if(lodash.isEmpty(specs)) throw "specs is empty"; | ||
| if(lodash.isEmpty(specs.map)) throw "specs maps is empty"; | ||
| if(lodash.isEmpty(specs.options)) throw "specs options is empty"; | ||
| if(input === "") throw "input is empty"; | ||
| var array_output = []; | ||
| var object_output = {}; | ||
| var split_input = input.replace(/\r\n/g,'\n').split("\n"); | ||
| if(split_input.indexOf("") !== -1){ | ||
| split_input.splice(split_input.indexOf(""), 1); | ||
| } | ||
| lodash.forEach(split_input, function(i, idx){ | ||
| if(i.length === specs.options.fullwidth && !specs.options.levels){ | ||
| if(!lodash.isEmpty(specs.options.skiplines)){ | ||
| if(specs.options.skiplines.indexOf(parseInt(idx) + 1) === -1){ | ||
| array_output.push(parseCol(i, specs.map, specs.options.format)); | ||
| } | ||
| } | ||
| else if(specs.options.levels){ | ||
| var level = lodash.find(specs.options.levels, function(v, k){ | ||
| else{ | ||
| array_output.push(parseCol(i, specs.map, specs.options.format)); | ||
| } | ||
| } | ||
| else if(specs.options.levels){ | ||
| var level = lodash.find(specs.options.levels, function(v){ | ||
| if(idx >= v.start && idx <= v.end){ | ||
| return true; | ||
| } | ||
| }); | ||
| var level_map = lodash.filter(specs.map, { | ||
| level: lodash.findKey(specs.options.levels, function(v){ | ||
| if(idx >= v.start && idx <= v.end){ | ||
| return true; | ||
| } | ||
| }); | ||
| var level_map = lodash.filter(specs.map, { | ||
| level: lodash.findKey(specs.options.levels, function(v, k){ | ||
| if(idx >= v.start && idx <= v.end){ | ||
| return true; | ||
| } | ||
| }) | ||
| }); | ||
| if(i.length === level.fullwidth){ | ||
| if(!object_output.hasOwnProperty(level.nickname)){ | ||
| object_output[level.nickname] = []; | ||
| } | ||
| if(specs.options.skiplines !== null){ | ||
| if(specs.options.skiplines.indexOf(parseInt(idx) + 1) === -1){ | ||
| object_output[level.nickname].push(parseCol(i, level_map, specs.options.format)); | ||
| } | ||
| } | ||
| else{ | ||
| }) | ||
| }); | ||
| if(i.length === level.fullwidth){ | ||
| if(!object_output.hasOwnProperty(level.nickname)){ | ||
| object_output[level.nickname] = []; | ||
| } | ||
| if(specs.options.skiplines !== null){ | ||
| if(specs.options.skiplines.indexOf(parseInt(idx) + 1) === -1){ | ||
| object_output[level.nickname].push(parseCol(i, level_map, specs.options.format)); | ||
@@ -127,3 +121,3 @@ } | ||
| else{ | ||
| throw "Row #" + (parseInt(idx) + 1) + " does not match fullwidth"; | ||
| object_output[level.nickname].push(parseCol(i, level_map, specs.options.format)); | ||
| } | ||
@@ -134,21 +128,20 @@ } | ||
| } | ||
| }); | ||
| switch(specs.options.format){ | ||
| case "csv": | ||
| if(array_output.length === 0){ | ||
| throw "Multi-Level Maps Cannot Convert to CSV"; | ||
| } | ||
| else{ | ||
| return Papa.unparse(array_output.length > 0 ? array_output : object_output, { | ||
| newline: "\n" | ||
| }); | ||
| } | ||
| break; | ||
| default: | ||
| return array_output.length > 0 ? array_output : object_output; | ||
| } | ||
| } | ||
| catch(err){ | ||
| console.log(err); | ||
| } | ||
| } | ||
| else{ | ||
| throw "Row #" + (parseInt(idx) + 1) + " does not match fullwidth"; | ||
| } | ||
| }); | ||
| switch(specs.options.format){ | ||
| case "csv": | ||
| if(array_output.length === 0){ | ||
| throw "Multi-Level Maps Cannot Convert to CSV"; | ||
| } | ||
| else{ | ||
| return Papa.unparse(array_output.length > 0 ? array_output : object_output, { | ||
| newline: "\n" | ||
| }); | ||
| } | ||
| default: | ||
| return array_output.length > 0 ? array_output : object_output; | ||
| } | ||
| }; | ||
@@ -158,60 +151,26 @@ | ||
| var output = []; | ||
| try { | ||
| if(typeof(specs) !== "object") throw "specs is not an array"; | ||
| if(lodash.isEmpty(specs)) throw "specs is empty"; | ||
| if(input === "") throw "input is empty"; | ||
| var counter = 0; | ||
| if(levels){ | ||
| var rowCount = 0; | ||
| lodash.forEach(levels, function(l){ | ||
| var input_by_level = input[l]; | ||
| rowCount = rowCount + input_by_level.length; | ||
| if(typeof(specs) !== "object") throw "specs is not an array"; | ||
| if(lodash.isEmpty(specs)) throw "specs is empty"; | ||
| if(input === "") throw "input is empty"; | ||
| var counter = 0; | ||
| if(levels){ | ||
| var rowCount = 0; | ||
| lodash.forEach(levels, function(l){ | ||
| var input_by_level = input[l]; | ||
| rowCount = rowCount + input_by_level.length; | ||
| }); | ||
| lodash.forEach(levels, function(l){ | ||
| var input_by_level = input[l]; | ||
| var specs_by_level = lodash.filter(specs, { | ||
| level: l | ||
| }); | ||
| lodash.forEach(levels, function(l){ | ||
| var input_by_level = input[l]; | ||
| var specs_by_level = lodash.filter(specs, { | ||
| level: l | ||
| }); | ||
| lodash.forEach(input_by_level, function(inp){ | ||
| lodash.forEach(specs_by_level, function(spec){ | ||
| var value = String(inp[spec.name]); | ||
| var valueLength = value.length; | ||
| if(spec.width - value.length > 0){ | ||
| for(var i = 1; i <= spec.width - valueLength; i++){ | ||
| var symbol = spec.padding_symbol ? spec.padding_symbol : " "; | ||
| if(symbol.length > 1) throw "padding_symbol can not have length > 1"; | ||
| switch(spec.padding_position){ | ||
| case "start": | ||
| value = symbol + value; | ||
| break; | ||
| case "end": | ||
| value = value + symbol; | ||
| break; | ||
| default: | ||
| value = symbol + value; | ||
| break; | ||
| } | ||
| } | ||
| output = output + value; | ||
| } | ||
| }); | ||
| counter = counter + 1; | ||
| if(rowCount !== counter){ | ||
| output = output + "\n" | ||
| } | ||
| }); | ||
| }); | ||
| return output; | ||
| } | ||
| else{ | ||
| for(var row in input){ | ||
| for(var spec in specs){ | ||
| var value = String(input[row][specs[spec].name]); | ||
| lodash.forEach(input_by_level, function(inp){ | ||
| lodash.forEach(specs_by_level, function(spec){ | ||
| var value = String(inp[spec.name]); | ||
| var valueLength = value.length; | ||
| if(specs[spec].width - value.length > 0){ | ||
| for(var i = 1; i <= specs[spec].width - valueLength; i++){ | ||
| var symbol = specs[spec].padding_symbol ? specs[spec].padding_symbol : " "; | ||
| if(spec.width - value.length >= 0){ | ||
| for(var i = 1; i <= spec.width - valueLength; i++){ | ||
| var symbol = spec.padding_symbol ? spec.padding_symbol : " "; | ||
| if(symbol.length > 1) throw "padding_symbol can not have length > 1"; | ||
| switch(specs[spec].padding_position){ | ||
| switch(spec.padding_position){ | ||
| case "start": | ||
@@ -228,18 +187,50 @@ value = symbol + value; | ||
| } | ||
| output = output + value.substring(0, spec.width); | ||
| } | ||
| output = output + value; | ||
| } | ||
| }); | ||
| counter = counter + 1; | ||
| if(input.length !== counter){ | ||
| output = output + "\n" | ||
| if(rowCount !== counter){ | ||
| output = output + "\n"; | ||
| } | ||
| }); | ||
| }); | ||
| return output; | ||
| } | ||
| else{ | ||
| for(var row in input){ | ||
| for(var spec in specs){ | ||
| var value = input[row][specs[spec].name]; | ||
| var defaultValue = lodash.defaultTo(specs[spec].default, ""); | ||
| value = lodash.defaultTo(value, defaultValue); | ||
| value = String(value); | ||
| var valueLength = value.length; | ||
| if(specs[spec].width - value.length >= 0){ | ||
| for(var i = 1; i <= specs[spec].width - valueLength; i++){ | ||
| var symbol = specs[spec].padding_symbol ? specs[spec].padding_symbol : " "; | ||
| if(symbol.length > 1) throw "padding_symbol can not have length > 1"; | ||
| switch(specs[spec].padding_position){ | ||
| case "start": | ||
| value = symbol + value; | ||
| break; | ||
| case "end": | ||
| value = value + symbol; | ||
| break; | ||
| default: | ||
| value = symbol + value; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| output = output + value.substring(0, specs[spec].width); | ||
| } | ||
| return output; | ||
| counter = counter + 1; | ||
| if(input.length !== counter){ | ||
| output = output + "\n"; | ||
| } | ||
| } | ||
| return output; | ||
| } | ||
| catch(err){ | ||
| console.log(err); | ||
| } | ||
| }; | ||
| module.exports = internals; | ||
| module.exports = internals; |
+0
-0
@@ -0,0 +0,0 @@ The MIT License (MIT) |
+0
-0
| .PHONY: test | ||
| test: | ||
| mocha --check-leaks --reporter spec |
+1
-1
| { | ||
| "name": "fixy", | ||
| "version": "1.1.0", | ||
| "version": "1.2.0", | ||
| "description": "A Fixed Width Input Parser", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
+0
-0
@@ -0,0 +0,0 @@ [](https://badge.fury.io/js/fixy) |
+55
-1
@@ -5,2 +5,20 @@ var fixy = require("../index"); | ||
| describe("#parse()", function(){ | ||
| it("should allow optional values to be omitted", function(){ | ||
| var test = fixy.parse({ | ||
| map:[{ | ||
| name: "Age", | ||
| width: 2, | ||
| start: 1, | ||
| type: "int" | ||
| }], | ||
| options:{ | ||
| fullwidth: 2, | ||
| } | ||
| }, '30'); | ||
| assert.deepEqual(test, [{ | ||
| Age: 30, | ||
| }]); | ||
| }); | ||
| it("should return fixed-width-input as array(object)", function(){ | ||
@@ -527,3 +545,39 @@ var test = fixy.parse({ | ||
| }); | ||
| it("should allow default values", function(){ | ||
| var test = fixy.unparse([{ | ||
| name: "Age", | ||
| width: 7, | ||
| default: 20 | ||
| },{ | ||
| name: "Initial", | ||
| width: 4 | ||
| }], [{ | ||
| Initial: "SJP" | ||
| }]); | ||
| assert.deepEqual(test, " 20 SJP"); | ||
| }); | ||
| it("should handle null values", function(){ | ||
| var test = fixy.unparse([{ | ||
| name: "Age", | ||
| width: 7 | ||
| },{ | ||
| name: "Initial", | ||
| width: 4, | ||
| padding_symbol: "#" | ||
| }], [{ | ||
| Age: undefined, | ||
| Initial: null | ||
| }]); | ||
| assert.deepEqual(test, " ####"); | ||
| }); | ||
| it("should truncate values that are too long", function(){ | ||
| var test = fixy.unparse([{ | ||
| name: "Name", | ||
| width: 7 | ||
| }], [{ | ||
| Name: "Alexander" | ||
| }]); | ||
| assert.deepEqual(test, "Alexand"); | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
Sorry, the diff of this file is not supported yet
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
803
5.94%0
-100%24889
-2.48%7
-12.5%