Skip to content

Instantly share code, notes, and snippets.

@ktilcu
Created March 18, 2019 18:18
Show Gist options
  • Select an option

  • Save ktilcu/3ffc842390926725e7669d9eddddafd4 to your computer and use it in GitHub Desktop.

Select an option

Save ktilcu/3ffc842390926725e7669d9eddddafd4 to your computer and use it in GitHub Desktop.
JS Bin Intersperse function 2 ways // source https://jsbin.com/furejas
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Intersperse function 2 ways">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://bundle.run/tape@4.10.1"></script>
<script src="https://bundle.run/tap-browser-color"></script>
<script src="https://bundle.run/tape-catch"></script>
</head>
<body>
<script>
window.test = tape;
tapBrowserColor();
</script>
<script id="jsbin-javascript">
'use strict';
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } }
var merge = function merge(a, b) {
var len = a.length < b.length ? b.length : a.length;
var i = 0;
var out = '';
while (i < len) {
out += (a[i] || '') + (b[i] || '');
i += 1;
}
return out;
};
var insert = function insert(idx, elt, list) {
var index = idx < list.length && idx >= 0 ? idx : list.length;
var copy = [].concat(list);
copy.splice(idx, 0, elt);
return copy;
};
var inter = function inter(a, b) {
return [].concat(_toConsumableArray(b)).reduce(function (base, letter, index) {
return insert(index * 2 + 1, letter || '', base);
}, [].concat(_toConsumableArray(a))).join('');
};
test('insert', function (t) {
t.deepEqual(['a', 'b', 'x', 'c', 'd', 'e'], insert(2, 'x', ['a', 'b', 'c', 'd', 'e']), 'inserts an element into the given list');
t.deepEqual(['a', 'b', ['s', 't'], 'c', 'd', 'e'], insert(2, ['s', 't'], ['a', 'b', 'c', 'd', 'e']), 'inserts another list as an element');
t.deepEqual(['a', 'b', 'c', 'd', 'e', 'z'], insert(8, 'z', ['a', 'b', 'c', 'd', 'e']), 'appends to the end of the list if the index is too large');
t.end();
});
test('intersperse', function (t) {
t.equal('asbtcuvwx', inter('abc', 'stuvwx'), 'continues with longer second arg');
t.equal('kknyolcek.com', inter('knock.com', 'kyle'), 'continues with longer first arg');
t.equal('nkoydlee', inter('node', 'kyle'), 'intersperses same length strings');
t.equal('', inter('', ''), 'intersperses empty strings');
t.throws(inter.bind(null, ['node', 2]), 'Does not intersperse non strings');
t.end();
});
test('merge', function (t) {
t.equal('asbtcuvwx', merge('abc', 'stuvwx'), 'First string longer');
t.equal('kknyolcek', merge('knock', 'kyle'), 'Second string longer');
t.equal('nkoydlee', merge('node', 'kyle'), 'Same length strings');
t.equal('', merge('', ''), 'Empty strings');
t.throws(merge.bind(null, ['node', 2]), 'Not strings');
t.end();
});
</script>
<script id="jsbin-source-javascript" type="text/javascript">const merge = (a, b) => {
const len = a.length < b.length ? b.length : a.length;
let i = 0;
let out = '';
while (i < len) {
out += (a[i] || '') + (b[i] || '');
i += 1;
}
return out;
};
const insert = (idx, elt, list) => {
const index = idx < list.length && idx >= 0 ? idx : list.length;
const copy = [].concat(list);
copy.splice(idx, 0, elt);
return copy;
};
const inter = (a, b) => {
return [...b]
.reduce(
(base, letter, index) => insert(index * 2 + 1, letter || '', base),
[...a]
)
.join('');
};
test('insert', t => {
t.deepEqual(
['a', 'b', 'x', 'c', 'd', 'e'],
insert(2, 'x', ['a', 'b', 'c', 'd', 'e']),
'inserts an element into the given list'
);
t.deepEqual(
['a', 'b', ['s', 't'], 'c', 'd', 'e'],
insert(2, ['s', 't'], ['a', 'b', 'c', 'd', 'e']),
'inserts another list as an element'
);
t.deepEqual(
['a', 'b', 'c', 'd', 'e', 'z'],
insert(8, 'z', ['a', 'b', 'c', 'd', 'e']),
'appends to the end of the list if the index is too large'
);
t.end();
});
test('intersperse', t => {
t.equal('asbtcuvwx', inter('abc', 'stuvwx'), 'continues with longer second arg');
t.equal('kknyolcek.com', inter('knock.com', 'kyle'), 'continues with longer first arg');
t.equal('nkoydlee', inter('node', 'kyle'), 'intersperses same length strings');
t.equal('', inter('', ''), 'intersperses empty strings');
t.throws(
inter.bind(null, ['node', 2]),
'Does not intersperse non strings'
);
t.end();
});
test('merge', t => {
t.equal('asbtcuvwx', merge('abc', 'stuvwx'), 'First string longer');
t.equal('kknyolcek', merge('knock', 'kyle'), 'Second string longer');
t.equal('nkoydlee', merge('node', 'kyle'), 'Same length strings');
t.equal('', merge('', ''), 'Empty strings');
t.throws(merge.bind(null, ['node', 2]), 'Not strings');
t.end();
});
</script></body>
</html>
'use strict';
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } }
var merge = function merge(a, b) {
var len = a.length < b.length ? b.length : a.length;
var i = 0;
var out = '';
while (i < len) {
out += (a[i] || '') + (b[i] || '');
i += 1;
}
return out;
};
var insert = function insert(idx, elt, list) {
var index = idx < list.length && idx >= 0 ? idx : list.length;
var copy = [].concat(list);
copy.splice(idx, 0, elt);
return copy;
};
var inter = function inter(a, b) {
return [].concat(_toConsumableArray(b)).reduce(function (base, letter, index) {
return insert(index * 2 + 1, letter || '', base);
}, [].concat(_toConsumableArray(a))).join('');
};
test('insert', function (t) {
t.deepEqual(['a', 'b', 'x', 'c', 'd', 'e'], insert(2, 'x', ['a', 'b', 'c', 'd', 'e']), 'inserts an element into the given list');
t.deepEqual(['a', 'b', ['s', 't'], 'c', 'd', 'e'], insert(2, ['s', 't'], ['a', 'b', 'c', 'd', 'e']), 'inserts another list as an element');
t.deepEqual(['a', 'b', 'c', 'd', 'e', 'z'], insert(8, 'z', ['a', 'b', 'c', 'd', 'e']), 'appends to the end of the list if the index is too large');
t.end();
});
test('intersperse', function (t) {
t.equal('asbtcuvwx', inter('abc', 'stuvwx'), 'continues with longer second arg');
t.equal('kknyolcek.com', inter('knock.com', 'kyle'), 'continues with longer first arg');
t.equal('nkoydlee', inter('node', 'kyle'), 'intersperses same length strings');
t.equal('', inter('', ''), 'intersperses empty strings');
t.throws(inter.bind(null, ['node', 2]), 'Does not intersperse non strings');
t.end();
});
test('merge', function (t) {
t.equal('asbtcuvwx', merge('abc', 'stuvwx'), 'First string longer');
t.equal('kknyolcek', merge('knock', 'kyle'), 'Second string longer');
t.equal('nkoydlee', merge('node', 'kyle'), 'Same length strings');
t.equal('', merge('', ''), 'Empty strings');
t.throws(merge.bind(null, ['node', 2]), 'Not strings');
t.end();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment