xapu

Untitled

Jul 14th, 2017
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. let func = (function(){
  2. function deleteAt (arr, indexer) {
  3. let index = Number(indexer)
  4.  
  5. validateIndex(arr, index)
  6.  
  7. arr = arr.slice(0, index).concat(arr.slice(index + 1)).slice(0)
  8. return arr
  9. }
  10.  
  11. function rollRight (arr) {
  12. arr = arr.slice(arr.length - 1).concat(arr.slice(0, arr.length - 1)).slice(0)
  13. return arr
  14. }
  15.  
  16. function rollLeft (arr) {
  17. arr = arr.slice(1).concat(arr.slice(0, 1)).slice(0)
  18. return arr
  19. }
  20.  
  21. function insertAt (arr, indexer, item) {
  22. let index = Number(indexer)
  23. validateIndex(arr, index, true)
  24. arr = arr.slice(0, index).concat([item]).concat(arr.slice(index)).slice(0)
  25. return arr
  26. }
  27.  
  28. function validateIndex (arr, index, isInsert = false) {
  29. if (isNaN(index)) {
  30. throw new Error(`Error: invalid type.`)
  31. }
  32. if (index < 0) {
  33. throw new Error(`Error: invalid index ${index}.`)
  34. }
  35.  
  36. if (isInsert) {
  37. if (index > arr.length) {
  38. throw new Error(`Error: invalid index ${index}.`)
  39. }
  40. } else if (index >= arr.length) {
  41. throw new Error(`Error: invalid index ${index}.`)
  42. }
  43. }
  44.  
  45. return{
  46. validateIndex,
  47. insertAt,
  48. rollLeft,
  49. rollRight,
  50. deleteAt,
  51. }
  52.  
  53. })()
  54.  
  55. // delete comments for mocha test
  56. //module.exports = {func}
Add Comment
Please, Sign In to add comment