Skip to content

Commit

Permalink
fix: root non nested array
Browse files Browse the repository at this point in the history
  • Loading branch information
sharevb committed May 19, 2024
1 parent 6eb1b56 commit 6542bb9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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};`
Expand Down
24 changes: 21 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();')
Expand Down Expand Up @@ -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))
})

0 comments on commit 6542bb9

Please sign in to comment.