簡體   English   中英

"在 Typescript 上准備字符串的一些功能需要更優雅的解決方案"

[英]Some function for preparing string on Typescript There is a need for a more elegant solution

我有一個為搜索查詢准備字符串的功能。 但是看起來好難 需要一個更優雅的解決方案。 也許有人可以看看並提供更好的東西? 它是原始功能。

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'
}

也許這對你有用?

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

也許是不同的組織:

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;
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM