Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const compareObjects = require('../helpers/compareObjects');
- const matchesProp = require('../helpers/matchesProperty');
- const isProp = require('../helpers/isProp');
- function funcSplice(array, pred) {
- let index = 0;
- for (let i = 0; i < array.length; i += 1) {
- if (!pred(array[i])) {
- index = i;
- break;
- }
- }
- return array.splice(index);
- }
- function objSplice(array, pred) {
- let index = 0;
- for (let i = 0; i < array.length; i += 1) {
- if (!compareObjects(array[i], pred)) {
- index = i;
- break;
- }
- }
- return array.splice(index);
- }
- function arrSplice(array, pred) {
- let index = 0;
- for (let i = 0; i < array.length; i += 1) {
- if (!matchesProp(array[i], pred[0], pred[1])) {
- index = i;
- break;
- }
- }
- return array.splice(index);
- }
- function strSplice(array, pred) {
- let index = 0;
- for (let i = 0; i < array.length; i += 1) {
- if (!isProp(array[i], pred)) {
- index = i;
- break;
- }
- }
- return array.splice(index);
- }
- function dropWhile(array, pred) {
- const tempArr = [...array];
- if (pred instanceof Function) return funcSplice(tempArr, pred);
- if (pred instanceof Array) return arrSplice(tempArr, pred);
- if (pred instanceof Object) return objSplice(tempArr, pred);
- if (typeof pred === 'string') return strSplice(tempArr, pred);
- return null;
- }
- module.exports = dropWhile;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement