Skip to content

Commit 68cf172

Browse files
committed
Made sure that quoted strings will not be re-quoted.
1 parent ea9016e commit 68cf172

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ The marshalled version of the input data.
5656

5757
## Changelog:
5858

59+
**v1.0.21**:
60+
61+
- Made sure that quoted strings will not be re-quoted.
62+
5963
**v1.0.20**:
6064

6165
- Added marshalling to/from class instances and class constructors.

src/Marshaller.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,6 +1612,15 @@ export class Marshaller implements IMarshaller {
16121612
return this.marshalToString(data);
16131613
}
16141614

1615+
/**
1616+
* Returns true if the given content is a quote.
1617+
* @param {string} content
1618+
* @returns {boolean}
1619+
*/
1620+
private isQuote (content: string): boolean {
1621+
return /["'`]/.test(content);
1622+
}
1623+
16151624
/**
16161625
* Quotes the given string if needed. It will escape the string if it already starts and/or ends with a clashing quote.
16171626
* @param {string} content
@@ -1621,6 +1630,7 @@ export class Marshaller implements IMarshaller {
16211630
if (!(typeof content === "string")) return content;
16221631
const firstChar = content[0];
16231632
const lastChar = content[content.length - 1];
1633+
if (this.isQuote(firstChar) && this.isQuote(lastChar)) return content;
16241634
let str = "`";
16251635
const startsWithClashingQuote = firstChar === "`";
16261636
const endsWithClashingQuote = lastChar === "`";

test/Marshaller.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ test(`'marshal()' string -> object. #1`, t => {
273273

274274
const input = "{\"foo\": false, \"type\": {\"expression\": `hello`}}";
275275

276-
t.deepEqual<Object|null|undefined>(marshaller.marshal(input, expected), expected);
276+
t.deepEqual(marshaller.marshal(input, expected), expected);
277277
});
278278

279279
test(`'marshal()' class -> string. #1`, t => {

0 commit comments

Comments
 (0)