@promptbook/utils 0.44.0-0 → 0.44.0-1

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/umd/index.umd.js CHANGED
@@ -409,7 +409,7 @@
409
409
  /**
410
410
  * The version of the Promptbook library
411
411
  */
412
- var PROMPTBOOK_VERSION = '0.43.0';
412
+ var PROMPTBOOK_VERSION = '0.44.0-0';
413
413
 
414
414
  /**
415
415
  * Parses the given script and returns the list of all used variables that are not defined in the script
@@ -1682,11 +1682,17 @@
1682
1682
  return text.split(/\n\s*\n/).filter(function (paragraph) { return paragraph.trim() !== ''; }).length;
1683
1683
  }
1684
1684
 
1685
+ /**
1686
+ * Split text into sentences
1687
+ */
1688
+ function splitIntoSentences(text) {
1689
+ return text.split(/[.!?]+/).filter(function (sentence) { return sentence.trim() !== ''; });
1690
+ }
1685
1691
  /**
1686
1692
  * Counts number of sentences in the text
1687
1693
  */
1688
1694
  function countSentences(text) {
1689
- return text.split(/[.!?]+/).filter(function (sentence) { return sentence.trim() !== ''; }).length;
1695
+ return splitIntoSentences(text).length;
1690
1696
  }
1691
1697
 
1692
1698
  var defaultDiacriticsRemovalMap = [
@@ -1982,146 +1988,6 @@
1982
1988
  }
1983
1989
  }
1984
1990
 
