@memberjunction/global
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -13,2 +13,15 @@ /** | ||
export declare function CopyScalarsAndArrays<T extends object>(input: T): Partial<T>; | ||
/** | ||
* This function takes in an input string and attempts to clean it up to return a valid JSON string. This function will attempt to extract JSON from a Markdown code block if it exists, | ||
* otherwise it will attempt to extract JSON from the input string itself. If the input string is not valid JSON, this function will return null. | ||
* @param inputString | ||
* @returns | ||
*/ | ||
export declare function CleanJSON(inputString: string | null): string | null; | ||
/** | ||
* This function takes in a string that may contain JavaScript code in a markdown code block and returns the JavaScript code without the code block. | ||
* @param javaScriptCode | ||
* @returns | ||
*/ | ||
export declare function CleanJavaScript(javaScriptCode: string): string; | ||
//# sourceMappingURL=util.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.CopyScalarsAndArrays = exports.GetGlobalObjectStore = void 0; | ||
exports.CleanJavaScript = exports.CleanJSON = exports.CopyScalarsAndArrays = exports.GetGlobalObjectStore = void 0; | ||
/** | ||
@@ -65,2 +65,78 @@ * The Global Object Store is a place to store global objects that need to be shared across the application. Depending on the execution environment, this could be the window object in a browser, or the global object in a node environment. | ||
exports.CopyScalarsAndArrays = CopyScalarsAndArrays; | ||
/** | ||
* This function takes in an input string and attempts to clean it up to return a valid JSON string. This function will attempt to extract JSON from a Markdown code block if it exists, | ||
* otherwise it will attempt to extract JSON from the input string itself. If the input string is not valid JSON, this function will return null. | ||
* @param inputString | ||
* @returns | ||
*/ | ||
function CleanJSON(inputString) { | ||
if (!inputString) | ||
return null; | ||
// replace all \n and \t with nothing | ||
const jsonString = inputString.trim().replace(/\n/g, "").replace(/\t/g, ""); | ||
// Regular expression to match JavaScript code blocks within Markdown fences | ||
// This regex looks for ``` optionally followed by js or javascript (case-insensitive), then captures until the closing ``` | ||
const markdownRegex = /```(?:json|JSON)?\s*([\s\S]*?)```/gi; | ||
// Check if the input contains Markdown code fences for JavaScript | ||
const matches = Array.from(jsonString.matchAll(markdownRegex)); | ||
if (matches.length > 0) { | ||
// If there are matches, concatenate all captured groups (in case there are multiple code blocks) | ||
return matches.map(match => match[1].trim()).join('\n'); | ||
} | ||
else { | ||
// If there are no Markdown code fences, we could have a string that contains JSON, or is JUST JSON | ||
// Attempt to extract JSON from a mixed string | ||
const firstBracketIndex = jsonString.indexOf('['); | ||
const firstBraceIndex = jsonString.indexOf('{'); | ||
let startIndex = -1; | ||
let endIndex = -1; | ||
// Determine the starting index based on the position of the first '[' and '{' | ||
if ((firstBracketIndex !== -1 && firstBracketIndex < firstBraceIndex) || firstBraceIndex === -1) { | ||
startIndex = firstBracketIndex; | ||
endIndex = jsonString.lastIndexOf(']'); | ||
} | ||
else if (firstBraceIndex !== -1) { | ||
startIndex = firstBraceIndex; | ||
endIndex = jsonString.lastIndexOf('}'); | ||
} | ||
if (startIndex === -1 || endIndex === -1 || endIndex < startIndex) { | ||
console.warn("No JSON found in the input."); | ||
return jsonString.trim(); | ||
} | ||
const potentialJSON = jsonString.substring(startIndex, endIndex + 1); | ||
try { | ||
// Parse and stringify to format the JSON nicely | ||
// and to validate it's indeed a valid JSON. | ||
const jsonObject = JSON.parse(potentialJSON); | ||
return JSON.stringify(jsonObject, null, 2); | ||
} | ||
catch (error) { | ||
console.error("Failed to parse extracted string as JSON:", error); | ||
// Return null or potentially invalid JSON text as a fallback | ||
return null; | ||
} | ||
} | ||
} | ||
exports.CleanJSON = CleanJSON; | ||
/** | ||
* This function takes in a string that may contain JavaScript code in a markdown code block and returns the JavaScript code without the code block. | ||
* @param javaScriptCode | ||
* @returns | ||
*/ | ||
function CleanJavaScript(javaScriptCode) { | ||
// Regular expression to match JavaScript code blocks within Markdown fences | ||
// This regex looks for ``` optionally followed by js or javascript (case-insensitive), then captures until the closing ``` | ||
const markdownRegex = /```(?:js|javascript)?\s*([\s\S]*?)```/gi; | ||
// Check if the input contains Markdown code fences for JavaScript | ||
const matches = Array.from(javaScriptCode.matchAll(markdownRegex)); | ||
if (matches.length > 0) { | ||
// If there are matches, concatenate all captured groups (in case there are multiple code blocks) | ||
return matches.map(match => match[1].trim()).join('\n'); | ||
} | ||
else { | ||
// If there are no Markdown code fences, assume the input is plain JavaScript code | ||
return javaScriptCode.trim(); | ||
} | ||
} | ||
exports.CleanJavaScript = CleanJavaScript; | ||
//# sourceMappingURL=util.js.map |
{ | ||
"name": "@memberjunction/global", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "MemberJunction: Global Object - Needed for ALL other MJ components", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
45496
623