Change:
Currently json edit allows to insert items in an array at the end by passing . It does not support inserting at an index.

Discussion:
return withFormatting(text, edit, formattingOptions); } } else if (parent.type === 'array' && typeof lastSegment === 'number' && Array.isArray(parent.children)) { <del> const insertIndex = lastSegment; if (insertIndex === -1) { </del> <ins> if (value !== undefined) { </ins> // Insert const newProperty = `${JSON.stringify(value)}`; let edit: Edit; <del> if (parent.children.length === 0) { edit = { offset: parent.offset + 1, length: 0, content: newProperty }; </del> <ins> if (parent.children.length === 0 || lastSegment === 0) { edit = { offset: parent.offset + 1, length: 0, content: parent.children.length === 0 ? newProperty : newProperty + ',' }; </ins> } else { <del> const previous = parent.children[parent.children.length - 1]; </del> <ins> const index = lastSegment === -1 || lastSegment > parent.children.length ? parent.children.length : lastSegment; const previous = parent.children[index - 1]; </ins> edit = { offset: previous.offset + previous.length, length: 0, content: ',' + newProperty }; } return withFormatting(text, edit, formattingOptions); } else { <del> if (value === undefined && parent.children.length >= 0) { //Removal const removalIndex = lastSegment; const toRemove = parent.children[removalIndex]; let edit: Edit; if (parent.children.length === 1) { // only item edit = { offset: parent.offset + 1, length: parent.length - 2, content: '' }; } else if (parent.children.length - 1 === removalIndex) { // last item const previous = parent.children[removalIndex - 1]; const offset = previous.offset + previous.length; const parentEndOffset = parent.offset + parent.length; edit = { offset, length: parentEndOffset - 2 - offset, content: '' }; } else { edit = { offset: toRemove.offset, length: parent.children[removalIndex + 1].offset - toRemove.offset, content: '' }; } return withFormatting(text, edit, formattingOptions); </del> <ins> //Removal const removalIndex = lastSegment; const toRemove = parent.children[removalIndex]; let edit: Edit; if (parent.children.length === 1) { // only item edit = { offset: parent.offset + 1, length: parent.length - 2, content: '' }; } else if (parent.children.length - 1 === removalIndex) { // last item const previous = parent.children[removalIndex - 1]; const offset = previous.offset + previous.length; const parentEndOffset = parent.offset + parent.length; edit = { offset, length: parentEndOffset - 2 - offset, content: '' }; </ins> } else { <del> throw new Error('Array modification not supported yet'); </del> <ins> edit = { offset: toRemove.offset, length: parent.children[removalIndex + 1].offset - toRemove.offset, content: '' }; </ins> } <ins> return withFormatting(text, edit, formattingOptions); </ins> } } else { throw new Error(`Can not add ${typeof lastSegment !== 'number' ? 'index' : 'property'} to parent of type ${parent.type}`);

assertEdit(content, edits, '{n  "x": "y"n}'); }); <del> test('insert item to empty array', () => { </del> <ins> test('insert item at 0', () => { let content = '[n  2,n  3n]'; let edits = setProperty(content, [0], 1, formatterOptions); assertEdit(content, edits, '[n  1,n  2,n  3n]'); }); test('insert item at 0 in empty array', () => { let content = '[n]'; let edits = setProperty(content, [0], 1, formatterOptions); assertEdit(content, edits, '[n  1n]'); }); test('insert item at an index', () => { let content = '[n  1,n  3n]'; let edits = setProperty(content, [1], 2, formatterOptions); assertEdit(content, edits, '[n  1,n  2,n  3n]'); }); test('insert item at an index im empty array', () => { let content = '[n]'; let edits = setProperty(content, [1], 1, formatterOptions); assertEdit(content, edits, '[n  1n]'); }); test('insert item at end index', () => { let content = '[n  1,n  2n]'; let edits = setProperty(content, [2], 3, formatterOptions); assertEdit(content, edits, '[n  1,n  2,n  3n]'); }); test('insert item at end to empty array', () => { </ins> let content = '[n]'; let edits = setProperty(content, [-1], 'bar', formatterOptions); assertEdit(content, edits, '[n  "bar"n]'); }); <del> test('insert item', () => { </del> <ins> test('insert item at end', () => { </ins> let content = '[n  1,n  2n]'; let edits = setProperty(content, [-1], 'bar', formatterOptions); assertEdit(content, edits, '[n  1,n  2,n  "bar"n]');

assertEdit(content, edits, '// This is a commentn[n  1,n  "foo"n]'); }); <del> }); </del>  No newline at end of file <ins> }); </ins>