[SOLVED] Permutations Algorithm - Steinhaus–Johnson–Trotter-algorithm help needed

Hi, I’m having troubles implementing this F# snippet: fast F# permutations (tested it, works ok)

My code so far:
/* jshint esversion: 6 */
const
  isEmpty = x => x.length === 0,
  cons = (h, t) => [h].concat(t);

function insertions(x, ys) {
	const [head, ...tail] = ys;
	return isEmpty(ys) ? [[x]] :
		cons(cons(x, ys), insertions(x, tail).map(x => cons(head, x)));
}

function permutations(xs) {
	const [head, ...tail] = xs;
	return isEmpty(xs) ? [[]] :
  	  permutations(tail).map(ys => insertions(head, ys));
}

repl.it ->
It seems that permutations function generates something close to that I need, but formatted in wrong way.

UPD: Found correct solution:

const insertions = (x, [y, ...ys]) =>
  !y ? [[x]] :
  [[x, y, ...ys], ...insertions(x, ys).map(zs => [y, ...zs])];


const permutations = ([x, ...xs]) =>
  !x ? [[]] :
  [].concat(...permutations(xs).map(ys => insertions(x, ys)));