Socket
Socket
Sign inDemoInstall

data-interpreter

Package Overview
Dependencies
0
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    data-interpreter

A library to transform / normalize / map a data source (JS Data Structure) using a template


Version published
Maintainers
1
Install size
15.0 kB
Created

Readme

Source

Data Interpreter

A library to transform / normalize / map a data source (JS Data Structure) using a template.

Instalation
  • node: npm install data-interpreter
Basic Documentation
  • constructor([options]) -options: Is an optional object with any of the follow attributes
    • splitChar (String "."): Accesor char, used to indicated inner object attributes in the template
    • defaultValue (Any undefined): Value to be used when no other valued is found from the template in the source
    • literalChar (String "="): Indicates that the following text in the template must be used as literal value
    • reverseMapping (Boolean false): When true, use the absense of literalChar as indication of literal value
  • cast(source, dictionary[, options]) Main function, used to transfor / cast / translate a template / dictionary using a input (Js data structure)
    • source (Object|Array): Source to use as input in the template
    • dictionary (String|Object|Array): The structure of the expected output, using the reference to the source.
    • options (Object): Optional override of the instance options
  • getValueFromObject(source, path)
    • source: (Object|Array): Source to use as input to get the data
    • path: (String|Array): the path to seek the value, using the splitChar as accesor separator. For example "path.to.inner.value" or ["path","to","inner","value"]
  • getType(variableToCheck): Helper function that return a string with the tipe of the variable.
    • variableToCheck (Any)
Use Examples
  • Using a source like:
    const source = {
      stringLvl1                      : "stringValueLvl1",
      arrayLvl1                       : ["arrayValueLvl1"],
      objectLvl1                      : {
          numberLvl2                  : 2,
          booleanLvl2                 : true,
          objectLvl2                  : {
              stringLvl3              : "stringValueLvl3",
          },
      },
      iterableArray                   : [
          {iterableItemAttribute      : "iterableItemValue1"},
          {iterableItemAttribute      : "iterableItemValue2"},
      ],
    }
    
  • And a basic instance of data-interpreter
    const dataInterpreter = require("data-interpreter");
    const interpreter = new dataInterpreter();
    
  • You could:
    • Use a plain dictionary
      interpreter.cast(source, "stringLvl1")
      -> "stringValueLvl1"
      
    • Use an object type dictionary
      interpreter.cast(source, { 
          attrName : "stringLvl1"
      })
      -> { attrName : "stringValueLvl1" }
      
    • Use nested references to the source
      interpreter.cast(source, {
        attrName : "objectLvl1.numberLvl2"
      })
      -> { attrName : 2 }
      
    • Use dictionary as complex as need it
      interpreter.cast(source, { 
        attrName : { 
          innerAttrName : "stringLvl1"
        }
      })
      -> { attrName : { innerAttrName : "stringValueLvl1"} }
      
    • Use dictionary with literal values
      interpreter.cast(source, { attrName : "=LiteralText"})
      -> { attrName : "LiteralText"}
      
    • Use dictionary with objects or arrays as part of its data structures
      interpreter.cast(source, { 
        attrName : [ "stringLvl1", "objectLvl1.numberLvl2" ]
      })
      -> { attrName : ["stringValueLvl1", 2] }
      
    • Iterate collections (By value)
      interpreter.cast(source, { attributeName : [ 
        "$forEach($iterableArray, $iterationItem, $iterationIndex)", 
        "$iterationItem.iterableItemAttribute"
      ]})
      -> {attributeName : ["iterableItemValue1", "iterableItemValue2"]}
      
    • Iterate collections (By index)
      interpreter.cast(source, { attributeName : [ 
        "$forEach($iterableArray, $iterationItem, $iterationIndex)", 
        "iterableArray.$iterationIndex.iterableItemAttribute"
      ]})
      -> {attributeName : ["iterableItemValue1", "iterableItemValue2"]}
      

Keywords

FAQs

Last updated on 11 Oct 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc