@@ -4,6 +4,30 @@ import { isJsonObject } from 'tiny-essentials';
44/** @typedef {import('./TinySqlQuery.mjs').Pcache } Pcache */
55/** @typedef {import('./TinySqlQuery.mjs').TagCriteria } TagCriteria */
66
7+ /**
8+ * Represents a key-value pair extracted from a special chunk format.
9+ *
10+ * @typedef {Object } SpecialFromChunks
11+ * @property {string } key - The key or identifier extracted from the chunk.
12+ * @property {string } value - The associated value linked to the key.
13+ */
14+
15+ /**
16+ * Represents a collection of string chunks used in parsing or filtering.
17+ *
18+ * @typedef {Array<string | string[]> } Chunks
19+ *
20+ * A chunk can be a single string or an array of strings grouped as OR conditions.
21+ */
22+
23+ /**
24+ * Result of parsing a string expression into a column and list of included values.
25+ *
26+ * @typedef {Object } ParseStringResult
27+ * @property {string } column - The SQL column to which the values apply.
28+ * @property {Chunks } include - List of values or grouped OR conditions to be included in the query.
29+ */
30+
731/**
832 * @class PuddySqlTags
933 * @description A powerful utility class for building advanced SQL WHERE clauses with support for tag-based filtering,
@@ -282,16 +306,27 @@ class PuddySqlTags {
282306 const allowWildcards = typeof group . allowWildcards === 'boolean' ? group . allowWildcards : false ;
283307 const include = group . include || [ ] ;
284308
309+ /**
310+ * @param {string } funcName
311+ * @param {string } param
312+ * @param {boolean } [useLike=false]
313+ * @returns {string }
314+ */
285315 const createQuery = ( funcName , param , useLike = false ) =>
286316 `${ funcName } (SELECT 1 FROM ${
287317 this . useJsonEach
288318 ? `${ this . jsonEach } (${ tagsColumn } ) WHERE value ${ useLike ? 'LIKE' : '=' } ${ param } `
289319 : `${ tagsColumn } WHERE ${ tagsColumn } .${ tagsValue } ${ useLike ? 'LIKE' : '=' } ${ param } `
290320 } )`;
291321
322+ /**
323+ * @param {string } tag
324+ * @returns {{ param: string; usesWildcard: boolean; not: boolean; } }
325+ */
292326 const filterTag = ( tag ) => {
293327 const not = tag . startsWith ( '!' ) ;
294328 const cleanTag = not ? tag . slice ( 1 ) : tag ;
329+ if ( typeof pCache . index !== 'number' ) throw new Error ( 'Invalid pCache index' ) ;
295330 const param = `$${ pCache . index ++ } ` ;
296331
297332 const usesWildcard =
@@ -303,6 +338,7 @@ class PuddySqlTags {
303338 . replaceAll ( this . wildcardB , '_' )
304339 : cleanTag ;
305340
341+ if ( ! Array . isArray ( pCache . values ) ) throw new Error ( 'Invalid pCache values' ) ;
306342 pCache . values . push ( filteredTag ) ;
307343 return { param, usesWildcard, not } ;
308344 } ;
@@ -336,16 +372,20 @@ class PuddySqlTags {
336372 * It also updates the input chunks to remove already-processed terms and eliminate repetitions
337373 * when `noRepeat` mode is enabled.
338374 *
339- * @param {Array<string|string[]> } chunks - A list of search terms or OR-groups (e.g., ['pony', ['red', 'blue']]).
375+ * @param {Chunks } chunks - A list of search terms or OR-groups (e.g., ['pony', ['red', 'blue']]).
340376 *
341- * @returns {Object } An object with:
377+ * @returns {{ specials: SpecialFromChunks[] } } An object with:
342378 * - `specials`: An array of extracted special queries `{ key, value }`.
343379 * - one property for each defined group in `#tagInputs`, each holding an array of objects with extracted values.
344380 * Example: `{ boosts: [{ term: "pony", boost: 2 }], specials: [...] }`
345381 */
346382 #extractSpecialsFromChunks( chunks ) {
383+ /** @type {SpecialFromChunks[] } */
347384 const specials = [ ] ;
385+
386+ /** @type {Record<string, { term: string; }[]> } */
348387 const outputGroups = { } ; // Will store the dynamic groups
388+ /** @type {Record<string, Set<string>> } */
349389 const uniqueMap = { } ; // Will store the dynamic sets
350390
351391 // Initiating sets for each group set in #tagInputs
@@ -444,7 +484,7 @@ class PuddySqlTags {
444484 *
445485 * @param {string } input - The user input string to parse.
446486 *
447- * @returns {Object } An object containing:
487+ * @returns {ParseStringResult } An object containing:
448488 * - `column`: The column name from `this.getColumnName()`.
449489 * - `include`: Array of tags and OR-groups to include in the query.
450490 * - Additional properties (e.g., `boosts`, `specials`) depending on matches in `#tagInputs` or `specialQueries`.
@@ -460,9 +500,13 @@ class PuddySqlTags {
460500 * ```
461501 */
462502 parseString ( input ) {
503+ /** @type {Chunks } */
463504 const chunks = [ ] ;
464- let buffer = '' ;
505+
506+ /** @type {string[] } */
465507 let currentGroup = [ ] ;
508+
509+ let buffer = '' ;
466510 let inQuotes = false ;
467511 let quoteChar = '' ;
468512 const uniqueTags = new Set ( ) ; // Para garantir que não existam tags duplicadas
@@ -565,7 +609,7 @@ class PuddySqlTags {
565609 *
566610 * @param {string } input - The raw user input string.
567611 *
568- * @returns {Object } A structured result object returned by `parseString()`,
612+ * @returns {ParseStringResult } A structured result object returned by `parseString()`,
569613 * containing keys like `column`, `include`, `specials`, `boosts`, etc., depending on
570614 * the tags and expressions detected.
571615 *
0 commit comments