@twec/ip-parser
Advanced tools
Comparing version 2.0.2 to 2.1.0
@@ -18,7 +18,13 @@ const debug = require('debug')('ip-asn-parser'); | ||
(acc, [key, { start, length, type, ...rest }]) => { | ||
const value = | ||
text.slice(start, start + length).trim() || rest.default; | ||
const [len, decimal] = String(length) | ||
.split('.') | ||
.map(Number); | ||
const value = text.slice(start, start + len).trim(); | ||
const parsed = transform.to(value, type, { decimal }); | ||
return { | ||
...acc, | ||
[key]: transform.to(value, type), | ||
[key]: | ||
parsed === 0 || parsed === null | ||
? parsed | ||
: parsed || rest.default || '', | ||
}; | ||
@@ -63,2 +69,3 @@ }, | ||
} | ||
container = { | ||
@@ -99,3 +106,7 @@ ...result, | ||
container.orders.push(order); | ||
if (container) { | ||
container.orders.push(order); | ||
} else { | ||
debug(`no container header present`); | ||
} | ||
order = null; | ||
@@ -106,9 +117,12 @@ return; | ||
if (type === layouts.trailer.TYPE) { | ||
if (result.numberOfHeaderRecords !== container.orders.length) { | ||
debug( | ||
`number of orders doesn't match [${result.numberOfHeaderRecords} <> ${container.orders.length}]`, | ||
); | ||
if (container) { | ||
if (result.numberOfHeaderRecords !== container.orders.length) { | ||
debug( | ||
`number of orders doesn't match [${result.numberOfHeaderRecords} <> ${container.orders.length}]`, | ||
); | ||
} | ||
this.push(container); | ||
} else { | ||
debug(`no container header present`); | ||
} | ||
this.push(container); | ||
container = null; | ||
@@ -115,0 +129,0 @@ } |
const etl = require('etl'); | ||
const Multistream = require('multistream'); | ||
const layouts = require('./layouts'); | ||
@@ -32,20 +31,21 @@ const transform = require('./transform'); | ||
return `${buffer.toString().trim()}\n`; | ||
return `${buffer.toString()}\n`; | ||
} | ||
module.exports = d => { | ||
const data = { | ||
...d, | ||
numberOfHeaderRecords: d.orders.length, | ||
numberOfDetailRecords: d.orders.reduce( | ||
(acc, cur) => acc + cur.lines.length, | ||
0, | ||
), | ||
}; | ||
module.exports = function main(input) { | ||
return etl.toStream([].concat(input)).pipe( | ||
// eslint-disable-next-line array-callback-return | ||
etl.map(function handle(container) { | ||
const data = { | ||
...container, | ||
numberOfHeaderRecords: container.orders.length, | ||
numberOfDetailRecords: container.orders.reduce( | ||
(acc, cur) => acc + cur.lines.length, | ||
0, | ||
), | ||
}; | ||
return new Multistream([ | ||
etl.toStream([stringify(layouts.header, data)]), | ||
etl.toStream(data.orders).pipe( | ||
// eslint-disable-next-line func-names, array-callback-return | ||
etl.map(function({ lines, ...o }) { | ||
this.push(stringify(layouts.header, data)); | ||
// eslint-disable-next-line array-callback-return | ||
data.orders.map(({ lines, ...o }) => { | ||
const order = { | ||
@@ -60,6 +60,6 @@ ...o, | ||
this.push(stringify(layouts.orderTrailer, order)); | ||
}), | ||
), | ||
etl.toStream([stringify(layouts.trailer, data)]), | ||
]); | ||
}); | ||
this.push(stringify(layouts.trailer, data)); | ||
}), | ||
); | ||
}; |
@@ -9,3 +9,3 @@ const layout = { | ||
start: 252, | ||
length: 5, | ||
length: 5.2, | ||
type: 'float', | ||
@@ -15,3 +15,3 @@ }, | ||
start: 257, | ||
length: 5, | ||
length: 5.2, | ||
type: 'float', | ||
@@ -21,3 +21,3 @@ }, | ||
start: 262, | ||
length: 5, | ||
length: 5.2, | ||
type: 'float', | ||
@@ -71,3 +71,3 @@ }, | ||
lineItemStatusCode: { start: 491, length: 2 }, | ||
extendedWeight: { start: 493, length: 10 }, | ||
extendedWeight: { start: 493, length: 10.2, type: 'float' }, | ||
acknowledgementCode: { start: 503, length: 2 }, | ||
@@ -80,3 +80,3 @@ referenceID: { start: 505, length: 2 }, | ||
start: 533, | ||
length: 10, | ||
length: 10.2, | ||
type: 'float', | ||
@@ -88,3 +88,3 @@ }, | ||
start: 617, | ||
length: 10, | ||
length: 10.2, | ||
type: 'float', | ||
@@ -94,3 +94,3 @@ }, | ||
start: 627, | ||
length: 10, | ||
length: 10.2, | ||
type: 'float', | ||
@@ -101,8 +101,8 @@ }, | ||
carrierTrackingNumber: { start: 677, length: 50 }, | ||
itemFee1: { start: 727, length: 10, type: 'int' }, | ||
itemFee2: { start: 737, length: 10, type: 'int' }, | ||
itemFee3: { start: 747, length: 10, type: 'int' }, | ||
itemDiscounts: { start: 757, length: 10, type: 'int' }, | ||
itemRebate: { start: 767, length: 10, type: 'int' }, | ||
itemSalesTax: { start: 777, length: 10, type: 'int' }, | ||
itemFee1: { start: 727, length: 10.2, type: 'float' }, | ||
itemFee2: { start: 737, length: 10.2, type: 'float' }, | ||
itemFee3: { start: 747, length: 10.2, type: 'float' }, | ||
itemDiscounts: { start: 757, length: 10.2, type: 'float' }, | ||
itemRebate: { start: 767, length: 10.2, type: 'float' }, | ||
itemSalesTax: { start: 777, length: 10.2, type: 'float' }, | ||
newHeaderReserveField1: { start: 787, length: 1 }, | ||
@@ -109,0 +109,0 @@ newHeaderReserveField2: { start: 788, length: 10 }, |
@@ -15,26 +15,12 @@ const { | ||
const padr = (value, length) => { | ||
return ( | ||
value + | ||
Array(length) | ||
.fill('0') | ||
.join('') | ||
).slice(0, length); | ||
}; | ||
const transformers = { | ||
time: { | ||
to: data => { | ||
return parse(data, 'HHmm', { timeZone }); | ||
}, | ||
from: data => { | ||
return [ | ||
format(data, 'HH', { timeZone }), | ||
format(data, 'mm', { timeZone }), | ||
].join(''); | ||
}, | ||
}, | ||
date: { | ||
to: data => { | ||
return parse(data, 'YYYYMMDD', { timeZone }); | ||
}, | ||
from: data => { | ||
return [ | ||
format(data, 'YYYY', { timeZone }), | ||
format(data, 'MM', { timeZone }), | ||
format(data, 'DD', { timeZone }), | ||
].join(''); | ||
}, | ||
}, | ||
datetime: { | ||
@@ -54,5 +40,9 @@ to: data => { | ||
float: { | ||
to: (data = 0, options) => { | ||
if (options && options.decimal) { | ||
const divisor = 10 ** options.decimal; | ||
to: (data, { decimal = 0 } = {}) => { | ||
if (data.trim() === '') { | ||
return null; | ||
} | ||
if (decimal) { | ||
const divisor = 10 ** decimal; | ||
const value = parseInt(data, 10); | ||
@@ -63,6 +53,10 @@ return parseFloat(`${value / divisor}.${value % divisor}`); | ||
}, | ||
from: (data = 0, { length, decimal = 0 }) => { | ||
const [a, b] = String(data).split('.'); | ||
const av = pad(a, length); | ||
const bv = b ? pad(b, decimal) : ''; | ||
from: (data, { length, decimal = 0 }) => { | ||
if (data === null) { | ||
return ''; | ||
} | ||
const [a, b = 0] = String(data).split('.'); | ||
const av = pad(a, length - decimal); | ||
const bv = padr(b, decimal); | ||
return `${av}${bv}`; | ||
@@ -72,7 +66,7 @@ }, | ||
int: { | ||
to: (data = 0) => { | ||
return parseInt(data, 10); | ||
to: data => { | ||
return data.trim() === '' ? null : parseInt(data, 10); | ||
}, | ||
from: (data = 0, { length }) => { | ||
return pad(data, length); | ||
from: (data, { length }) => { | ||
return data === null ? '' : pad(data, length); | ||
}, | ||
@@ -84,19 +78,13 @@ }, | ||
to: (data, type, options) => { | ||
if (data !== undefined) { | ||
const transformer = transformers[type]; | ||
return transformer && transformer.to | ||
? transformer.to(data, options) | ||
: data || ''; | ||
} | ||
return data || ''; | ||
const transformer = transformers[type]; | ||
return transformer && transformer.to | ||
? transformer.to(data, options) | ||
: data || ''; | ||
}, | ||
from: (data, type, options) => { | ||
if (data !== undefined) { | ||
const transformer = transformers[type]; | ||
return transformer && transformer.from | ||
? transformer.from(data, options) | ||
: data || ''; | ||
} | ||
return data || ''; | ||
const transformer = transformers[type]; | ||
return transformer && transformer.from | ||
? transformer.from(data, options) | ||
: data || ''; | ||
}, | ||
}; |
{ | ||
"name": "@twec/ip-parser", | ||
"version": "2.0.2", | ||
"version": "2.1.0", | ||
"description": "Parser for Island Pacific orders", | ||
@@ -29,4 +29,3 @@ "keywords": [ | ||
"debug": "^4.1.1", | ||
"etl": "^0.6.6", | ||
"multistream": "^4.0.0" | ||
"etl": "^0.6.6" | ||
}, | ||
@@ -33,0 +32,0 @@ "devDependencies": { |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
18374
4
0
587
- Removedmultistream@^4.0.0
- Removedmultistream@4.1.0(transitive)
- Removedonce@1.4.0(transitive)
- Removedreadable-stream@3.6.2(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removedstring_decoder@1.3.0(transitive)
- Removedwrappy@1.0.2(transitive)