Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow single quotes in key=value pairs #427

Merged
merged 4 commits into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/dataCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,17 @@ export abstract class DataCommand extends SfdxCommand {
const keyValuePairs: string[] = [];

const trimmedText = text.trim();

const singleQuoteCount = (trimmedText.match(/'/g) || []).length;
const doubleQuoteCount = (trimmedText.match(/"/g) || []).length;

for (const currentChar of trimmedText) {
const isSeparator = separator.exec(currentChar);
const isSeparator = separator.test(currentChar);

if (currentChar === "'" && !inDoubleQuote) {
if (currentChar === "'" && !inDoubleQuote && singleQuoteCount >= 2) {
inSingleQuote = !inSingleQuote;
continue;
} else if (currentChar === '"' && !inSingleQuote) {
} else if (currentChar === '"' && !inSingleQuote && doubleQuoteCount >= 2) {
inDoubleQuote = !inDoubleQuote;
continue;
}
Expand Down
28 changes: 20 additions & 8 deletions test/commands/force/data/dataCommand.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,34 +62,46 @@ describe('dataCommand', () => {
});

it('should allow single quotes in key=value pairs', () => {
const dict = dataCommand.testStringToDictionary('key1="val\'ue"');
let dict = dataCommand.testStringToDictionary('key="val\'ue"');

expect(dict).to.deep.equal({
key1: "val'ue",
key: "val'ue",
});

dict = dataCommand.testStringToDictionary("key=val'ue");

expect(dict).to.deep.equal({
key: "val'ue",
});
});

it('should allow double quotes in key=value pairs', () => {
const dict = dataCommand.testStringToDictionary("key1='val\"ue'");
let dict = dataCommand.testStringToDictionary("key='val\"ue'");

expect(dict).to.deep.equal({
key: 'val"ue',
});

dict = dataCommand.testStringToDictionary('key=val"ue');

expect(dict).to.deep.equal({
key1: 'val"ue',
key: 'val"ue',
});
});

it('should allow non alphanumeric characters in key=value pairs', () => {
const dict = dataCommand.testStringToDictionary('key1=!@#$%^&*()-_=+[{]}\\|;:,<.>/?`~');
const dict = dataCommand.testStringToDictionary('key=!@#$%^&*()-_=+[{]}\\|;:,<.>/?`~');

expect(dict).to.deep.equal({
key1: '!@#$%^&*()-_=+[{]}\\|;:,<.>/?`~',
key: '!@#$%^&*()-_=+[{]}\\|;:,<.>/?`~',
});
});

it('should allow weird or foreign unicode characters in key=value pairs', () => {
const dict = dataCommand.testStringToDictionary('key1=♣♦♥♠&£ë╤è☺¼Φ╚↕↓㍿々');
const dict = dataCommand.testStringToDictionary('key=♣♦♥♠&£ë╤è☺¼Φ╚↕↓㍿々');

expect(dict).to.deep.equal({
key1: '♣♦♥♠&£ë╤è☺¼Φ╚↕↓㍿々',
key: '♣♦♥♠&£ë╤è☺¼Φ╚↕↓㍿々',
});
});
});