Skip to content

Commit

Permalink
Merge b8c61d1 into fe18316
Browse files Browse the repository at this point in the history
  • Loading branch information
appleJax committed Dec 23, 2018
2 parents fe18316 + b8c61d1 commit ac6670c
Show file tree
Hide file tree
Showing 4 changed files with 2,557 additions and 11 deletions.
64 changes: 64 additions & 0 deletions iePolyfills.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* eslint-disable no-bitwise, no-extend-native, prefer-rest-params */

module.exports = function iePolyfills() {
// IE Polyfill for Array.prototype.fill
if (!Array.prototype.fill) {
Object.defineProperty(Array.prototype, 'fill', {
value(value) {
// Steps 1-2.
if (this == null) {
throw new TypeError('this is null or not defined');
}

const O = Object(this);

// Steps 3-5.
const len = O.length >>> 0;

// Steps 6-7.
const start = arguments[1];
const relativeStart = start >> 0;

// Step 8.
let k = relativeStart < 0
? Math.max(len + relativeStart, 0)
: Math.min(relativeStart, len);

// Steps 9-10.
const end = arguments[2];
const relativeEnd = end === undefined
? len : end >> 0;

// Step 11.
const final = relativeEnd < 0
? Math.max(len + relativeEnd, 0)
: Math.min(relativeEnd, len);

// Step 12.
while (k < final) {
O[k] = value;
k += 1;
}

// Step 13.
return O;
},
});
}

// IE Polyfill for Object.entries
if (!Object.entries) {
Object.entries = function entries(obj) {
const ownProps = Object.keys(obj);
let i = ownProps.length;
const resArray = new Array(i); // preallocate the Array

while (i) {
i -= 1;
resArray[i] = [ownProps[i], obj[ownProps[i]]];
}

return resArray;
};
}
};
17 changes: 9 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const _ = require('lodash');
// Load polyfills for IE Array.prototype.fill and Object.entries
require('./iePolyfills')();

/**
* Returns a comparison function used to compare entries during ranking
Expand All @@ -22,38 +23,38 @@ function getRankingFn(strategy) {
if (strategy === 'modified-competition') {
// Modified competition ranking ("1334" ranking)
return (start, length) => ({
current: _.fill(Array(length), start + (length - 1)),
current: Array(length).fill(start + (length - 1)),
next: start + length,
});
}

if (strategy === 'dense') {
// Dense ranking ("1223" ranking)
return (start, length) => ({
current: _.fill(Array(length), start),
current: Array(length).fill(start),
next: start + 1,
});
}

if (strategy === 'ordinal') {
// Ordinal ranking ("1234" ranking)
return (start, length) => ({
current: _.range(start, start + length),
current: Array(length).fill(null).map((_, i) => start + i),
next: start + length,
});
}

if (strategy === 'fractional') {
// Fractional ranking ("1 2.5 2.5 4" ranking)
return (start, length) => ({
current: _.fill(Array(length), start + ((length - 1) / 2)),
current: Array(length).fill(start + ((length - 1) / 2)),
next: start + length,
});
}

// Standard competition ranking ("1224" ranking)
return (start, length) => ({
current: _.fill(Array(length), start),
current: Array(length).fill(start),
next: start + length,
});
}
Expand Down Expand Up @@ -88,15 +89,15 @@ module.exports = {

items.forEach((e) => {
const score = scoreFn(e);
if (_.has(buckets, score)) {
if (buckets[score]) {
buckets[score].push(e);
} else {
buckets[score] = [e];
}
});

// convert to array for sorting
buckets = _.toPairs(buckets);
buckets = Object.entries(buckets);

// sort based on the score
buckets.sort(compareFn);
Expand Down
Loading

0 comments on commit ac6670c

Please sign in to comment.