mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-15 01:52:22 +00:00
32 lines
994 B
TypeScript
32 lines
994 B
TypeScript
/** Extracts the key and value from a passed in env string based on the provided delimiters. */
|
|
export const getKeyValue = (pastedContent: string, delimiters: string[]) => {
|
|
if (!pastedContent) {
|
|
return { key: "", value: "" };
|
|
}
|
|
|
|
let firstDelimiterIndex = -1;
|
|
let foundDelimiter = "";
|
|
|
|
delimiters.forEach((delimiter) => {
|
|
const index = pastedContent.indexOf(delimiter);
|
|
if (index !== -1 && (firstDelimiterIndex === -1 || index < firstDelimiterIndex)) {
|
|
firstDelimiterIndex = index;
|
|
foundDelimiter = delimiter;
|
|
}
|
|
});
|
|
|
|
const hasValueAfterDelimiter = pastedContent.length > firstDelimiterIndex + foundDelimiter.length;
|
|
|
|
if (firstDelimiterIndex === -1 || !hasValueAfterDelimiter) {
|
|
return { key: pastedContent.trim(), value: "" };
|
|
}
|
|
|
|
const key = pastedContent.substring(0, firstDelimiterIndex);
|
|
const value = pastedContent.substring(firstDelimiterIndex + foundDelimiter.length);
|
|
|
|
return {
|
|
key: key.trim(),
|
|
value: value.trim()
|
|
};
|
|
};
|