Advertisement
Guest User

Untitled

a guest
Nov 15th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. const compareObjects = require('../helpers/compareObjects');
  2. const matchesProp = require('../helpers/matchesProperty');
  3. const isProp = require('../helpers/isProp');
  4.  
  5. function funcSplice(array, pred) {
  6. let index = 0;
  7.  
  8. for (let i = 0; i < array.length; i += 1) {
  9. if (!pred(array[i])) {
  10. index = i;
  11. break;
  12. }
  13. }
  14.  
  15. return array.splice(index);
  16. }
  17.  
  18. function objSplice(array, pred) {
  19. let index = 0;
  20.  
  21. for (let i = 0; i < array.length; i += 1) {
  22. if (!compareObjects(array[i], pred)) {
  23. index = i;
  24. break;
  25. }
  26. }
  27.  
  28. return array.splice(index);
  29. }
  30.  
  31. function arrSplice(array, pred) {
  32. let index = 0;
  33.  
  34. for (let i = 0; i < array.length; i += 1) {
  35. if (!matchesProp(array[i], pred[0], pred[1])) {
  36. index = i;
  37. break;
  38. }
  39. }
  40.  
  41. return array.splice(index);
  42. }
  43.  
  44. function strSplice(array, pred) {
  45. let index = 0;
  46.  
  47. for (let i = 0; i < array.length; i += 1) {
  48. if (!isProp(array[i], pred)) {
  49. index = i;
  50. break;
  51. }
  52. }
  53.  
  54. return array.splice(index);
  55. }
  56.  
  57. function dropWhile(array, pred) {
  58. const tempArr = [...array];
  59.  
  60. if (pred instanceof Function) return funcSplice(tempArr, pred);
  61. if (pred instanceof Array) return arrSplice(tempArr, pred);
  62. if (pred instanceof Object) return objSplice(tempArr, pred);
  63. if (typeof pred === 'string') return strSplice(tempArr, pred);
  64.  
  65. return null;
  66. }
  67.  
  68.  
  69. module.exports = dropWhile;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement