npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jsons-to-cnab

v1.1.19

Published

Convert JSON data and map to cnab file

Downloads

1,562

Readme

Build Status Coverage Status

jsons-to-cnab

Convert JSON map and data to cnab file

Installation

npm install jsons-to-cnab --save

Use

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

(Direction: sending/remessa) Methods available for manipulating files through JSONs directly.

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:

  • lineBreak (String): Indicates which special character will be used to finalize the lines;
  • breakLastLine (Boolean): Indicates whether a blank line will be added at the end of the file;

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:

  • fieldName: Field name for future reference;
  • positionStart: initial position in the line, the line starts at character 1;
  • positionEnd: final position in the line;
  • positionLength: Confirmation of the number of characters occupied, must be equal to the equation: (positionEnd - (positionStart - 1));
  • picture: Indicates the type of the content value and how the spare positions should be filled:
    • X: alphanumeric value, aligned to the left and filled with whites to the right;
    • 9: integer numeric value, right aligned and filled with leading zeros;
    • V0: decimal numeric value, follows the same characteristics of picture 9, but the integer to the right indicates the number of decimal places;
  • defaultValue: Default value (in string) used to the field, when is not informed in the data mapping;

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:

  • countRow (Boolean): passing this parameter as false, the line will not be counted, this feature can be useful for continuation of lines;

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()

...****

(Direction: return/retorno) Methods available for manipulating files through JSONs directly.

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()

...

Methods available for picking up standard layouts

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

Others help methods

  • 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 lot
  • jsonsToCnab.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.

Tips

CNABs for payments

CNABs for bank charges