Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

211
Visualizações
Some function for preparing string on Typescript There is a need for a more elegant solution

I have a function for preparing string for a search query. But it looks so difficult There is a need for a more elegant solution. Maybe someone can take a look and provide something better? It is original func.

export const transformedSortFunc = (sortingPath) => {
  let transformPath;
  if (sortingPath) {
    if (sortingPath[0] === '-') {
      const sortingValue = get(SORTING_PATH_MAP, sortingPath.slice(1));
      transformPath = sortingValue ? `-${sortingValue}` : undefined;
    } else {
      transformPath = get(SORTING_PATH_MAP, sortingPath);
    }
  }
  return transformPath || sortingPath;
};

const SORTING_PATH_MAP = {
    asset: 'asset.type'
}

Input parameters can be

  const conditional = [
        'id', 'asset', 'conditional', '-id', '-asset', '-conditional' ,
    ]

I am using lodash.

For some incoming lines, I need to add a continuation. For some, no. Lines can be with a minus, so I added additional checks. It ends up looking very complicated

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Maybe this will work for you?

tsf=s=>SORTING_PATH_MAP[(s??"").replace(/^-/,"")] ?? s;

tsf() will remove an optional leading - while looking in SORTING_PATH_MAP and will use the result if it exists, otherwise it will return s directly.

about 4 years ago · Juan Pablo Isaza Relatório

0

Maybe a different organization:

const transformedSortFunc = (sortingPath) => {
  if (!sortingPath) return sortingPath;

  const negative = sortingPath[0] === '-' ? '-' : '';
  const sortingValue = negative ? sortingPath.slice(1) : sortingPath;

  const transformPath = get(SORTING_PATH_MAP, sortingValue);
  return transformPath ? `${negative}${transformPath}` : sortingPath;
};
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda