-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gen bg replace, gen fill - handle special chars
- Loading branch information
1 parent
39965d6
commit ab4e832
Showing
5 changed files
with
38 additions
and
8 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
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
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
export const encodePromptComponent = (prompt: string): string => composeStringOperations(encodeURIComponent, replaceMissingChars, decodeSpaces)(prompt); | ||
|
||
const charsMissingFromUrlEncode = /[!'()*~_.-]/g; | ||
|
||
const missingCharsMap: { [char: string]: string } = { | ||
'!': '%21', | ||
"'": '%27', | ||
'(': '%28', | ||
')': '%29', | ||
'*': '%2A', | ||
'~': '%7E', | ||
_: '%5F', | ||
'.': '%2E', | ||
'-': '%2D', | ||
}; | ||
|
||
const replaceMissingChars = (str: string): string => { | ||
return str.replace(charsMissingFromUrlEncode, (match) => missingCharsMap[match] ?? match); | ||
}; | ||
|
||
const decodeSpaces = (str: string): string => str.replace(/%20/g, ' '); | ||
const composeStringOperations = (...fns: Array<(str: string) => string>) => (arg: string) => fns.reduce((acc, fn) => fn(acc), arg); |
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