From 6542bb901eb560321ece210dd7d478ded0997081 Mon Sep 17 00:00:00 2001 From: ShareVB Date: Sun, 19 May 2024 14:23:11 +0200 Subject: [PATCH] fix: root non nested array Fix #5 --- index.js | 2 +- test/test.js | 24 +++++++++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index cbeabff..9be4015 100644 --- a/index.js +++ b/index.js @@ -202,7 +202,7 @@ exports.arrify = (json, options) => { const validJSON = isJSON(json) let object = validJSON || {} - object = isPlainObject(json) ? json : object + object = isPlainObject(json) || isArray(json) ? json : object const phpArray = arrify(object, options) return `${phpArray};` diff --git a/test/test.js b/test/test.js index 420c75f..e0cda98 100644 --- a/test/test.js +++ b/test/test.js @@ -8,11 +8,11 @@ const jsonFile = () => new Promise(resolve => { readFile(exampleFile, 'ascii', (error, data) => { if (error) { throw new Error(error) - } + } - resolve(data) - }) + resolve(data) }) +}) test('undefined param is blank array', t => { t.is(jsonar.arrify(), 'array();') @@ -100,3 +100,21 @@ test('js object to PHP array and back (#18)', t => { t.is(JSON.stringify(jsonar.parse(phpArray)), array) }) +test('js object to PHP array and back (#5)', t => { + const object1 = {foo: ['42', '52']} + const phpArray1 = 'array("foo"=>array("42","52"));' + t.is(jsonar.arrify(object1), phpArray1) + t.is(JSON.stringify(jsonar.parse(phpArray1)), JSON.stringify(object1)) + + const object2 = ['42', '52'] + const phpArray2 = 'array("42","52");' + t.is(jsonar.arrify(object2), phpArray2) + t.is(JSON.stringify(jsonar.parse(phpArray2)), JSON.stringify(object2)) +}) + +test('more js array test (#5)', t => { + const object1 = [{foo: ['42', '52']}] + const phpArray1 = 'array(array("foo"=>array("42","52")));' + t.is(jsonar.arrify(object1), phpArray1) + t.is(JSON.stringify(jsonar.parse(phpArray1)), JSON.stringify(object1)) +})