@memberjunction/global 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/util.d.ts CHANGED
@@ -10,4 +10,17 @@ export declare function GetGlobalObjectStore(): typeof globalThis;
10
10
  * @returns
11
11
  */
12
12
  export declare function CopyScalarsAndArrays<T extends object>(input: T): Partial<T>;
13
+ /**
14
+ * 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,
15
+ * 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.
16
+ * @param inputString
17
+ * @returns
18
+ */
19
+ export declare function CleanJSON(inputString: string | null): string | null;
20
+ /**
21
+ * 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.
22
+ * @param javaScriptCode
23
+ * @returns
24
+ */
25
+ export declare function CleanJavaScript(javaScriptCode: string): string;
13
26
  //# sourceMappingURL=util.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,wBAAgB,oBAAoB,sBA4BnC;AAED;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,CAAC,SAAS,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAiB3E"}
1
+ {"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,wBAAgB,oBAAoB,sBA4BnC;AAED;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,CAAC,SAAS,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAiB3E;AAGD;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,WAAW,EAAE,MAAM,GAAG,IAAI,GAAG,MAAM,GAAG,IAAI,CAmDnE;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,cAAc,EAAE,MAAM,GAAG,MAAM,CAe9D"}
package/dist/util.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CopyScalarsAndArrays = exports.GetGlobalObjectStore = void 0;
3
+ exports.CleanJavaScript = exports.CleanJSON = exports.CopyScalarsAndArrays = exports.GetGlobalObjectStore = void 0;
4
4
  /**
5
5
  * 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.
6
6
  * This function will return the appropriate object based on the environment.
@@ -62,4 +62,80 @@ function CopyScalarsAndArrays(input) {
62
62
  return result;
63
63
  }
64
64
  exports.CopyScalarsAndArrays = CopyScalarsAndArrays;
65
+ /**
66
+ * 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,
67
+ * 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.
68
+ * @param inputString
69
+ * @returns
70
+ */
71
+ function CleanJSON(inputString) {
72
+ if (!inputString)
73
+ return null;
74
+ // replace all \n and \t with nothing
75
+ const jsonString = inputString.trim().replace(/\n/g, "").replace(/\t/g, "");
76
+ // Regular expression to match JavaScript code blocks within Markdown fences
77
+ // This regex looks for ``` optionally followed by js or javascript (case-insensitive), then captures until the closing ```
78
+ const markdownRegex = /```(?:json|JSON)?\s*([\s\S]*?)```/gi;
79
+ // Check if the input contains Markdown code fences for JavaScript
80
+ const matches = Array.from(jsonString.matchAll(markdownRegex));
81
+ if (matches.length > 0) {
82
+ // If there are matches, concatenate all captured groups (in case there are multiple code blocks)
83
+ return matches.map(match => match[1].trim()).join('\n');
84
+ }
85
+ else {
86
+ // If there are no Markdown code fences, we could have a string that contains JSON, or is JUST JSON
87
+ // Attempt to extract JSON from a mixed string
88
+ const firstBracketIndex = jsonString.indexOf('[');
89
+ const firstBraceIndex = jsonString.indexOf('{');
90
+ let startIndex = -1;
91
+ let endIndex = -1;
92
+ // Determine the starting index based on the position of the first '[' and '{'
93
+ if ((firstBracketIndex !== -1 && firstBracketIndex < firstBraceIndex) || firstBraceIndex === -1) {
94
+ startIndex = firstBracketIndex;
95
+ endIndex = jsonString.lastIndexOf(']');
96
+ }
97
+ else if (firstBraceIndex !== -1) {
98
+ startIndex = firstBraceIndex;
99
+ endIndex = jsonString.lastIndexOf('}');
100
+ }
101
+ if (startIndex === -1 || endIndex === -1 || endIndex < startIndex) {
102
+ console.warn("No JSON found in the input.");
103
+ return jsonString.trim();
104
+ }
105
+ const potentialJSON = jsonString.substring(startIndex, endIndex + 1);
106
+ try {
107
+ // Parse and stringify to format the JSON nicely
108
+ // and to validate it's indeed a valid JSON.
109
+ const jsonObject = JSON.parse(potentialJSON);
110
+ return JSON.stringify(jsonObject, null, 2);
111
+ }
112
+ catch (error) {
113
+ console.error("Failed to parse extracted string as JSON:", error);
114
+ // Return null or potentially invalid JSON text as a fallback
115
+ return null;
116
+ }
117
+ }
118
+ }
119
+ exports.CleanJSON = CleanJSON;
120
+ /**
121
+ * 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.
122
+ * @param javaScriptCode
123
+ * @returns
124
+ */
125
+ function CleanJavaScript(javaScriptCode) {
126
+ // Regular expression to match JavaScript code blocks within Markdown fences
127
+ // This regex looks for ``` optionally followed by js or javascript (case-insensitive), then captures until the closing ```
128
+ const markdownRegex = /```(?:js|javascript)?\s*([\s\S]*?)```/gi;
129
+ // Check if the input contains Markdown code fences for JavaScript
130
+ const matches = Array.from(javaScriptCode.matchAll(markdownRegex));
131
+ if (matches.length > 0) {
132
+ // If there are matches, concatenate all captured groups (in case there are multiple code blocks)
133
+ return matches.map(match => match[1].trim()).join('\n');
134
+ }
135
+ else {
136
+ // If there are no Markdown code fences, assume the input is plain JavaScript code
137
+ return javaScriptCode.trim();
138
+ }
139
+ }
140
+ exports.CleanJavaScript = CleanJavaScript;
65
141
  //# sourceMappingURL=util.js.map
package/dist/util.js.map CH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H;IAC3H,MAAM,aAAa,GAAG,yCAAyC,CAAC;IAEhE,kEAAkE;IAClE,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,CAAC,cAAc,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAC,CAAC;IAEnE,IAAI,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACrB,iGAAiG;QACjG,OAAO,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC5D,CAAC;SAAM,CAAC;QACJ,kFAAkF;QAClF,OAAO,cAAc,CAAC,IAAI,EAAE,CAAC;IACjC,CAAC;AACL,CAAC;AAfD,0CAeC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@memberjunction/global",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "MemberJunction: Global Object - Needed for ALL other MJ components",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",