Skip to content

Instantly share code, notes, and snippets.

@Hamzali
Created July 10, 2018 14:28
Show Gist options
  • Save Hamzali/79d2d6eaab00760e391a6ea4a35675e9 to your computer and use it in GitHub Desktop.
Save Hamzali/79d2d6eaab00760e391a6ea4a35675e9 to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/rijiwor
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<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 validateParams = function validateParams(arr, key) {
if (!Array.isArray(arr)) {
return new Error('Invalid array!');
}
if (key < 0 || key >= arr.length) {
return new Error('Key out of bounds!');
}
};
var rmItem = function rmItem(arr, key) {
var err = validateParams(arr, key);
if (err) {
throw err;
}
return [].concat(_toConsumableArray(arr.slice(0, key)), _toConsumableArray(arr.slice(key + 1, arr.length)));
};
var addItem = function addItem(arr, item, key) {
var err = validateParams(arr, key);
if (err) {
throw err;
}
return [].concat(_toConsumableArray(arr.slice(0, key)), [item], _toConsumableArray(arr.slice(key, arr.length)));
};
// Usage
var arr = [0, 1, 2, 3, 4, 5, 6, 7];
// removes the item with index 4
rmItem(arr, 4);
// result: [0, 1, 2, 3, 5, 6, 7]
// add 6 to index 5
addItem(arr, 6, 5);
// result: [0, 1, 2, 3, 4, 6, 5, 6, 7]
</script>
<script id="jsbin-source-javascript" type="text/javascript">const validateParams = (arr, key) => {
if (!Array.isArray(arr)) {
return new Error('Invalid array!')
}
if (key < 0 || key >= arr.length) {
return new Error('Key out of bounds!')
}
}
const rmItem = (arr, key) => {
const err = validateParams(arr, key)
if (err) {
throw err;
}
return [
...arr.slice(0, key), ...arr.slice(key + 1, arr.length)
]
}
const addItem = (arr, item, key) => {
const err = validateParams(arr, key)
if (err) {
throw err;
}
return [
...arr.slice(0, key), item, ...arr.slice(key, arr.length)
]
}
// Usage
const arr = [0, 1, 2, 3, 4, 5, 6, 7]
// removes the item with index 4
rmItem(arr, 4)
// result: [0, 1, 2, 3, 5, 6, 7]
// add 6 to index 5
addItem(arr, 6, 5)
// result: [0, 1, 2, 3, 4, 6, 5, 6, 7]</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 validateParams = function validateParams(arr, key) {
if (!Array.isArray(arr)) {
return new Error('Invalid array!');
}
if (key < 0 || key >= arr.length) {
return new Error('Key out of bounds!');
}
};
var rmItem = function rmItem(arr, key) {
var err = validateParams(arr, key);
if (err) {
throw err;
}
return [].concat(_toConsumableArray(arr.slice(0, key)), _toConsumableArray(arr.slice(key + 1, arr.length)));
};
var addItem = function addItem(arr, item, key) {
var err = validateParams(arr, key);
if (err) {
throw err;
}
return [].concat(_toConsumableArray(arr.slice(0, key)), [item], _toConsumableArray(arr.slice(key, arr.length)));
};
// Usage
var arr = [0, 1, 2, 3, 4, 5, 6, 7];
// removes the item with index 4
rmItem(arr, 4);
// result: [0, 1, 2, 3, 5, 6, 7]
// add 6 to index 5
addItem(arr, 6, 5);
// result: [0, 1, 2, 3, 4, 6, 5, 6, 7]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment