javascript-utils
Reactive icon

JavaScript Utils

icon_trusted
Stable version 2.0.2 (Compatible with OutSystems 11)
Other versions available for 10
Uploaded
 on 14 July 2021
 by 
4.5
 (11 ratings)
javascript-utils

JavaScript Utils

Documentation
2.0.2

How to use JavaScript Utils


Description

This component containing JavaScript utilities to be used in intermediate to advanced scenarios, such as safe JSON string parsing.



(Function) CorrectJSONStringForParse


Strips a JSON object string of whitespaces, wraps keys in double-quotes and replaces any occurrences of single-quotes to double quotes, in order to make it compatible with JSON.parse. Based off of https://stackoverflow.com/a/26291352/1293116

Inputs

JSONString

Type: Text. Mandatory.
 JSON String to be corrected.

Outputs

CorrectedJSONString

Type: Text.
 Result of correction from JSONString.

Example
JSON String: { hello: 'world', places: ['Africa', 'America', 'Asia', 'Australia'] }
CorrectedJSONString:: { "hello": "world", "places": ["Africa", "America", "Asia", "Australia"] }
 



(Function) ReplaceObjectTaggedAttributes


Replaces constant attributes in a JSON object that are wrapped with the Delimiter text. Uses the RootObjectFunction parameter function to obtain values or, if none is provided, uses JavaScript's 'eval'.


Inputs

JSONObject

Type: Object. Mandatory.

Delimiter

Type: Text. Mandatory.

RootObjectFunction

Type: Object. Mandatory.


Outputs

ReplacedJSONObject

Type: Object.

Example



(Function) SafelyEvalStringIntoObject


Does a safe 'eval' of the given string through a switch-case mapping. The first value in the string (the root) will be obtained using the RootObjectFunction. If a complete mapping is not defined for variable, it will return a 'null' object.

 

Inputs

ConstantString

Type: Text. Mandatory.
 JSON String to be corrected.

RootObjectFunction

Function object that will be used to return the first value in the string (the root). This must value must be assigned to an "Object" output parameter.

Type: Object. Mandatory.


Outputs

Object

Type: Object.

Example




(Function) TagConstantsInJSONString


Takes a JSON string, identifies possible constants and wraps them in double quotes and the '§' character to be easily identifiable.


Inputs

JSONString

Type: Text. Mandatory.

ConstantPreffix

Type: Text. Mandatory.

Delimiter

Type: Text. Mandatory.


Outputs

TaggedJSONString

Type: Text.

Example



(Function) TransformConstantsInJSONString


Takes a JSON string, identifies possible constants and applies a transformation to them, according to a given function. NOTE: The given function MUST assign an output parameter named 'Object'. It may have other outputs, but this one contains the value of the intended transformation.


Inputs

JSONString

Type: Text. Mandatory.

ConstantPreffix

Type: Text. Mandatory.

TransformationFunction

Type: Object. Mandatory.


Outputs

TransformedJSONString

Type: Text.

Example


Correct JSON String for parse

Strips a JSON object string of whitespaces, wraps keys in double-quotes and replaces any occurrences of single-quotes to double quotes, in order to make it compatible with JSON.parse.


Input parameters

JSONString

Invalid JSON string to be corrected


Output parameters

CorrectedJSONString

The corrected JSON string


Usage


Correct JSON String for parse with Eval

Attempts to correct an invalid JSON string with JavaScript's 'eval' function.


IMPORTANT: Eval() is a JS function that executes javascript from a string and is an enormous security risk.


Avoid using this action if you can and if not, then only use it if the JSON String to correct is from a trusted source. Use with caution.


Input parameters

JSONString

Invalid JSON string to be corrected

RemoveParentheses

Should parentheses() be removed? This can avoid some code injection on the 'eval' function but can break your JSON string.


Output parameters

CorrectedJSONString

The corrected JSON string


Usage


Replace attributes in a JSON object

Replaces constant attributes in a JSON object that are wrapped with the Delimiter text.
Uses the RootObjectFunction parameter function to obtain values or, if none is provided, uses JavaScript's 'eval'.


IMPORTANT: Eval() is a JS function that executes javascript from a string and is an enormous security risk.


Avoid leaving the RootObjectFunction parameter empty if you can.


Input parameters

JSONObject

JSON Object where the tagged attributes will be replaced

Delimiter

Characters used to identify the constants that are wrapped in this same characters

RootObjectFunction

Function where the values will be replaced at.


Output parameters

ReplacedJSONObject

JSON Object with the values replaced


Usage


Safe eval of string into object

Does a safe 'eval' of the given string through a switch-case mapping. The first value in the string (the root) will be obtained using the RootObjectFunction.
If a complete mapping is not defined for a variable, it will return a 'null' object.


Input parameters

ConstantString

Constant string to be evaluated.

RootObjectFunction

Function object that will be used to return the first value in the string (the root). This value must be assigned to an "Object" output parameter.


Output parameters

Object

Evaluated JSON object


Usage


Tag constant in a JSON string

Takes a JSON string, identifies possible constants and wraps them in double quotes and the Delimiter character to be easily identifiable.


Input parameters

JSONString

JSON string to be tagged

ConstantPrefix

String used to search for constants

Delimiter

Character/s to wrap the found constants


Output parameters

TaggedJSONString

The input JSON string with its constants tagged


Usage


Transform constant in JSON string

Takes a JSON string, identifies possible constants and applies a transformation to them, according to a given function.
NOTE: The given function MUST assign an output parameter named 'Object'. It may have other outputs, but this one contains the value of the intended transformation.


Input parameters

JSONString

JSON string where the constants are going to get transformed

ConstantPrefix

String used to search for constants

TransformationFunction

Function used to replace the constants found


Output parameters

TransformedJSONString

The input JSON string with its constants transformed


Usage