@truto/truto-jsonata 1.0.47 → 1.0.48

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/README.md CHANGED
@@ -2116,6 +2116,53 @@ expression.evaluate({ input, possibleValues, threshold }).then(result => { conso
2116
2116
 
2117
2117
  </details>
2118
2118
 
2119
+ <details>
2120
+ <summary>diceCoefficient(value1, value2)</summary>
2121
+
2122
+ Calculates the Dice Coefficient similarity score between two strings. The Dice Coefficient measures the similarity of two strings based on their bigrams (pairs of adjacent characters). The function normalizes both strings (converts to lowercase and removes non-alphanumeric characters) before comparison.
2123
+
2124
+ **Parameters:**
2125
+
2126
+ - **value1**: The first string to compare.
2127
+ - **value2**: The second string to compare.
2128
+
2129
+ **Returns:**
2130
+
2131
+ A number between `0.0` (completely different) and `1.0` (identical), representing the similarity score.
2132
+
2133
+ **Example Usage:**
2134
+
2135
+ ```javascript
2136
+ import trutoJsonata from '@truto/truto-jsonata';
2137
+
2138
+ // Example 1: Identical strings return 1.0
2139
+ const expression1 = trutoJsonata("$diceCoefficient('hello', 'hello')");
2140
+ expression1.evaluate({}).then(result => { console.log(result); });
2141
+ // Output: 1.0
2142
+
2143
+ // Example 2: Similar strings return a score between 0 and 1
2144
+ const expression2 = trutoJsonata("$diceCoefficient('apple', 'appl')");
2145
+ expression2.evaluate({}).then(result => { console.log(result); });
2146
+ // Output: 0.8 (or similar value depending on similarity)
2147
+
2148
+ // Example 3: Case-insensitive comparison
2149
+ const expression3 = trutoJsonata("$diceCoefficient('Hello', 'HELLO')");
2150
+ expression3.evaluate({}).then(result => { console.log(result); });
2151
+ // Output: 1.0
2152
+
2153
+ // Example 4: Non-alphanumeric characters are ignored
2154
+ const expression4 = trutoJsonata("$diceCoefficient('hello-world', 'hello world')");
2155
+ expression4.evaluate({}).then(result => { console.log(result); });
2156
+ // Output: 1.0
2157
+
2158
+ // Example 5: Completely different strings return 0.0
2159
+ const expression5 = trutoJsonata("$diceCoefficient('hello', 'xyz')");
2160
+ expression5.evaluate({}).then(result => { console.log(result); });
2161
+ // Output: 0.0
2162
+ ```
2163
+
2164
+ </details>
2165
+
2119
2166
  <details>
2120
2167
  <summary>sortNodes(array, idKey = 'id',
2121
2168
  parentIdKey = 'parent_id',
@@ -50641,6 +50641,76 @@ var digest = async (text, algorithm = "SHA-256", stringType = "hex") => {
50641
50641
  };
50642
50642
  var digest_default = digest;
50643
50643
 
50644
+ // node_modules/n-gram/index.js
50645
+ var bigram = nGram(2);
50646
+ var trigram = nGram(3);
50647
+ function nGram(n2) {
50648
+ if (typeof n2 !== "number" || Number.isNaN(n2) || n2 < 1 || n2 === Number.POSITIVE_INFINITY) {
50649
+ throw new Error("`" + n2 + "` is not a valid argument for `n-gram`");
50650
+ }
50651
+ return grams;
50652
+ function grams(value) {
50653
+ const nGrams = [];
50654
+ if (value === null || value === undefined) {
50655
+ return nGrams;
50656
+ }
50657
+ const source = typeof value.slice === "function" ? value : String(value);
50658
+ let index = source.length - n2 + 1;
50659
+ if (index < 1) {
50660
+ return nGrams;
50661
+ }
50662
+ while (index--) {
50663
+ nGrams[index] = source.slice(index, index + n2);
50664
+ }
50665
+ return nGrams;
50666
+ }
50667
+ }
50668
+
50669
+ // node_modules/dice-coefficient/index.js
50670
+ function diceCoefficient(value, other2) {
50671
+ const left = toPairs(value);
50672
+ const right = toPairs(other2);
50673
+ let index = -1;
50674
+ let intersections = 0;
50675
+ while (++index < left.length) {
50676
+ const leftPair = left[index];
50677
+ let offset2 = -1;
50678
+ while (++offset2 < right.length) {
50679
+ const rightPair = right[offset2];
50680
+ if (leftPair === rightPair) {
50681
+ intersections++;
50682
+ right[offset2] = "";
50683
+ break;
50684
+ }
50685
+ }
50686
+ }
50687
+ return 2 * intersections / (left.length + right.length);
50688
+ }
50689
+ function toPairs(value) {
50690
+ if (Array.isArray(value)) {
50691
+ return value.map((bigram2) => normalize(bigram2));
50692
+ }
50693
+ const normal = normalize(value);
50694
+ return normal.length === 1 ? [normal] : bigram(normal);
50695
+ }
50696
+ function normalize(value) {
50697
+ return String(value).toLowerCase();
50698
+ }
50699
+
50700
+ // src/functions/diceCoefficient.ts
50701
+ function getNormalizedString(value) {
50702
+ return value.toLowerCase().replace(/[^a-z0-9]/g, "");
50703
+ }
50704
+ function _diceCoefficient(value, value2) {
50705
+ if (value === value2) {
50706
+ return 1;
50707
+ }
50708
+ const normalizedValue = getNormalizedString(value);
50709
+ const normalizedValue2 = getNormalizedString(value2);
50710
+ return diceCoefficient(normalizedValue, normalizedValue2);
50711
+ }
50712
+ var diceCoefficient_default = _diceCoefficient;
50713
+
50644
50714
  // src/functions/dtFromFormat.ts
50645
50715
  function dtFromFormat(date, format, options3) {
50646
50716
  if (format === "RFC2822") {
@@ -51448,70 +51518,14 @@ function jsToXml_default(json, options3 = { compact: true, spaces: 4 }) {
51448
51518
  return import_xml_js.js2xml(json, options3);
51449
51519
  }
51450
51520
 
51451
- // node_modules/n-gram/index.js
51452
- var bigram = nGram(2);
51453
- var trigram = nGram(3);
51454
- function nGram(n2) {
51455
- if (typeof n2 !== "number" || Number.isNaN(n2) || n2 < 1 || n2 === Number.POSITIVE_INFINITY) {
51456
- throw new Error("`" + n2 + "` is not a valid argument for `n-gram`");
51457
- }
51458
- return grams;
51459
- function grams(value) {
51460
- const nGrams = [];
51461
- if (value === null || value === undefined) {
51462
- return nGrams;
51463
- }
51464
- const source = typeof value.slice === "function" ? value : String(value);
51465
- let index = source.length - n2 + 1;
51466
- if (index < 1) {
51467
- return nGrams;
51468
- }
51469
- while (index--) {
51470
- nGrams[index] = source.slice(index, index + n2);
51471
- }
51472
- return nGrams;
51473
- }
51474
- }
51475
-
51476
- // node_modules/dice-coefficient/index.js
51477
- function diceCoefficient(value, other2) {
51478
- const left = toPairs(value);
51479
- const right = toPairs(other2);
51480
- let index = -1;
51481
- let intersections = 0;
51482
- while (++index < left.length) {
51483
- const leftPair = left[index];
51484
- let offset2 = -1;
51485
- while (++offset2 < right.length) {
51486
- const rightPair = right[offset2];
51487
- if (leftPair === rightPair) {
51488
- intersections++;
51489
- right[offset2] = "";
51490
- break;
51491
- }
51492
- }
51493
- }
51494
- return 2 * intersections / (left.length + right.length);
51495
- }
51496
- function toPairs(value) {
51497
- if (Array.isArray(value)) {
51498
- return value.map((bigram2) => normalize(bigram2));
51499
- }
51500
- const normal = normalize(value);
51501
- return normal.length === 1 ? [normal] : bigram(normal);
51502
- }
51503
- function normalize(value) {
51504
- return String(value).toLowerCase();
51505
- }
51506
-
51507
51521
  // src/functions/mostSimilar.ts
51508
- function getNormalizedString(value) {
51522
+ function getNormalizedString2(value) {
51509
51523
  return value.toLowerCase().replace(/[^a-z0-9]/g, "");
51510
51524
  }
51511
51525
  function mostSimilar(value, possibleValues, threshold = 0.8) {
51512
- const normalizedValue = getNormalizedString(value);
51526
+ const normalizedValue = getNormalizedString2(value);
51513
51527
  const result = reduce_default(possibleValues, (acc, possibleValue) => {
51514
- const normalizedPossibleValue = getNormalizedString(possibleValue);
51528
+ const normalizedPossibleValue = getNormalizedString2(possibleValue);
51515
51529
  const similarity = diceCoefficient(normalizedValue, normalizedPossibleValue);
51516
51530
  if (similarity > acc.similarity) {
51517
51531
  return { similarity, value: possibleValue };
@@ -67877,6 +67891,7 @@ function registerJsonataExtensions(expression) {
67877
67891
  expression.registerFunction("uuid", uuid_default);
67878
67892
  expression.registerFunction("getArrayBuffer", getArrayBuffer_default);
67879
67893
  expression.registerFunction("mostSimilar", mostSimilar_default);
67894
+ expression.registerFunction("diceCoefficient", diceCoefficient_default);
67880
67895
  expression.registerFunction("sortNodes", sortNodes_default);
67881
67896
  expression.registerFunction("blob", blob_default);
67882
67897
  expression.registerFunction("convertHtmlToMarkdown", convertHtmlToMarkdown_default);
@@ -67950,4 +67965,4 @@ export {
67950
67965
  trutoJsonata as default
67951
67966
  };
67952
67967
 
67953
- //# debugId=E22AACED232C3A8A64756E2164756E21
67968
+ //# debugId=19179F04099D0A3864756E2164756E21