Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

210
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!