From d0f1a91dfd65d7bf704fe0702181a9d17a0163a7 Mon Sep 17 00:00:00 2001 From: Shivam Mathur Date: Fri, 15 Jan 2021 13:52:18 +0530 Subject: [PATCH] Add support for quoted csv in ini-values --- __tests__/utils.test.ts | 11 +++++++++++ dist/index.js | 10 ++++++---- src/utils.ts | 12 +++++++----- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/__tests__/utils.test.ts b/__tests__/utils.test.ts index 3008d38c..5a234e02 100644 --- a/__tests__/utils.test.ts +++ b/__tests__/utils.test.ts @@ -104,6 +104,17 @@ describe('Utils tests', () => { 'b=2', 'c=3' ]); + expect(await utils.CSVArray('\'a=1,2\', "b=3, 4", c=5, d=~e~')).toEqual([ + 'a=1,2', + 'b=3, 4', + 'c=5', + "d='~e~'" + ]); + expect(await utils.CSVArray('a=\'1,2\', b="3, 4", c=5')).toEqual([ + 'a=1,2', + 'b=3, 4', + 'c=5' + ]); expect(await utils.CSVArray('')).toEqual([]); expect(await utils.CSVArray(' ')).toEqual([]); }); diff --git a/dist/index.js b/dist/index.js index 0f3e1c6e..f4f046fa 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1222,8 +1222,7 @@ async function extensionArray(extension_csv) { return extension .trim() .toLowerCase() - .replace('php-', '') - .replace('php_', ''); + .replace(/^php[-_]/, ''); }) .filter(Boolean); } @@ -1242,9 +1241,12 @@ async function CSVArray(values_csv) { return []; default: return values_csv - .split(',') + .split(/,(?=(?:(?:[^"']*["']){2})*[^"']*$)/) .map(function (value) { - return value.trim(); + return value + .trim() + .replace(/^["']|["']$|(?<==)["']/g, '') + .replace(/=(.*[?{}|&~![()^]+.*)/, "='$1'"); }) .filter(Boolean); } diff --git a/src/utils.ts b/src/utils.ts index 4312734a..e2ec8a54 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -209,8 +209,7 @@ export async function extensionArray( return extension .trim() .toLowerCase() - .replace('php-', '') - .replace('php_', ''); + .replace(/^php[-_]/, ''); }) .filter(Boolean); } @@ -229,9 +228,12 @@ export async function CSVArray(values_csv: string): Promise> { return []; default: return values_csv - .split(',') - .map(function (value: string) { - return value.trim(); + .split(/,(?=(?:(?:[^"']*["']){2})*[^"']*$)/) + .map(function (value) { + return value + .trim() + .replace(/^["']|["']$|(?<==)["']/g, '') + .replace(/=(.*[?{}|&~![()^]+.*)/, "='$1'"); }) .filter(Boolean); }