1985
- /**
1986
- * Extracts code block from markdown.
1987
- *
1988
- * Note: If there are multiple or no code blocks the function throws an error
1989
- *
1990
- * Note: There are 3 simmilar function:
1991
- * - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
1992
- * - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
1993
- * - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
1994
- */
1995
- function extractBlock(markdown) {
1996
- var content = extractOneBlockFromMarkdown(markdown).content;
1997
- return content;
1998
- }
1999
- //
2000
-
2001
- /**
2002
- * Removes quotes from a string
2003
- *
2004
- * Tip: This is very usefull for post-processing of the result of the LLM model
2005
- * Note: This function removes only the same quotes from the beginning and the end of the string
2006
- * Note: There are two simmilar functions:
2007
- * - `removeQuotes` which removes only bounding quotes
2008
- * - `unwrapResult` which removes whole introduce sentence
2009
- *
2010
- * @param text optionally quoted text
2011
- * @returns text without quotes
2012
- */
2013
- function removeQuotes(text) {
2014
- if (text.startsWith('"') && text.endsWith('"')) {
2015
- return text.slice(1, -1);
2016
- }
2017
- if (text.startsWith('\'') && text.endsWith('\'')) {
2018
- return text.slice(1, -1);
2019
- }
2020
- return text;
2021
- }
2022
-
2023
- /**
2024
- * Function trimCodeBlock will trim starting and ending code block from the string if it is present.
2025
- *
2026
- * Note: This is usefull for post-processing of the result of the chat LLM model
2027
- * when the model wraps the result in the (markdown) code block.
2028
- *
2029
- */
2030
- function trimCodeBlock(value) {
2031
- value = spaceTrim__default["default"](value);
2032
- if (!/^```[a-z]*(.*)```$/is.test(value)) {
2033
- return value;
2034
- }
2035
- value = value.replace(/^```[a-z]*/i, '');
2036
- value = value.replace(/```$/i, '');
2037
- value = spaceTrim__default["default"](value);
2038
- return value;
2039
- }
2040
-
2041
- /**
2042
- * Function trimEndOfCodeBlock will remove ending code block from the string if it is present.
2043
- *
2044
- * Note: This is usefull for post-processing of the result of the completion LLM model
2045
- * if you want to start code block in the prompt but you don't want to end it in the result.
2046
- */
2047
- function trimEndOfCodeBlock(value) {
2048
- value = spaceTrim__default["default"](value);
2049
- value = value.replace(/```$/g, '');
2050
- value = spaceTrim__default["default"](value);
2051
- return value;
2052
- }
2053
-
2054
- /**
2055
- * Removes quotes and optional introduce text from a string
2056
- *
2057
- * Tip: This is very usefull for post-processing of the result of the LLM model
2058
- * Note: This function trims the text and removes whole introduce sentence if it is present
2059
- * Note: There are two simmilar functions:
2060
- * - `removeQuotes` which removes only bounding quotes
2061
- * - `unwrapResult` which removes whole introduce sentence
2062
- *
2063
- * @param text optionally quoted text
2064
- * @returns text without quotes
2065
- */
2066
- function unwrapResult(text, options) {
2067
- var _a = options || {}, _b = _a.isTrimmed, isTrimmed = _b === void 0 ? true : _b, _c = _a.isIntroduceSentenceRemoved, isIntroduceSentenceRemoved = _c === void 0 ? true : _c;
2068
- var trimmedText = text;
2069
- // Remove leading and trailing spaces and newlines
2070
- if (isTrimmed) {
2071
- trimmedText = spaceTrim__default["default"](trimmedText);
2072
- }
2073
- var processedText = trimmedText;
2074
- if (isIntroduceSentenceRemoved) {
2075
- var introduceSentenceRegex = /^[a-zěščřžýáíéúů:\s]*:\s*/i;
2076
- if (introduceSentenceRegex.test(text)) {
2077
- // Remove the introduce sentence and quotes by replacing it with an empty string
2078
- processedText = processedText.replace(introduceSentenceRegex, '');
2079
- }
2080
- processedText = spaceTrim__default["default"](processedText);
2081
- }
2082
- if (processedText.length < 3) {
2083
- return trimmedText;
2084
- }
2085
- if (processedText.includes('\n')) {
2086
- return trimmedText;
2087
- }
2088
- // Remove the quotes by extracting the substring without the first and last characters
2089
- var unquotedText = processedText.slice(1, -1);
2090
- // Check if the text starts and ends with quotes
2091
- if ([
2092
- ['"', '"'],
2093
- ["'", "'"],
2094
- ['`', '`'],
2095
- ['*', '*'],
2096
- ['_', '_'],
2097
- ['„', '“'],
2098
- ['«', '»'] /* <- QUOTES to config */,
2099
- ].some(function (_a) {
2100
- var _b = __read(_a, 2), startQuote = _b[0], endQuote = _b[1];
2101
- if (!processedText.startsWith(startQuote)) {
2102
- return false;
2103
- }
2104
- if (!processedText.endsWith(endQuote)) {
2105
- return false;
2106
- }
2107
- if (unquotedText.includes(startQuote) && !unquotedText.includes(endQuote)) {
2108
- return false;
2109
- }
2110
- if (!unquotedText.includes(startQuote) && unquotedText.includes(endQuote)) {
2111
- return false;
2112
- }
2113
- return true;
2114
- })) {
2115
- return unwrapResult(unquotedText, { isTrimmed: false, isIntroduceSentenceRemoved: false });
2116
- }
2117
- else {
2118
- return processedText;
2119
- }
2120
- }
2121
- /**
2122
- * TODO: [🧠] Should this also unwrap the (parenthesis)
2123
- */
2124
-
2125
1991
  /**
2126
1992
  * Makes first letter of a string uppercase
2127
1993
  *
@@ -2428,6 +2294,158 @@
2428
2294
  return true;
2429
2295
  }
2430
2296
 
2297
+ /**
2298
+ * Extracts code block from markdown.
2299
+ *
2300
+ * Note: If there are multiple or no code blocks the function throws an error
2301
+ *
2302
+ * Note: There are 3 simmilar function:
2303
+ * - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
2304
+ * - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
2305
+ * - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
2306
+ */
2307
+ function extractBlock(markdown) {
2308
+ var content = extractOneBlockFromMarkdown(markdown).content;
2309
+ return content;
2310
+ }
2311
+ //
2312
+
2313
+ /**
2314
+ * Removes quotes from a string
2315
+ *
2316
+ * Tip: This is very usefull for post-processing of the result of the LLM model
2317
+ * Note: This function removes only the same quotes from the beginning and the end of the string
2318
+ * Note: There are two simmilar functions:
2319
+ * - `removeQuotes` which removes only bounding quotes
2320
+ * - `unwrapResult` which removes whole introduce sentence
2321
+ *
2322
+ * @param text optionally quoted text
2323
+ * @returns text without quotes
2324
+ */
2325
+ function removeQuotes(text) {
2326
+ if (text.startsWith('"') && text.endsWith('"')) {
2327
+ return text.slice(1, -1);
2328
+ }
2329
+ if (text.startsWith('\'') && text.endsWith('\'')) {
2330
+ return text.slice(1, -1);
2331
+ }
2332
+ return text;
2333
+ }
2334
+
2335
+ /**
2336
+ * Function trimCodeBlock will trim starting and ending code block from the string if it is present.
2337
+ *
2338
+ * Note: This is usefull for post-processing of the result of the chat LLM model
2339
+ * when the model wraps the result in the (markdown) code block.
2340
+ *
2341
+ */
2342
+ function trimCodeBlock(value) {
2343
+ value = spaceTrim__default["default"](value);
2344
+ if (!/^```[a-z]*(.*)```$/is.test(value)) {
2345
+ return value;
2346
+ }
2347
+ value = value.replace(/^```[a-z]*/i, '');
2348
+ value = value.replace(/```$/i, '');
2349
+ value = spaceTrim__default["default"](value);
2350
+ return value;
2351
+ }
2352
+
2353
+ /**
2354
+ * Function trimEndOfCodeBlock will remove ending code block from the string if it is present.
2355
+ *
2356
+ * Note: This is usefull for post-processing of the result of the completion LLM model
2357
+ * if you want to start code block in the prompt but you don't want to end it in the result.
2358
+ */
2359
+ function trimEndOfCodeBlock(value) {
2360
+ value = spaceTrim__default["default"](value);
2361
+ value = value.replace(/```$/g, '');
2362
+ value = spaceTrim__default["default"](value);
2363
+ return value;
2364
+ }
2365
+
2366
+ /**
2367
+ * Removes quotes and optional introduce text from a string
2368
+ *
2369
+ * Tip: This is very usefull for post-processing of the result of the LLM model
2370
+ * Note: This function trims the text and removes whole introduce sentence if it is present
2371
+ * Note: There are two simmilar functions:
2372
+ * - `removeQuotes` which removes only bounding quotes
2373
+ * - `unwrapResult` which removes whole introduce sentence
2374
+ *
2375
+ * @param text optionally quoted text
2376
+ * @returns text without quotes
2377
+ */
2378
+ function unwrapResult(text, options) {
2379
+ var _a = options || {}, _b = _a.isTrimmed, isTrimmed = _b === void 0 ? true : _b, _c = _a.isIntroduceSentenceRemoved, isIntroduceSentenceRemoved = _c === void 0 ? true : _c;
2380
+ var trimmedText = text;
2381
+ // Remove leading and trailing spaces and newlines
2382
+ if (isTrimmed) {
2383
+ trimmedText = spaceTrim__default["default"](trimmedText);
2384
+ }
2385
+ var processedText = trimmedText;
2386
+ if (isIntroduceSentenceRemoved) {
2387
+ var introduceSentenceRegex = /^[a-zěščřžýáíéúů:\s]*:\s*/i;
2388
+ if (introduceSentenceRegex.test(text)) {
2389
+ // Remove the introduce sentence and quotes by replacing it with an empty string
2390
+ processedText = processedText.replace(introduceSentenceRegex, '');
2391
+ }
2392
+ processedText = spaceTrim__default["default"](processedText);
2393
+ }
2394
+ if (processedText.length < 3) {
2395
+ return trimmedText;
2396
+ }
2397
+ if (processedText.includes('\n')) {
2398
+ return trimmedText;
2399
+ }
2400
+ // Remove the quotes by extracting the substring without the first and last characters
2401
+ var unquotedText = processedText.slice(1, -1);
2402
+ // Check if the text starts and ends with quotes
2403
+ if ([
2404
+ ['"', '"'],
2405
+ ["'", "'"],
2406
+ ['`', '`'],
2407
+ ['*', '*'],
2408
+ ['_', '_'],
2409
+ ['„', '“'],
2410
+ ['«', '»'] /* <- QUOTES to config */,
2411
+ ].some(function (_a) {
2412
+ var _b = __read(_a, 2), startQuote = _b[0], endQuote = _b[1];
2413
+ if (!processedText.startsWith(startQuote)) {
2414
+ return false;
2415
+ }
2416
+ if (!processedText.endsWith(endQuote)) {
2417
+ return false;
2418
+ }
2419
+ if (unquotedText.includes(startQuote) && !unquotedText.includes(endQuote)) {
2420
+ return false;
2421
+ }
2422
+ if (!unquotedText.includes(startQuote) && unquotedText.includes(endQuote)) {
2423
+ return false;
2424
+ }
2425
+ return true;
2426
+ })) {
2427
+ return unwrapResult(unquotedText, { isTrimmed: false, isIntroduceSentenceRemoved: false });
2428
+ }
2429
+ else {
2430
+ return processedText;
2431
+ }
2432
+ }
2433
+ /**
2434
+ * TODO: [🧠] Should this also unwrap the (parenthesis)
2435
+ */
2436
+
2437
+ // And the normalization (originally n12 library) utilities:
2438
+ var normalizeTo = {
2439
+ camelCase: normalizeTo_camelCase,
2440
+ PascalCase: normalizeTo_PascalCase,
2441
+ 'SCREAMING-CASE': normalizeTo_SCREAMING_CASE,
2442
+ snake_case: normalizeTo_snake_case,
2443
+ 'kebab-case': normalizeToKebabCase,
2444
+ };
2445
+ /**
2446
+ * TODO: [🧠] Maybe create some indipendent package like `markdown-tools` from both here exported and @private utilities
2447
+ */
2448
+
2431
2449
  exports.CountUtils = CountUtils;
