MCPcopy Create free account
hub / github.com/BrasilAPI/BrasilAPI / searchTussAdvanced

Function searchTussAdvanced

services/tuss/index.js:80–137  ·  view source on GitHub ↗
({
  q,
  name,
  tuss,
  match = 'prefix',
  sort = 'tuss',
  order = 'asc',
})

Source from the content-addressed store, hash-verified

78}
79
80export function searchTussAdvanced({
81 q,
82 name,
83 tuss,
84 match = 'prefix',
85 sort = 'tuss',
86 order = 'asc',
87}) {
88 let terms = getTussTerms();
89
90 const hasQ = typeof q === 'string' && q.trim().length > 0;
91 const hasName = typeof name === 'string' && name.trim().length > 0;
92 const hasTuss = typeof tuss === 'string' && tuss.trim().length > 0;
93
94 if (!hasQ && !hasName && !hasTuss) {
95 return sortTerms(terms, sort, order);
96 }
97
98 const nameQuery = hasName ? normalize(name) : null;
99 const tussQuery = hasTuss ? sanitizeCode(tuss) : null;
100 const tokens = hasQ ? String(q).split(/\s+/).filter(Boolean) : [];
101
102 const isExact = String(match).toLowerCase() === 'exact';
103
104 terms = terms.filter((item) => {
105 const itemName = normalize(item.name || item.nome || item.descricao);
106 const itemCode = sanitizeCode(item.tuss || item.codigo || item.codigo_tuss);
107
108 const matchName = (() => {
109 if (!hasName) return true;
110 if (isExact) return itemName === nameQuery;
111 return itemName.includes(nameQuery);
112 })();
113
114 const matchCode = (() => {
115 if (!hasTuss) return true;
116 if (isExact) return itemCode === tussQuery;
117 return itemCode.startsWith(tussQuery);
118 })();
119
120 let matchQ = true;
121 if (hasQ) {
122 matchQ = tokens.every((t) => {
123 const isDigits = /^(\d+)$/.test(t);
124 if (isDigits) {
125 const qt = sanitizeCode(t);
126 return isExact ? itemCode === qt : itemCode.startsWith(qt);
127 }
128 const nt = normalize(t);
129 return itemName.includes(nt);
130 });
131 }
132
133 return matchName && matchCode && matchQ;
134 });
135
136 return sortTerms(terms, sort, order);
137}

Callers 2

TussAutocompleteFunction · 0.90
TussSearchAdvancedFunction · 0.90

Calls 4

getTussTermsFunction · 0.85
sortTermsFunction · 0.85
normalizeFunction · 0.85
sanitizeCodeFunction · 0.85

Tested by

no test coverage detected