Last active
February 1, 2017 23:19
-
-
Save ydatech/2f27f663c396f0bd95799813a21ba5cc to your computer and use it in GitHub Desktop.
Class that I proud of
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * MoveLayer (ES6 class) | |
| * Move array item up and down | |
| */ | |
| class MoveLayer{ | |
| static to(direction ='up',data = { items:[] , activeIndex:0}){ | |
| if(data.activeIndex >= data.items.length-1 || data.activeIndex <= 0 || !Array.isArray(data.items)) | |
| { | |
| return Array.isArray(data.items)?[...data.items]:[] | |
| } | |
| else{ | |
| switch(direction) | |
| { | |
| case 'up': | |
| return [ | |
| ...data.items.slice(0,data.activeIndex), | |
| data.items[data.activeIndex+1], | |
| data.items[data.activeIndex], | |
| ...data.items.slice(data.activeIndex+2) | |
| ] | |
| case 'down': | |
| return [ | |
| ...data.items.slice(0,data.activeIndex-1), | |
| data.items[data.activeIndex], | |
| data.items[data.activeIndex-1], | |
| ...data.items.slice(data.activeIndex+1) | |
| ] | |
| } | |
| } | |
| } | |
| } | |
| /* | |
| * Using Class Move Layer | |
| */ | |
| const items = [0,1,2,3,4,5,6,7,8,9] | |
| const data = {items:items, activeIndex:2 } | |
| console.log(Array.isArray(items)) | |
| const newItems = MoveLayer.to('up', data) | |
| console.log(newItems) | |
| const newItems2 = MoveLayer.to('down',data) | |
| console.log(newItems2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment