Skip to content

Instantly share code, notes, and snippets.

@qwersk
qwersk / gist:4c1ae4bf6a38570e86b0f7356ae5bce3
Created September 30, 2019 08:41
Function always returns a random number between min and max (both included) #js
function getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1) ) + min;
}
@qwersk
qwersk / gist:f7f659ad94c0cb61d8c9c2614ec7350f
Created September 25, 2019 13:55
Sort object array #js
function myFunction(arr) {
arr.sort(function(a, b){
var x = a.type.toLowerCase();
var y = b.type.toLowerCase();
if (x < y) {return -1;}
if (x > y) {return 1;}
return 0;
});
}
@qwersk
qwersk / trim.js
Created September 24, 2019 09:22
Custom trim function #JS
if (!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
};
}
var str = " Hello World! ";
alert(str.trim());
@qwersk
qwersk / parse_url.js
Created July 18, 2019 05:16
Parse url #JS
var url = "https://developer.mozilla.org/en-US/Web/JavaScript";
var parsedURL = /^(\w+)\:\/\/([^\/]+)\/(.*)$/.exec(url);
var [, protocol, fullhost, fullpath] = parsedURL;
console.log(protocol); // выведет "https"
В js есть 3 способа назначить обработчика события:
1. Через HTML-атрибут, например, onclick
2. С помощью DOM объекта. document.getElementById('el').onclick = function() {...}
Таким способом можно назначить только один обработчик. Отработает самый нижний по коду обработчик
3. Через метод addEventListener
Данный метод может принимать в качестве третьего аргумента true/false или объект:
{
capture: false,
once: false,
@qwersk
qwersk / package_composer.json
Last active February 23, 2019 10:05
Installing package from local git repository #composer
// package's composer.json file
{
"name": "vendor-name/package-name",
"type": "library",
"autoload": {
"psr-4": {
"namespace\\": "src/"
}
},
git diff --color filename
@qwersk
qwersk / gist:2863c6e6c11d1fd796fed1676ff11370
Created November 21, 2018 07:53
Linux commands #linux
Find out folder size:
`du -sh file_path`
du (disc usage) command estimates file_path space usage
-s, --summarize
display only a total for each argument
-h, --human-readable
print sizes in human readable format (e.g., 1K 234M 2G)
@qwersk
qwersk / change_author.sh
Created September 25, 2018 08:59
Change all commits author's name and email #GIT
#!/bin/sh
git filter-branch -f --env-filter '
OLD_EMAIL="OLD_EMAIL"
CORRECT_NAME="NEW_NAME"
CORRECT_EMAIL="NEW_EMAIL"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_COMMITTER_NAME="$CORRECT_NAME"
export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
@qwersk
qwersk / gist:035aa5ed792b2c2c3d9e58a7481e3b4b
Last active September 7, 2018 04:24
Array sorting by alphabet #PHP
<?php
setlocale(LC_ALL, 'ru_RU.CP1251', 'rus_RUS.CP1251', 'Russian_Russia.1251');
sort($example, SORT_LOCALE_STRING);