From fc3d62f98461de0cf4833c14c57215e704c475c1 Mon Sep 17 00:00:00 2001 From: Christian Date: Fri, 14 Jun 2024 07:23:48 +0200 Subject: [PATCH 1/2] [New] Allow array in dotted notation --- index.js | 24 ++++++++++++++++++++++++ test/dotted.js | 11 +++++++++++ 2 files changed, 35 insertions(+) diff --git a/index.js b/index.js index 78cafa8..c3190b4 100644 --- a/index.js +++ b/index.js @@ -20,6 +20,23 @@ function isConstructorOrProto(obj, key) { return (key === 'constructor' && typeof obj[key] === 'function') || key === '__proto__'; } +function resolveArrays(o, key) { + var ARRAY = RegExp(/^([^[]+)\[([^\]]+)\](.*)/); + var groups = ARRAY.exec(key); + while (groups) { + var arr = groups[1]; + var idx = groups[2]; + var rest = groups[3]; + if (!Array.isArray(o[arr])) { + o[arr] = []; + } + o = o[arr]; + key = idx + (rest || ''); + groups = ARRAY.exec(key); + } + return [o, key]; +} + module.exports = function (args, opts) { if (!opts) { opts = {}; } @@ -86,9 +103,13 @@ module.exports = function (args, opts) { function setKey(obj, keys, value) { var o = obj; + var resolved; for (var i = 0; i < keys.length - 1; i++) { var key = keys[i]; if (isConstructorOrProto(o, key)) { return; } + resolved = resolveArrays(o, key); + o = resolved[0]; + key = resolved[1]; if (o[key] === undefined) { o[key] = {}; } if ( o[key] === Object.prototype @@ -103,6 +124,9 @@ module.exports = function (args, opts) { var lastKey = keys[keys.length - 1]; if (isConstructorOrProto(o, lastKey)) { return; } + resolved = resolveArrays(o, lastKey); + o = resolved[0]; + lastKey = resolved[1]; if ( o === Object.prototype || o === Number.prototype diff --git a/test/dotted.js b/test/dotted.js index c606118..df48773 100644 --- a/test/dotted.js +++ b/test/dotted.js @@ -34,3 +34,14 @@ test('dotted array', function (t) { t.end(); }); + +test('dotted notation with array in square bracket syntax', function (t) { + var argv = parse(['--a[1][0].foo', 'x', '--a[1][2]', '42']); + t.ok(Array.isArray(argv.a)); + t.notOk(0 in argv.a); + t.ok(Array.isArray(argv.a[1])); + t.equal(argv.a[1][0].foo, 'x'); + t.notOk(1 in argv.a[1]); + t.equal(argv.a[1][2].foo, 42); + t.end(); +}); From 31b23ca2706c1d5c337c9ffd9628edf6e764eeeb Mon Sep 17 00:00:00 2001 From: Christian Date: Fri, 14 Jun 2024 20:24:06 +0200 Subject: [PATCH 2/2] [New] Allow array in dotted notation --- index.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index c3190b4..950a29f 100644 --- a/index.js +++ b/index.js @@ -22,8 +22,8 @@ function isConstructorOrProto(obj, key) { function resolveArrays(o, key) { var ARRAY = RegExp(/^([^[]+)\[([^\]]+)\](.*)/); - var groups = ARRAY.exec(key); - while (groups) { + var groups; + while ((groups = ARRAY.exec(key))) { var arr = groups[1]; var idx = groups[2]; var rest = groups[3]; @@ -32,7 +32,6 @@ function resolveArrays(o, key) { } o = o[arr]; key = idx + (rest || ''); - groups = ARRAY.exec(key); } return [o, key]; }