Skip to content

Instantly share code, notes, and snippets.

@sshmyg
sshmyg / animation.vanila.js
Last active September 18, 2015 06:01
Animation
/**
* Анимация. Параметры:
* opts.delta(time) -- временная функция, time=0..1. По умолчанию linear.
* opts.step(progress) -- функция рисования, progress=0..1.
* opts.complete -- функция, которая выполнится по окончании анимации
* opts.dely -- задержка между кадрами, по умолчанию 13
*
* Пример:
var opts = {
@sshmyg
sshmyg / Trigger
Created September 2, 2014 06:21
Trigger DOM event
/**
* @param target can be any DOM Element or other EventTarget
* @param type Event type (i.e. 'click')
*/
var trigger = function (target, type) {
if (document.createEvent) {
event = new Event(type);
target.dispatchEvent(event);
} else {
event = document.createEventObject();
@sshmyg
sshmyg / regex.js
Last active August 29, 2015 14:07 — forked from Integralist/regex.js
var str = "The quick brown fox jumped over the box like an ox with a sox in its mouth";
str.match(/\w(ox)/g); // ["fox", "box", "sox"]
// match (when used with a 'g' flag) returns an Array with all matches found
// if you don't use the 'g' flag then it acts the same as the 'exec' method.
str.match(/\w(ox)/); // ["fox", "ox"]
/\w(ox)/.exec(str); // ["fox", "ox"]

Better local require() paths for Node.js

Problem

When the directory structure of your Node.js application (not library!) has some depth, you end up with a lot of annoying relative paths in your require calls like:

var Article = require('../../../models/article');

Those suck for maintenance and they're ugly.

Possible solutions

@sshmyg
sshmyg / format-number.regexp.js
Created September 18, 2015 06:04
Format number like 1 000 000, with space
var str = '234456234';
str.replace(/(\d)(?=(\d\d\d)+([^\d]|$))/g, '$1 ');
@sshmyg
sshmyg / extend.js
Created December 16, 2015 14:49
SImple extend
var extend = function(target, source) {
target = target || {};
for (var prop in source) {
if (!source.hasOwnProperty(prop)) continue;
if (typeof source[prop] === 'object') {
target[prop] = extend(target[prop], source[prop]);
} else {
target[prop] = source[prop];
@sshmyg
sshmyg / keysimulator.js
Last active March 22, 2016 08:13
Keyboard simulator
var E = {
createEvent: function(e, keyValue, keyCode) {
e = e || 'keypress';
keyValue = keyValue ? keyValue.charCodeAt(0) : keyCode;
if (!(e || keyValue)) {
throw Error('e and keyValue required');
}
@sshmyg
sshmyg / debouncing-throttling-explained-examples.md
Created January 31, 2017 20:27
Difference between Debounce and Throttle

Difference between Debounce and Throttle

Debounce

Debounce a function when you want it to execute only once after a defined interval of time. If the event occurs multiple times within the interval, the interval is reset each time.
Example A user is typing into an input field and you want to execute a function, such as a call to the server, only when the user stops typing for a certain interval, such as 500ms.

Throttle

@sshmyg
sshmyg / README.md
Created May 5, 2018 19:44 — forked from nicerobot/README.md
Mac OS X uninstall script for packaged install of node.js

To run this, you can try:

curl -ksO https://gist.githubusercontent.com/nicerobot/2697848/raw/uninstall-node.sh
chmod +x ./uninstall-node.sh
./uninstall-node.sh
rm uninstall-node.sh
@sshmyg
sshmyg / current-dir-in-iterm-tab-title.sh
Created September 18, 2018 06:22 — forked from phette23/current-dir-in-iterm-tab-title.sh
Set the iTerm tab title to the current directory, not full path.
# put this in your .bash_profile
if [ $ITERM_SESSION_ID ]; then
export PROMPT_COMMAND='echo -ne "\033];${PWD##*/}\007"; ':"$PROMPT_COMMAND";
fi
# Piece-by-Piece Explanation:
# the if condition makes sure we only screw with $PROMPT_COMMAND if we're in an iTerm environment
# iTerm happens to give each session a unique $ITERM_SESSION_ID we can use, $ITERM_PROFILE is an option too
# the $PROMPT_COMMAND environment variable is executed every time a command is run
# see: ss64.com/bash/syntax-prompt.html