Skip to content

Instantly share code, notes, and snippets.

View dominikbulaj's full-sized avatar

Dominik Bułaj dominikbulaj

View GitHub Profile
@dominikbulaj
dominikbulaj / time_manipulation.js
Created August 25, 2015 19:29
Add or remove time from given date. Examples: getNewTime('7 days') // return current time + 7 days getNewTime('-1 month') // return past time (last month)
/**
* Add (or remove) some time from current date. Returns new Date
*
* @param {string} elapsedTime
* @param {Date|null} dateObject
* @returns {Date}
* @private
*/
function getNewTime(elapsedTime, dateObject) {
@dominikbulaj
dominikbulaj / gist:4302666ffa691e9f1b49
Created November 27, 2015 14:07 — forked from stuart11n/gist:9628955
rename git branch locally and remotely
git branch -m old_branch new_branch # Rename branch locally
git push origin :old_branch # Delete the old branch
git push --set-upstream origin new_branch # Push the new branch, set local branch to track the new remote
@dominikbulaj
dominikbulaj / git_rename_tag.sh
Last active November 27, 2015 14:24
git rename tag
git tag new old # create new tag
git tag -d old # remove old tag
git push origin :refs/tags/old # remove old tag from remote repository
git push --tags # push tags
@dominikbulaj
dominikbulaj / iframe_handle_click.js
Last active October 12, 2016 13:34
Handle click inside iframe #ES2015 #ES6 code
document.querySelector('iframe#abc123').contentDocument
.querySelectorAll('a')
.forEach(a => a.addEventListener('click', () => console.log('CLICK!')));
@dominikbulaj
dominikbulaj / new_gist_file.js
Last active October 21, 2016 06:53
one-time handling of the click event using vanilla addEventListener() From https://medium.com/dev-channel/once-upon-an-event-listener-f516bca519e6#.e9lx7f787
element.addEventListener('click', function cb(event) {
// ...one-time handling of the click event...
event.target.removeEventListener(event.type, cb);
});
// or:
element.addEventListener('click', function(event) {
// ...one-time handling of the click event...
}, {once: true});
@dominikbulaj
dominikbulaj / fibonacci-generator.js
Created January 27, 2017 10:57 — forked from jfairbank/fibonacci-generator.js
Fibonacci ES6 Generator
function *fibonacci(n) {
const infinite = !n && n !== 0;
let current = 0;
let next = 1;
while (infinite || n--) {
yield current;
[current, next] = [next, current + next];
}
}
@dominikbulaj
dominikbulaj / scratchParams.es6
Created March 23, 2018 09:17
Simple code to get URL searchParams as key-value object
location.search.substr(1)
.split('&')
.reduce((ac, cur) => {
if (cur) {
const kv = cur.split('=')
ac[kv[0]] = decodeURIComponent(kv[1].replace(/\+/g, ' '))
}
return ac
}, {})
@dominikbulaj
dominikbulaj / mysql_datetime_to_js_date.js
Created June 11, 2018 11:36
MySQL DATETIME to JavaScript Date
// example MySQL DATETIME
const dateTime = '2017-02-04 11:23:54';
let dateTimeParts= dateTime.split(/[- :]/); // regular expression split that creates array with: year, month, day, hour, minutes, seconds values
dateTimeParts[1]--; // monthIndex begins with 0 for January and ends with 11 for December so we need to decrement by one
const dateObject = new Date(...dateTimeParts); // our Date object