-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2859 from Infisical/daniel/copy-paste
fix(dashboard): pasting secrets into create secret modal
- Loading branch information
Showing
3 changed files
with
73 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,31 @@ | ||
/** Extracts the key and value from a passed in env string based on the provided delimiters. */ | ||
export const getKeyValue = (pastedContent: string, delimiters: string[]) => { | ||
const foundDelimiter = delimiters.find((delimiter) => pastedContent.includes(delimiter)); | ||
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; | ||
} | ||
}); | ||
|
||
if (!foundDelimiter) { | ||
const hasValueAfterDelimiter = pastedContent.length > firstDelimiterIndex + foundDelimiter.length; | ||
|
||
if (firstDelimiterIndex === -1 || !hasValueAfterDelimiter) { | ||
return { key: pastedContent.trim(), value: "" }; | ||
} | ||
|
||
const [key, value] = pastedContent.split(foundDelimiter); | ||
const key = pastedContent.substring(0, firstDelimiterIndex); | ||
const value = pastedContent.substring(firstDelimiterIndex + foundDelimiter.length); | ||
|
||
return { | ||
key: key.trim(), | ||
value: (value ?? "").trim() | ||
value: value.trim() | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters