clean up positions (#1919)

* clean up positions

* perf i guess
This commit is contained in:
Gus Caplan
2017-09-09 15:40:32 -05:00
committed by Crawl
parent 98582cd1b7
commit 2ffe3048ba
6 changed files with 78 additions and 131 deletions

View File

@@ -1,3 +1,4 @@
const Long = require('long');
const snekfetch = require('snekfetch');
const Constants = require('./Constants');
const ConstantsHttp = Constants.DefaultOptions.http;
@@ -290,6 +291,23 @@ class Util {
return color;
}
/**
* Sort by discord's position then ID thing
* @param {Collection} collection Collection of objects to sort
* @returns {Collection}
*/
static discordSort(collection) {
return collection
.sort((a, b) => a.rawPosition - b.rawPosition || Long.fromString(a.id).sub(Long.fromString(b.id)).toNumber());
}
static setPosition(item, position, relative, sorted, route, handle, reason) {
let updatedItems = sorted.array();
Util.moveElementInArray(updatedItems, item, position, relative);
updatedItems = updatedItems.map((r, i) => ({ id: r.id, position: i }));
return route.patch({ data: updatedItems, reason }).then(handle || (x => x));
}
}
module.exports = Util;