简体   繁体   English

如何将queryString中的逗号分隔符仅在末尾转换为pipe分隔符

[英]How to turn comma separator in queryString to pipe separator only at the end

So i currently have an API i'm hitting with queryParams.所以我目前有一个 API 我正在使用 queryParams。 The params are colour and i'm calling the api like this:参数是颜色,我像这样调用 api:

{apiUrl}?firstQuery=someQuery&colour=Blue,Green

Where the above request will request items with Blue and Green colours from the server.上述请求将从服务器请求具有蓝色和绿色的项目。

Now sometimes the colours can get a bit tricky and cause me problems, specially long colours that already have commas in the colour description/text itself.现在有时颜色会变得有点棘手并给我带来问题,特别是颜色描述/文本本身中已经有逗号的长颜色。

Here is a list of the long/problematic colours:以下是长/有问题的颜色列表:

const coloursWithCommas = [
    'Red, Blue, Yellow and Green Play of Colour',
    'Red, Green, Blue And Orange Play Of Colour',
    'Orange With Green, Red, Yellow And Blue Play Of Colour',
    'Green, Blue And Yellow Play Of Colour',
    'Purple-Red, White and Blue-Green',
  ];

As you can see in my API call that i need a comma separator to be able to send multiple colours at once but there are colours that already have commas in it.正如您在我的 API 调用中看到的那样,我需要一个逗号分隔符才能一次发送多种颜色,但有些颜色中已经有逗号。 Therefore this request fails to get the desired colours: {apiUrl}?firstQuery=someQuery&colour=Blue,Green,Red, Blue, Yellow and Green Play of Colour因此,此请求无法获得所需的颜色: {apiUrl}?firstQuery=someQuery&colour=Blue,Green,Red, Blue, Yellow and Green Play of Colour

As you can see that i'm trying to get 3 different colours in 1 request which is Blue , Green and Red, Blue, Yellow and Green Play of Colour如您所见,我试图在 1 个请求中获得 3 种不同的颜色,即蓝色绿色红色、蓝色、黄色和绿色的颜色

To combat this issue the API now accepts |为了解决这个问题,API 现在接受 | pipe separator instead of comma to send multiple colours whilst respecting each colour value that contains commas. pipe 分隔符代替逗号发送多种颜色,同时尊重包含逗号的每个颜色值。

So now the new api call is:所以现在新的 api 调用是:

{apiUrl}?firstQuery=someQuery&colour=Blue|Green|Red, Blue, Yellow and Green Play of Colour|Purple-Red, White and Blue-Green

This would successfully return me Blue , Green , Red, Blue, Yellow and Green Play of Colour , Purple-Red, White and Blue-Green这将成功返回我Blue , Green , Red, Blue , Yellow 和 Green Play of Color , Purple-Red, White 和 Blue-Green

I have written this helper function:我写了这个助手 function:

const convertCommaToPipe = (queryInput) => {
  let queryStringName = queryInput.split('=')[0];
  let queryString = queryInput.split('=')[1];

  const coloursWithCommas = [
    'Red, Blue, Yellow and Green Play of Colour',
    'Red, Green, Blue And Orange Play Of Colour',
    'Orange With Green, Red, Yellow And Blue Play Of Colour',
    'Green, Blue And Yellow Play Of Colour',
    'Purple-Red, White and Blue-Green',
  ];

  if(!coloursWithCommas.includes(queryString)){
    queryString = queryString.replace(',', '|');
  } else{
    const longColourIndex = coloursWithCommas.findIndex((text) => text === queryString);
    queryString = coloursWithCommas[longColourIndex] + '|';
  }

  return queryStringName + '=' + queryString;
}

So that I can be able turn a queryString from: colour=Blue,Green, Blue And Yellow Play Of Colour,Orange With Green, Red, Yellow And Blue Play Of Colour这样我就可以从以下位置打开查询字符串: colour=Blue,Green, Blue And Yellow Play Of Colour,Orange With Green, Red, Yellow And Blue Play Of Colour

to:至:

&colour=Blue|Green, Blue And Yellow Play Of Colour|Orange With Green, Red, Yellow And Blue Play Of Colour

The queryInput from the helper function will always start with colour=Blue or colour=Green, Blue And Yellow Play Of Colour and it will then build/append one at time and it can then have the full query like colour=Blue,Green,Red, Blue, Yellow and Green Play of Colour give that the user has clicked on those 3 colours.来自帮助程序 function 的 queryInput 将始终以colour=Bluecolour=Green, Blue And Yellow Play Of Colour开始,然后它会一次构建/附加一个,然后可以进行完整的查询,例如colour=Blue,Green,Red, Blue, Yellow and Green Play of Colour表明用户点击了这 3 种颜色。

It won't be receiving all the colour queries at once, it will be appended as the user clicks the UI.它不会一次接收所有颜色查询,它会在用户单击 UI 时附加。 That seems to be what's causing my function to not fully work 100%.这似乎是导致我的 function 不能完全 100% 工作的原因。 Please advise the best way to achieve what i'm trying to given the circumstances.请告知在这种情况下实现我想要达到的目标的最佳方法。

I would suggest you change the API it self(assuming it's ur API).我建议您自行更改 API(假设它是您的 API)。 You should use PUT or POST and pass the colors as array of string.您应该使用PUTPOST并将 colors 作为字符串数组传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM