# utils-nxg-cg

> Library with different methods for use in each component from OIH

Latest version **1.6.3** (published 2024-05-31) · ISC license · 0 weekly downloads

## Install

```sh
npm install utils-nxg-cg
pnpm add utils-nxg-cg
yarn add utils-nxg-cg
bun add utils-nxg-cg
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.6.3 |
| Published | 2024-05-31 |
| First published | 2022-08-03 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 3.3 MB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | Arturo Valencia |
| Maintainers | cloudgenuser |
| Keywords | utils, library, CloudGen, N3xGen, components |

## Links

- npm: https://www.npmjs.com/package/utils-nxg-cg
- Repository: https://github.com/CloudGenUser/utils-oih-cg
- Homepage: https://github.com/CloudGenUser/utils-oih-cg#readme
- Issues: https://github.com/CloudGenUser/utils-oih-cg/issues
- npm.io page: https://npm.io/package/utils-nxg-cg

## Dependencies (5)

- [joi](https://npm.io/package/joi.md) ^17.7.0
- [axios](https://npm.io/package/axios.md) ^1.1.3
- [winston](https://npm.io/package/winston.md) ^3.8.2
- [fast-xml-parser](https://npm.io/package/fast-xml-parser.md) ^4.0.11
- [encoding-japanese](https://npm.io/package/encoding-japanese.md) ^2.0.0

## Recent versions

- 1.6.3 (latest) — 2024-05-31
- 1.6.2-SNAPSHOT — 2024-05-29
- 1.6.1-SNAPSHOT — 2024-05-24
- 1.6.0-SNAPSHOT — 2024-05-21
- 1.5.9-SNAPSHOT — 2024-05-21
- 1.5.5 — 2024-05-15
- 1.5.8-SNAPSHOT — 2024-05-14
- 1.5.7-SNAPSHOT — 2024-05-14
- 1.5.6-SNAPSHOT — 2024-04-23
- 1.5.5-SNAPSHOT — 2024-04-23
- 1.5.4 — 2024-04-10
- 1.5.3 — 2024-04-10
- 1.5.2-SNAPSHOT — 2024-04-10
- 1.3.8 — 2024-04-10
- 1.4.9-DEMO — 2024-04-10
- … 58 more at https://npm.io/package/utils-nxg-cg/versions

## README

[![N|Solid](https://cloudgensys.com/cg-demo/wp-content/uploads/2019/05/CG-Logo-01.png)](https://www.cloudgensys.com/)

# utils-nxg-cg (Utils lib)

## 1. Introduction

The purpose of this library is to provide methods and constants to be used in cloudgen libraries or components both for applications NodeJs.

## 2.	Library Constants

The library can be installed from npm page with some of the following commands:

**`npm install utils-nxg-cg`**, **`npm i utils-nxg-cg`** or **`yarn install utils-nxg-cg`**

* ### 2.1. Constants and Examples

  The constants are properties with values by default, the following constants are on the library:

   **emits**: This is an object with keys and values ready to use under components for emits on OIH.
   
   ***Values and Example***:
   ```javascript
   emits: {
        data: 'data',//to emit data to another step
        end: 'end',// to emit end of flow
        error: 'error', // to emit an error
        snapshot: 'snapshot'// to emit snapshot
   }
   //Example
   this.emit(emits.data, {
     data: {content: _data}
   });
   ```
    ---
   
   **messages_databases**: This is an object with messages for library or components that using connections to databases.
   
   ***Values and Example***:
   ```javascript
   messages_databases: {
        CONNECTION_END: 'Connection end',
        ERROR_BATCH: 'Batch size exceeds the limit of',
        QUERY_ERROR: 'Error executing query',
        QUERY_EXECUTED: 'Query successfully executed',
        ST: 'Start transaction',
        START_BATCH: 'Start batch query',
        START_COMMIT: 'Start commit',
        START_QUERY: 'Start single query',
        TRANSACTION_ERROR: 'Failed, undo changes and close connection'
   }
   //Example 
   log.debug(messages_databases.ST);
   ```
    ---

   **ELASTICIO_LISTEN_MESSAGES_ON**: This is a property with the value of an environment variable obviously if env exists in the operating system if not value is null.
   
   ***Example***:
   ```javascript
   if (constants.ELASTICIO_LISTEN_MESSAGES_ON) {
    ...any
   }
   ```
    ---
  **ERROR_JSON_FORMAT**: This is a constant with a text message to be used in the case that an object JSON is bad.

  ***Example***:
   ```javascript
   if(!isObjectValid(properties.content))
      throw Error(constants.ERROR_JSON_FORMAT);
   ```
    ---
  **ERROR_XML_FORMAT**: This is a constant with a text message to be used in the case that an XML structure is bad.

  ***Example***:
   ```javascript
   if(!validateXMLStructure(properties.content))
      throw Error(constants.ERROR_XML_FORMAT);
   ```
    ---
 
  **ERROR_CONVERT_ENCODING**: This is a constant with a text message to be used in the case that an encoding is invalid or data to verify it is null or undefined.

  ***Example***:
   ```javascript
   if (validateHex(value)) throw Error(ERROR_CONVERT_ENCODING);
   ```
    ---
 
  **ERROR_MD5**: This is a constant with a text message to be used in the case that the creation sum md5 fail.

  ***Example***:
   ```javascript
   const md5_source = checkSumMD5(msg, cfg);
   if (md5_source) {
      ....
   } else
      throw Error(constants.ERROR_MD5);
   ```
    ---
   
   **ERROR_PROPERTY**: This is a property with a message if property is missing.
   
   ***Example***:
   ```javascript
   if (!data) {
     throw Error(`${constants.ERROR_PROPERTY} data`);
   }
   ```
    ---
   
   **ERROR_WITH_PROPERTY**: This is a property with a message if property is in bad format.
   
   ***Example***:
   ```javascript
   if (typeof properties.content === 'string') {
     throw Error(`${constants.ERROR_WITH_PROPERTY} data must not be string`);
   }
   ```
    ---
   
   **FINISH_EXEC**: This is a property with a message for indicate the finish of any process or execution.
   
   ***Example***:
   ```javascript
   log.info(constants.FINISH_EXEC);
   ```
    ---

   **md5sum**: This is a constant with the name of a property to create and save value of sum to verify data integrity.

  ***Example***:
   ```javascript
   if (!data.hasOwnProperty(md5sum)) {
        if (!cfg.hasOwnProperty(md5sum)) {
            return true;
        } else {
            _md5sum = cfg[md5sum];
        }
    } else {
        _md5sum = data[md5sum];
    }
   ```
    ---
   
   **PROCESS_SUCCESS**:This is a property with a message for indicate the finish of any process, execution or transform.
   
   ***Example***:
   ```javascript
   log.info(constants.PROCESS_SUCCESS);
   ```
    ---
   
   **SUCCESS_TRANS**:This is a property with a message for indicate the finish of any process, execution or transform.
   
   ***Example***:
   ```javascript
   log.info(constants.SUCCESS_TRANS);
   ```
    ---
   
   **URI_RABBITMQ**: This is a property with the value of an environment variable obviously if env exists in the operating system if not value is `amqp://localhost:5672`.
   
   ***Example***:
   ```javascript
   if (constants.URI_RABBITMQ) {
    ...any
   }
   ```
    ---

   **flags**: This is an object with keys and values same ready to use and identify the flag to operations in components like sftp, ftp, ftps and aws

   ***Values and Example***:
   ```javascript
   flags: {
        CREATEDIRECTORY: 'CREATEDIRECTORY',
        DELETEDIRECTORY: 'DELETEDIRECTORY',
        DELETEFILE: 'DELETEFILE',
        DOWNLOADIRECTORY: 'DOWNLOADIRECTORY',
        GETFILE: 'GETFILE',
        GETLISTFILES: 'GETLISTFILES',
        RENAMEFILE: 'RENAMEFILE',
        SAVEFILE: 'SAVEFILE',
        UPLOADIRECTORY: 'UPLOADIRECTORY'
   }
   //Example
   if (flag) {
        if (flag === flags.GETFILE){
            ..........
        }
    }
   ```
    ---

   **log_levels**: This is an object with keys and values ready to use under components for send the level of log and data to elastic search.
   
   ***Values and Example***:
   ```javascript
   log_levels: {
	info:"info", //for info
	error:"error", //for error
	debug:"debug", //for debug
	warn:"warn" //for warn
   }
   //Example
   await loging_elastic(e, log_levels.error);
   ```
    ---
   

## 3.	Library Methods


* ### 3.1. Methods from Helpers and Examples

  The following methods are from helpers on the library:

   **convertBase64ToUtf8**: This method will convert the following encodings `base64` or `base64url` to `utf8`.
   
   ***Example***:
   ```javascript
   const result = helpers.convertBase64ToUtf8(properties.content);
   ```
    ---

   **convertObject**: This method tries to convert any parameter to object, we can send `base64` and wait for the result, if the conversion is not completed, function catch error.

   ***Example***:
   ```javascript
   console.log(helpers.convertToObject([……]))
   console.log(helpers.convertToObject("ZWZyZnI="))
   console.log(helpers.convertToObject({…..}))
   ```
    ---

   ***convertToUtf8***: This method will convert the following encodings `ascii`, `binary`, `latin1`(ISO), `utf-8` to `utf8`.
   
   ***Example***:
   ```javascript
   const result = helpers.convertToUtf8(properties.content);
   ```
    ---
   **deleteFile**: This method is to remove a file, the arg is a `path` , this method is `asynchronous`.
   
   ***Example***:
   ```javascript
   await helpers.deleteFile(properties.file);
   ```
    ---
   **helperDirectory**: This method is to create a directory inside the library of the path `node_modules/utils-nxg-cg` or in `root project`, this method return the path string.
   
   ***Examples***: In this first example create a directory in a root project from nodejs application
   ```javascript
   const result = helpers.helperDirectory(logs);
   ```
   Result:
   ![](https://github.com/cloudgenuser/utils-nxg-cg/blob/main/resources/example1.png?raw=true)
   
   In this second example create a directory inside directory `node_modules/utils-nxg-cg` a root project from nodejs application
   ```javascript
   const result = helpers.helperDirectory(logs, true);
   ```
   Result:
   ![](https://github.com/CloudGenUser/utils-nxg-cg/blob/main/resources/example2.png?raw=true)
   
   >The second result is when use the library in another project if not, then the result is the example one
     ---
   **isObject**: This method is to validate if any `thing` is an `object` of javascript and result will be `true` or `false`.
   
   ***Examples***: 
   ```javascript
    const object = {};
    const json_1 = await fs.readFile('files/exam.json');
    const json_2 = await fs.readFile('files/exam.json', 'base64');
    const json_4 = await fs.readFile('files/exam.json', 'base64url');
    const json_5 = await fs.readFile('files/exam.json', 'utf8');
    const json_6 = await fs.readFile('files/exam.json', 'ascii');

    const object_2 =
        {
            "product": "Live JSON generator",
            "version": 3.1,
            "releaseDate": "2014-06-25T00:00:00.000Z",
            "demo": true,
            "person": {
                "id": 12345,
                "name": "John Doe",
                "phones": {
                    "home": "800-123-4567",
                    "mobile": "877-123-1234"
                },
                "email": [
                    "jd@example.com",
                    "jd@example.org"
                ],
                "dateOfBirth": "1980-01-02T00:00:00.000Z",
                "registered": true
            }
        };

    const car = {type:"Fiat", model:"500", color:"white"};
    
    console.log('object',helpers.isObject(object))//will be true
    console.log('object_2',helpers.isObject(object_2))//will be true
    console.log('car',helpers.isObject(car))//will be true
    console.log('json_1',helpers.isObject(json_1))//will be true
    console.log('json_2',helpers.isObject(json_2))//will be true
    console.log('json_4',helpers.isObject(json_4))//will be true
    console.log('json_5',helpers.isObject(json_5))//will be true
    console.log('json_6',helpers.isObject(json_6))//will be true
    helpers.isObject({value:1}); //will be true
    helpers.isObject({}); //will be true
    helpers.isObject(1); //will be false
   ```
     ---
   **isObjectValid**: This method is similar that the method `isObject` but the method `isObjectValid` valid if any thing is an object of javascript and this contains any `keys` and `values` result will be true or false.
   
   ***Examples***: 
   ```javascript
    const object = {};
    const json_1 = await fs.readFile('files/exam.json');
    const json_2 = await fs.readFile('files/exam.json', 'base64');
    const json_4 = await fs.readFile('files/exam.json', 'base64url');
    const json_5 = await fs.readFile('files/exam.json', 'utf8');
    const json_6 = await fs.readFile('files/exam.json', 'ascii');

    const object_2 =
        {
            "product": "Live JSON generator",
            "version": 3.1,
            "releaseDate": "2014-06-25T00:00:00.000Z",
            "demo": true,
            "person": {
                "id": 12345,
                "name": "John Doe",
                "phones": {
                    "home": "800-123-4567",
                    "mobile": "877-123-1234"
                },
                "email": [
                    "jd@example.com",
                    "jd@example.org"
                ],
                "dateOfBirth": "1980-01-02T00:00:00.000Z",
                "registered": true
            }
        };

    const car = {type:"Fiat", model:"500", color:"white"};
    console.log('object',helpers.isObjectValid(object))//will be false
    console.log('object_2',helpers.isObjectValid(object_2))//will be true
    console.log('car',helpers.isObjectValid(car))//will be true
    console.log('json_1',helpers.isObjectValid(json_1))//will be true
    console.log('json_2',helpers.isObjectValid(json_2))//will be true
    console.log('json_4',helpers.isObjectValid(json_4))//will be true
    console.log('json_5',helpers.isObjectValid(json_5))//will be true
    console.log('json_6',helpers.isObjectValid(json_6))//will be true
    helpers.isObjectValid({value:1}); //will be true
    helpers.isObjectValid({}); //will be false
    helpers.isObjectValid(1); //will be false
   ```
     ---
   **randomNum**: This method is to generate a random number.
   
   ***Example***:
   ```javascript
   const result = helpers.randomNum();
   ```
     ---
   **saveFile**: This method is to `save` a file, by default the files saved `inside` a path of the library, this method is `asynchronous` we recommended use this method with the save file to remove files and the hard drive does not fill up, the method return the path.
   
   ***Example***:
   ```javascript
   const result = helpers.saveFile(image.png);
   ```
     ---
   **validProperties**: This method is to fill properties of an object from another two objects, this method receives four parameters, the first three are required and should be objects, the last is optional and is boolean by default this is false.
   
   When last parameter is false and any of two object not contains the keys or keys without values to fill the keys of the first object then the result will be a `false`.
   
   When last parameter is `true` the process will be same but for fill keys from the first object, but if keys and values not present in any of two other objects, the keys and values of the first object will be the same that initial call.
   
   This method is `asynchronous` and return a `boolean` true if all valid and false if fail.
   
   ***Examples***: 
   ```javascript
   //fisrt example-will be false and fail process
   const result = await helpers.validProperties({value:null, value2: null}, {},{});
   
   //second example-will be true and result will be {value:'data', value2: 'test'}
   const result = await helpers.validProperties({value:'data', value2: 'test'}, {},{}, true);
   
   //second example-will be true 
   //result will be {value:12, value2: 'hi'}
   const result = await helpers.validProperties({value:null, value2: null}, {value:12},{value2:'hi'}, true);
   
   //third example-will be true 
   //result will be {value:12, value2: 'hello'}
   const result = await helpers.validProperties({
    value: null,
    value2: null
    }, {
        value: 12,
        value2: 'hello'
    }, {
        value2: 'hi'
    }, true);
   
   //fourth example-will be false 
   //result fail
   const result = await helpers.validProperties({
    value: null,
    value2: null
    }, {
        key: 12,
        another: 'hello'
    }, {
        letter: 'hi'
    });
   
   //fifth example-will be true 
   //result will be {value:'i am value', value2: 'another'}
   const result = await helpers.validProperties({
    value: 'i am value',
    value2: 'another'
    }, {
        key: 12,
        another: 'hello'
    }, {
        letter: 'hi'
    }, true);
   ```
  
  > By default the keys and values are search in the second object if not present search in the second object
  
    ---
   
   **validateBase64**: Method to validate if string is a `base64` or `base64url`.

   ***Example***:
   ```javascript
   if (helpers.validateBase64(properties.content)) {
    ...any
    }
   ```
  **validateHex**: Method to validate if any parameter is a `hex` encoding
  
  ***Example***:
  ```javascript
     if (helpers.validateHex(properties.content)) {
      ...any
      }
  ```

  **validateXMLStructure**: This method is to valida structure xml content, this method can receive data in encoding `ascii`, `base64`, `base64url`, `binary`, `latin1`(ISO), `utf-8` to `utf8`. 
  
  If structure xml is valid method returns true if not returns false

  ***Examples***:
  ```javascript
  //read xml files with difereent data and validate it with the method validateXMLStructure
  
  const invoice = await fs.readFile('files/invoice.xml', 'base64');
  const books = await fs.readFile('files/books.xml', 'base64');
  const resultE = await fs.readFile('files/expectResult.xml', 'base64');
  const sat = await fs.readFile('files/sat.xml', 'base64');
  const resultBase64 = await fs.readFile('files/result.xml', 'base64');
  const rawXML = await fs.readFile('files/result.xml');
  
  console.log(helpers.validateXMLStructure(invoice))
  console.log(helpers.validateXMLStructure(books))
  console.log(helpers.validateXMLStructure(resultE))
  console.log(helpers.validateXMLStructure(sat))
  console.log(helpers.validateXMLStructure(resultBase64))
  console.log(helpers.validateXMLStructure(rawXML))
  console.log(helpers.validateXMLStructure(undefined))
  console.log(helpers.validateXMLStructure(null))
  ```

* ### 3.2. Methods for create logs

  These are the following methods for create logs physically or view in the console on the machine or server.
  
  **debug**: This method is to write a log with level `debug`.
   >The color of the messages in terminal will be blue
   
   ***Example***:
   ```javascript
   const optProps = {
    elementDelimiter: '*',
    endOfLine: '\n',
    format: true,
    segmentTerminator: '~',
    subElementDelimiter: '>'
   };
   log.debug('Any text', optProps);
   ```
     ---
  
  **error**: This method is to write a log with level `error`.
   >The color of the messages in terminal will be red
   
   ***Example***:
   ```javascript
   const optProps = {
    elementDelimiter: '*',
    endOfLine: '\n',
    format: true,
    segmentTerminator: '~',
    subElementDelimiter: '>'
   };
   log.error('Any text', optProps);
   ```
     ---
     
   **info**: This method is to write a log with level `info`.
   >The color of the messages in terminal will be green
   
   ***Example***:
   ```javascript
   const optProps = {
    elementDelimiter: '*',
    endOfLine: '\n',
    format: true,
    segmentTerminator: '~',
    subElementDelimiter: '>'
   };
   log.info('Any text', optProps);
   ```
     ---
     
   **warn**: This method is to write a log with level `warn`.
   >The color of the messages in terminal will be yellow
   
   ***Example***:
   ```javascript
   const optProps = {
    elementDelimiter: '*',
    endOfLine: '\n',
    format: true,
    segmentTerminator: '~',
    subElementDelimiter: '>'
   };
   log.warn('Any text', optProps);
   ```
   >**In any methods of logs we can send any number of parameters**.

---
_Source: https://npm.io/package/utils-nxg-cg · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
