Skip to content

Instantly share code, notes, and snippets.

@davestewart
Created February 13, 2019 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davestewart/64fcdf787937a1a98b7233948dc9f158 to your computer and use it in GitHub Desktop.
Save davestewart/64fcdf787937a1a98b7233948dc9f158 to your computer and use it in GitHub Desktop.
Gits for medium article: https://medium.com/p/1b2fee1721ec
// collection.js
export function forEach (arr, callback) { ... }
export function map (arr, callback) { ... }
export function get (arr, id, key = 'id') { ... }
export function getIndex (arr, id, key = 'id') { ... }
export function add (arr, item, index = -1) { ... }
export function update (arr, id, values) { ... }
export function move (fromArr, id, toIndex, toArr = fromArr) { ... }
export function moveByIndex (fromArr, fromIndex, toIndex, toArr = fromArr) { ... }
export function remove (arr, id) { ... }
import { move } from '@/common/utils/collection'
function onDragDrop (fromIndex, toIndex) {
move(this.data.bookmarks, fromIndex, toIndex)
}
export default class Window {
constructor (data) {
this.title = data.title
this.tabs = data.tabs
}
moveTab (from, to) { ... }
}
const window = new Window(data)
window.moveTab(1, 5)
const tabs = window.tabs
// Window.js
// additional code omitted for clarity
import {
get,
add,
update,
move,
remove
} from '@/common/utils/collection'
export default class Window {
constructor (data = {}) { ... }
updateTab (info) {
return update(this, info)
}
getTab (tabId) {
return get(this.tabs, tabId)
}
addTab (info, index = -1) {
return add(this.tabs, Tab.create(info), index)
}
moveTab (tabId, toIndex) {
return move(this.tabs, tabId, toIndex)
}
removeTab (tabId) {
return remove(this.tabs, tabId)
}
}
export default class Window {
static moveTab (tabs, from, to) { ... }
constructor (data) {
this.title = data.title
this.tabs = data.tabs
}
}
Window.moveTab(data.tabs, 1, 5)
export default class Window {
constructor (data) {
this.title = data.title
this.tabs = data.tabs
}
moveTab (from, to) { ... }
}
function moveTab (tabs, from, to) { ... }
moveTab(data.tabs, 1, 5)
export default class Window {
constructor (data) {
this.title = data.title
this.tabs = data.tabs
}
reorder (from, to) {
reorder(this.tabs, from, to)
}
}
function reorder (tabs, from, to) { ... }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment