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

Function sortTerms

services/tuss/index.js:57–78  ·  view source on GitHub ↗
(terms, sort = 'tuss', order = 'asc')

Source from the content-addressed store, hash-verified

55}
56
57function sortTerms(terms, sort = 'tuss', order = 'asc') {
58 const isDesc = String(order).toLowerCase() === 'desc';
59 const key = String(sort).toLowerCase();
60
61 const sorted = [...terms].sort((x, y) => {
62 const nx = normalize(x.name || x.nome || x.descricao);
63 const ny = normalize(y.name || y.nome || y.descricao);
64 const cx = sanitizeCode(x.tuss || x.codigo || x.codigo_tuss);
65 const cy = sanitizeCode(y.tuss || y.codigo || y.codigo_tuss);
66
67 let cmp = 0;
68 if (key === 'name') {
69 cmp = compareAsc(nx, ny);
70 } else {
71 // default sort by tuss code (lexical compare of digits)
72 cmp = compareAsc(cx, cy);
73 }
74 return isDesc ? -cmp : cmp;
75 });
76
77 return sorted;
78}
79
80export function searchTussAdvanced({
81 q,

Callers 1

searchTussAdvancedFunction · 0.85

Calls 3

normalizeFunction · 0.85
sanitizeCodeFunction · 0.85
compareAscFunction · 0.85

Tested by

no test coverage detected