random goes to helpers, getSiblingContent not used
This commit is contained in:
parent
14c760670a
commit
a592b44aa3
2 changed files with 8 additions and 47 deletions
|
|
@ -1,8 +0,0 @@
|
|||
module.exports = {
|
||||
random() {
|
||||
const segment = () => {
|
||||
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
|
||||
};
|
||||
return `${segment()}-${segment()}-${segment()}`;
|
||||
}
|
||||
};
|
||||
|
|
@ -17,49 +17,11 @@ module.exports = {
|
|||
}
|
||||
|
||||
if (itemUrl.length > 1 && pageUrl.indexOf(itemUrl) === 0) {
|
||||
response += ' data-state="active"';
|
||||
response += 'data-state="active"';
|
||||
}
|
||||
|
||||
return response;
|
||||
},
|
||||
/**
|
||||
* Filters out the passed item from the passed collection
|
||||
* and randomises and limits them based on flags
|
||||
*
|
||||
* @param {Array} collection The 11ty collection we want to take from
|
||||
* @param {Object} item The item we want to exclude (often current page)
|
||||
* @param {Number} limit=3 How many items we want back
|
||||
* @param {Boolean} random=true Wether or not this should be randomised
|
||||
* @returns {Array} The resulting collection
|
||||
*/
|
||||
getSiblingContent(collection, item, limit = 3, random = true) {
|
||||
let filteredItems = collection.filter(x => x.url !== item.url);
|
||||
|
||||
if (random) {
|
||||
let counter = filteredItems.length;
|
||||
|
||||
while (counter > 0) {
|
||||
// Pick a random index
|
||||
let index = Math.floor(Math.random() * counter);
|
||||
|
||||
counter--;
|
||||
|
||||
let temp = filteredItems[counter];
|
||||
|
||||
// Swap the last element with the random one
|
||||
filteredItems[counter] = filteredItems[index];
|
||||
filteredItems[index] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
// Lastly, trim to length
|
||||
if (limit > 0) {
|
||||
filteredItems = filteredItems.slice(0, limit);
|
||||
}
|
||||
|
||||
return filteredItems;
|
||||
},
|
||||
|
||||
/**
|
||||
* Take an array of keys and return back items that match.
|
||||
* Note: items in the collection must have a key attribute in
|
||||
|
|
@ -71,5 +33,12 @@ module.exports = {
|
|||
*/
|
||||
filterCollectionByKeys(collection, keys) {
|
||||
return collection.filter(x => keys.includes(x.data.key));
|
||||
},
|
||||
|
||||
random() {
|
||||
const segment = () => {
|
||||
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
|
||||
};
|
||||
return `${segment()}-${segment()}-${segment()}`;
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue