(
patchInput = "",
grammar = {
rowDelimiter: "&",
columnDelimiter: "=",
encodedRowDelimiter: "%2E%2E%2E",
encodedColumnDelimiter: "%7E"
}
)
| 1 | class Patch { |
| 2 | constructor( |
| 3 | patchInput = "", |
| 4 | grammar = { |
| 5 | rowDelimiter: "&", |
| 6 | columnDelimiter: "=", |
| 7 | encodedRowDelimiter: "%2E%2E%2E", |
| 8 | encodedColumnDelimiter: "%7E" |
| 9 | } |
| 10 | ) { |
| 11 | // The pipeline of encodings. Operations will be run in order for encoding (and reveresed for decoding). |
| 12 | this.encoders = [ |
| 13 | { |
| 14 | encode: str => encodeURIComponent(str), |
| 15 | decode: str => decodeURIComponent(str) |
| 16 | }, |
| 17 | { |
| 18 | encode: str => this.replaceAll(str, this.grammar.columnDelimiter, this.grammar.encodedColumnDelimiter), |
| 19 | decode: str => this.replaceAll(str, this.grammar.encodedColumnDelimiter, this.grammar.columnDelimiter) |
| 20 | }, |
| 21 | { |
| 22 | encode: str => this.replaceAll(str, this.grammar.rowDelimiter, this.grammar.encodedRowDelimiter), |
| 23 | decode: str => this.replaceAll(str, this.grammar.encodedRowDelimiter, this.grammar.rowDelimiter) |
| 24 | }, |
| 25 | { |
| 26 | // Turn "%20" into "+" for prettier urls. |
| 27 | encode: str => str.replace(/\%20/g, "+"), |
| 28 | decode: str => str.replace(/\+/g, "%20") |
| 29 | } |
| 30 | ] |
| 31 | this.grammar = grammar |
| 32 | if (typeof patchInput === "string") this.uriEncodedString = patchInput |
| 33 | else if (Array.isArray(patchInput)) this.uriEncodedString = this.arrayToEncodedString(patchInput) |
| 34 | else this.uriEncodedString = this.objectToEncodedString(patchInput) |
| 35 | } |
| 36 | replaceAll(str, search, replace) { |
| 37 | return str.split(search).join(replace) |
| 38 | } |
nothing calls this directly
no test coverage detected