Skip to content

Instantly share code, notes, and snippets.

View Dartv's full-sized avatar
🌚
...

Dmytro Artamonov Dartv

🌚
...
View GitHub Profile
@Dartv
Dartv / monthsRangeArray.js
Last active September 13, 2015 07:00
loop through months and insert maps to database
_.each(dateRange(), function (month) {
var beatmaps = osu.getBeatmapsRaw({
since: month
}, Meteor.bindEnvironment(function (err, res) {
if (err) throw new Meteor.Error(err);
var mapsJson = JSON.stringify(res);
var maps = JSON.parse(mapsJson);
for (var map in maps) {
var objectToUpdate = {
@Dartv
Dartv / infinitescroll.js
Created September 13, 2015 06:59
Infinite scroll with viewmodel in meteor
Template.myTemplate.viewmodel({
page: new ReactiveVar(0),
scrollListener: _.debounce(function () {
var self = this;
var diff = $(document).height() - $(window).height();
if ($(window).scrollTop() === diff) {
self.viewmodel.page().set(self.viewmodel.page().get() + 1);
}
}, 50);
});
@Dartv
Dartv / convert.js
Created September 16, 2015 10:27
Convert strings to numbers in meteor mongo
Beatmaps.find().forEach(function(x) {
Beatmaps.update({_id: x._id}, {
$set: {
approved: Number(x.approved),
total_length: Number(x.total_length),
hit_length: Number(x.hit_length),
diff_size: Number(x.diff_size),
diff_overall: Number(x.diff_overall),
diff_approach: Number(x.diff_approach),
diff_drain: Number(x.diff_drain),
@Dartv
Dartv / freq.js
Created September 21, 2015 14:46
Calculating frequency
let nums = [30, 36, 37, 41, 42, 41, 40, 41, 46, 47, 46, 46, 47, 45, 46, 45, 50, 51, 50, 52, 52, 51, 51, 52, 50, 50, 56, 55, 56, 57, 57, 57, 57, 58, 58, 55, 61, 62, 67, 67];
let unique = _.unique(nums);
let absoluteFrequency = _.countBy(nums, num => num);
let relativeFrequency = () => {
let obj = {};
let values = _.values(absoluteFrequency);
let keys = _.keys(absoluteFrequency);
for (var i = 0; i < keys.length; i++) {
obj[keys[i]] = values[i] / nums.length;
}
@Dartv
Dartv / keys.es6
Created September 22, 2015 15:21
Loop through array of keys and make an object with given values or null
handleQueries(queries, values) {
let obj = {};
for (let i = 0; i < queries.length; i++) {
obj[queries[i]] = this[queries[i]]() ? values[i] : null;
}
let vals = _.values(obj).filter(x => x !== null);
return vals;
}
@Dartv
Dartv / remove_dups.js
Created October 1, 2015 20:28
Remove duplicates and leave one document with the most difficultyrating
db.beatmaps.aggregate([
{
"$group": {
"_id": "$beatmapset_id",
"count": { "$sum": 1 },
"uniqueIds": { "$addToSet": "$_id" },
"maxRating": { "$max": "$difficultyrating" }
}
},
@Dartv
Dartv / max.js
Created October 9, 2015 06:02
Unique objects from array with max property
arr = [
{ id: 324, difficulty: 3 },
{ id: 324, difficulty: 2 },
{ id: 324, difficulty: 5 },
{ id: 500, difficulty: 3 },
{ id: 500, difficulty: 3.5 },
{ id: 500, difficulty: 7 }
];
let map = _.groupBy(arr, (el) => el.id)
const $chat = $('.chat-content');
const $message = $(ReactDOM.findDOMNode(component));
const msgOffset = $message.offset().top;
// center the linked message in the chat box
const elHeight = $message.height();
const chatHeight = $chat.height();
const offset = msgOffset - ((chatHeight / 2) - (elHeight / 2));
const getOffset = (offset, index, arr) => arr[(arr.length + index + (offset % arr.length)) % arr.length]
@Dartv
Dartv / itunes_lower_volume.js
Created September 12, 2017 03:18
Itunes userscript to lower volume
// ==UserScript==
// @name Itunes lower volume
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://itunes.apple.com/*/album/*/*$
// @grant none
// ==/UserScript==