Convert JSON map and data to cnab file
npm install jsons-to-cnab --save
Recalling the structure of CNAB:
N1 | N2 | N3 | N4 |
---|---|---|---|
File | Header of File | ||
Lots | Header of Lot | ||
Details | Detail | ||
Detail | |||
Footer of Lot | |||
Header of Lot | |||
Details | Detail | ||
Detail | |||
Footer of Lot | |||
Footer of File |
Start the file constructor, providing the size of the internal lines.
const { JsonsToCnab } = require("jsons-to-cnab")
const registryLength = 240
const jsonsToCnab = new JsonsToCnab(registryLength)
...
Optionally you can also pass the following parameters in the constructor:
We can use config methods more than once. Each time it is used it will configure the element map until the next map configuration.
The set method rewrites the element properties and should be used at least once. When it is used more than once, it will erase the previous element and write a new one in its place.
The "add method" includes a new element to the final document. the previous elements are retained instead of rewritten.
Pass the map of the fields that make up the element in the following format.
Each field of ve contains:
(positionEnd - (positionStart - 1))
;ex.:
...
const exJsonMapSample = [
{
fieldName: "CODIGODOBANCO",
positionStart: 1,
positionEnd: 3,
positionLength: 3,
picture: "9",
defaultValue: "341", // string!
},
{
fieldName: "CODIGODOLOTE",
positionStart: 4,
positionEnd: 7,
positionLength: 4,
picture: "9",
defaultValue: "0000",
},
...
]
...
The values of the fields will be informed through a JSON, where the attributes will be the names of previously mapped fields.
...
const exJsonDataSample = {
CODIGODOLOTE: 001
}
...
Configure the file header and define the data applied to the layout:
...
jsonsToCnab.configHeaderFile(exJsonMapSample)
jsonsToCnab.setHeaderFile(exJsonDataSample)
...
For each generated file, it is necessary to add only one header and file footer through the set method.
We can add one or more of the other elements as head and footer for batch or details.
Configure a lot header and define the data applied to the layout:
...
jsonsToCnab.configHeaderLote(exJsonMapSample)
jsonsToCnab.addHeaderLote(exJsonDataSample)
...
Configure a row detail and define the data applied to the layout:
...
jsonsToCnab.configRow(exJsonMapSample)
jsonsToCnab.addRow(exJsonDataSample)
...
Optionally you can also pass the following parameters in the method:
Configure a lot footer and define the data applied to the layout:
...
jsonsToCnab.configFooterLote(exJsonMapSample)
jsonsToCnab.addFooterLote(exJsonDataSample)
...
Configure a header footer and define the data applied to the layout:
...
jsonsToCnab.configFooterFile(exJsonMapSample)
jsonsToCnab.setFooterFile(exJsonDataSample)
...
Exports the data stored in the instance to a file:
...
const bufferWithFile = jsonsToCnab.save()
...****
Start the file reader, providing the size of the internal lines.
const { CnabToJsons } = require("jsons-to-cnab")
const base64FromFile = getFile() // this method is not included in the package, it returns a file encoded in base64
const cnabToJsons = new CnabToJsons(base64FromFile)
...
We need to train the reader so that it identifies which layout to apply to each line.
...
const definitions = [
{
"position": [14, 15],
"value": ["A" ],
"map": exJsonMapSample
}
]
cnabToJsons.fit(definitions)
...
Exports the data from file to instance:
...
const jsonData = cnabToJsons.convert()
...
Some of the auxiliary libraries included in this package allow you to get pre-defined layouts of CSV settings tables.
...
const product = "SISPAG"
const registerAlias = "HED-OP-DOC-TED-CC"
const jsonMap = JsonsToCnab.getFromLayoutsLib(product, registerAlias)
jsonsToCnab.setFooterFile(jsonMap)
...
You can save your own layouts in the format:
registerAlias | fieldName | descripton | positionStart | positionEnd | positionLength | picture | defaultValue |
---|---|---|---|---|---|---|---|
HED-FIL | CODIGODOBANCO | CÓDIGO DO BCO NA COMPENSAÇÃO | 1 | 3 | 3 | 9 | 341 |
HED-FIL | CODIGODOLOTE | LOTE DE SERVIÇO | 4 | 7 | 4 | 9 |
jsonsToCnab.counterLots()
: Count and return the current number of registered lots;jsonsToCnab.counterRegistersInFile()
: Count and return the total lines of the file;jsonsToCnab.counterRegistersInCurrentLot()
: Count and return total lines in lotjsonsToCnab.counterDetailsInCurrentLot()
: Count and return the total of computed* details* By adding a row you can tell whether or not it should be computed. Some instructions specify that special records should not be computed with a new sequential number.