spritesheet-creator
Version:
Spritesheet Creator
84 lines (75 loc) • 2.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = sort;
// based on: http://codeincomplete.com/posts/2011/5/7/bin_packing/example/
var Sorters = {
width: function width(a, b) {
return b.width - a.width;
},
height: function height(a, b) {
return b.height - a.height;
},
area: function area(a, b) {
return b.area - a.area;
},
max: function max(a, b) {
return Math.max(b.width, b.height) - Math.max(a.width, a.height);
},
min: function min(a, b) {
return Math.min(b.width, b.height) - Math.min(a.width, a.height);
}
};
var MultiSorters = {
height: function height(a, b) {
return msort(a, b, ['height', 'width']);
},
width: function width(a, b) {
return msort(a, b, ['width', 'height']);
},
area: function area(a, b) {
return msort(a, b, ['area', 'height', 'width']);
},
maxside: function maxside(a, b) {
return msort(a, b, ['max', 'min', 'height', 'width']);
}
};
function sort(method, files) {
var sorter = MultiSorters[method];
if (!sorter) {
throw new Error("unsupported sort method ".concat(method, ", supported methods: ").concat(Object.keys(MultiSorters).join(',')));
}
files.sort(function (file_a, file_b) {
return sorter(file_a.padded, file_b.padded);
});
}
/* sort by multiple criteria */
function msort(a, b, criteria) {
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = criteria[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var c = _step.value;
var diff = Sorters[c](a, b);
if (diff !== 0) {
return diff;
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator["return"] != null) {
_iterator["return"]();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return 0;
}