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