gpt4 book ai didi

javascript - 我有一个代表数组的字符串。如何推送保持格式的元素?

转载 作者:行者123 更新时间:2023-11-30 19:16:54 24 4
gpt4 key购买 nike

给定一个类的名称 (CustomClass6) 和一个表示具有 ANY 格式的数组的字符串,它应该将类的名称附加到数组中以保持格式。

示例 1:

给定这个字符串:

"""
const arr = [
CustomClass11,
CustomClass22,
CustomClass33,
CustomClass44,
CustomClass55
];
"""

函数应该返回:

"""
const arr = [
CustomClass11,
CustomClass22,
CustomClass33,
CustomClass44,
CustomClass55,
CustomClass6
];
"""

示例 2:

给定字符串:

"""
const arr = [CustomClass1, CustomClass2, CustomClass3];
"""

它应该返回:

"""
const arr = [CustomClass1, CustomClass2, CustomClass3, CustomClass6];
"""

示例 3:

"""
const arr = [];
"""

它应该返回:

"""
const arr = [CustomClass6];
"""

这些是我到目前为止所做的功能。我已经能够解决示例 1,但如您所见,我已将分隔符硬编码为 \n:


function getStringWithElementAppended(
stringWithArray: string,
classToAppend: string
) {
const openingBraketIndex = stringWithArray.indexOf('[');
const closingBracketIndex = getIndexInString(
stringWithArray,
']',
openingBraketIndex
);

const updatedContent = appendElementToArrayInString(
classToAppend,
openingBraketIndex,
closingBracketIndex,
stringWithArray
);

return updatedContent;
}

function getIndexInString(
String: string,
stringToLookFor: string,
startingIndex: number = 0,
limitIndex: number = String.length,
lastIndex = false
) {
const auxString = String.slice(startingIndex, limitIndex);

if (lastIndex) {
return startingIndex + auxString.lastIndexOf(stringToLookFor);
} else {
return startingIndex + auxString.indexOf(stringToLookFor);
}
}

function appendElementToArrayInString(
elementToAppend: string,
arrayBeginingIndex: number,
arrayEndIndex: number,
String: string
) {
const separationBetweenElements = '\n';
const endOfArrayIndex = getIndexInString(
String,
separationBetweenElements,
arrayBeginingIndex,
arrayEndIndex,
true
);
const previousElementEndIndex = getIndexInString(
String,
separationBetweenElements,
arrayBeginingIndex,
endOfArrayIndex,
true
);
const spaceBetweenElements = String.slice(
previousElementEndIndex + 1,
endOfArrayIndex
);
const whitespaceIndex = spaceBetweenElements.lastIndexOf(' ');
const whitespace = spaceBetweenElements.slice(0, whitespaceIndex + 1);
const contentToAdd =
',' +
separationBetweenElements +
whitespace +
elementToAppend +
separationBetweenElements;
const updatedString =
String.slice(0, endOfArrayIndex) + contentToAdd + String.slice(arrayEndIndex);
return updatedString;
}

归根结底,输出应该遵循输入的格式。我在修改现有文件的脚本中使用此代码,我们不希望我们的 SCM 检测到大量格式更改。

最佳答案

主要的并发症是分隔符。没有内置的方法可以做到这一点。为了检测分隔符,您需要从本质上将字符串解析为语法。

语法将包括:

  • 变量声明
  • 数组语法
  • 前缀填充
  • 值(value)观
  • 分隔符
  • 后缀填充

我们开发的语言将基于这种语法的构造,它本质上是每个片段的简单生成。需要根据设置的值检测分隔符。

// Test data

const inputa =
`"""
const arr = [
CustomClass25,
CustomClass9,
CustomClass12,
CustomClass8
];
"""`;

const inputb =
`"""
const arr = [CustomClass1, CustomClass2, CustomClass3];
"""`;

const inputc =
`"""
const arr = [];
"""`;

// Main actor
function modifyDeclaration(declaration,addition){

// Retain starting declaration index
const startingSyntax = declaration.indexOf('[');

// Retain ending declaration index
const endingSyntax = declaration.indexOf('];');

// Obtain the contained array from syntax
const arrayOnly = declaration.substring(startingSyntax+1,endingSyntax);

// Determine the array's prefix padding
const prefixPadding = arrayOnly.substring(0,arrayOnly.search(/\S|$/));

// Determine the array's suffix padding
// by reversing the string and finding
// the first nonwhitespace character
const suffixPadding = arrayOnly.substring(arrayOnly.length - arrayOnly.split('').reverse().findIndex(s => /\S/.test(s)));

// Strip array padding for value insertion
const values = arrayOnly.substring(prefixPadding.length,arrayOnly.length - suffixPadding.length);

// Assume base separation is the same
// as the trailing padding in cases
// of no set
let separator = suffixPadding;

// Gather the index of the first comma for
// separator detection
const commaIndex = arrayOnly.indexOf(',');

// When there are values or a commas
if(commaIndex > -1 || values.length > 0){

// Make the separator include a comma
// as well as the first separator
separator = ',' + arrayOnly.substring(
commaIndex+1,
commaIndex+1 + arrayOnly.substring(commaIndex+1).search(/\S|$/)
);
}

// Return our production value
return declaration.substring(0,startingSyntax+1) +
prefixPadding +
values +
separator +
addition +
suffixPadding +
declaration.substring(endingSyntax)
}

const addition = 'CustomClass42';

console.log(modifyDeclaration(inputa,addition));
console.log(modifyDeclaration(inputb,addition));
console.log(modifyDeclaration(inputc,addition));

关于javascript - 我有一个代表数组的字符串。如何推送保持格式的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57895293/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com