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

utils-limited

v1.1.6

Published

Library with different methods for use in each component from OIH

Downloads

13

Readme

N|Solid

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:

    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:

    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:

    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:

    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:

    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:

    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:

    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:

    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:

    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:

    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:

    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:

    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:

    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:

    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:

    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:

    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:

    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:

    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:

    const result = helpers.convertToUtf8(properties.content);

    deleteFile: This method is to remove a file, the arg is a path , this method is asynchronous.

    Example:

    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

    const result = helpers.helperDirectory(logs);

    Result:

    In this second example create a directory inside directory node_modules/utils-nxg-cg a root project from nodejs application

    const result = helpers.helperDirectory(logs, true);

    Result:

    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:

     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": [
                     "[email protected]",
                     "[email protected]"
                 ],
                 "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:

     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": [
                     "[email protected]",
                     "[email protected]"
                 ],
                 "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:

    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:

    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:

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

    if (helpers.validateBase64(properties.content)) {
     ...any
     }

    validateHex: Method to validate if any parameter is a hex encoding

    Example:

       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:

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

    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:

    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:

    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:

    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.