2432
2450
  exports.DIACRITIC_VARIANTS_LETTERS = DIACRITIC_VARIANTS_LETTERS;
2433
2451
  exports.ExecutionReportStringOptionsDefaults = ExecutionReportStringOptionsDefaults;
@@ -2449,6 +2467,7 @@
2449
2467
  exports.isValidKeyword = isValidKeyword;
2450
2468
  exports.nameToUriPart = nameToUriPart;
2451
2469
  exports.nameToUriParts = nameToUriParts;
2470
+ exports.normalizeTo = normalizeTo;
2452
2471
  exports.normalizeToKebabCase = normalizeToKebabCase;
2453
2472
  exports.normalizeTo_PascalCase = normalizeTo_PascalCase;
2454
2473
  exports.normalizeTo_SCREAMING_CASE = normalizeTo_SCREAMING_CASE;
@@ -2466,6 +2485,7 @@
2466
2485
  exports.removeQuotes = removeQuotes;
2467
2486
  exports.replaceParameters = replaceParameters;
2468
2487
  exports.searchKeywords = searchKeywords;
2488
+ exports.splitIntoSentences = splitIntoSentences;
2469
2489
  exports.trimCodeBlock = trimCodeBlock;
2470
2490
  exports.trimEndOfCodeBlock = trimEndOfCodeBlock;
2471
2491
  exports.unwrapResult = unwrapResult;