Skip to content

Instantly share code, notes, and snippets.

View Artem-Schander's full-sized avatar

Artem Schander Artem-Schander

View GitHub Profile
@Artem-Schander
Artem-Schander / loop.js
Created November 22, 2019 17:44
Recursive JavaScript function with delay in each loop
const run = (cb, repeats, timeout = 100, i = 0) => {
cb(i)
setTimeout(() => {
++ i
if (repeats >= i + 1) run(cb, repeats, timeout, i)
}, timeout)
}
run(i => {
console.log(i)
@Artem-Schander
Artem-Schander / 1. README.md
Last active December 21, 2018 18:49
Enhanced _.get

Lodash _.get enhanced with * wildcard

See in action jsfiddle

@Artem-Schander
Artem-Schander / 1. README.md
Last active October 20, 2018 14:14 — forked from joseluisq/1README.md
Configure PHP Lumen 5 HTTP Exception Handlers with common JSON responses.

Lumen 5 HTTP Exception Handlers with JSON support.

Configure PHP Lumen 5 HTTP Exception Handlers with common JSON responses.

image

Setup

Copy (replace) only the attached files to their respective directories. app/Exceptions/Handler.php and app/Http/Middleware/Authenticate.php

@Artem-Schander
Artem-Schander / docker-swarm-auto-rebalance.sh
Last active September 30, 2020 17:47 — forked from zerowebcorp/docker-swarm-auto-rebalance.sh
Docker Swarm automatically rebalance when new worker node joins
FILE=/tmp/worker.nodes
touch ${FILE} || exit
for node in `docker node ls --filter role=worker -q`; do
if grep -Fxq "${node}" ${FILE}
then
echo "This node ${node} already exists"
else
echo "This node ${node} joined recently, so rebalance"
for service in `docker service ls -q`; do
docker service update --with-registry-auth --detach=true --force ${service}
FROM php:7.2-fpm
RUN export CFLAGS="$PHP_CFLAGS" CPPFLAGS="$PHP_CPPFLAGS" LDFLAGS="$PHP_LDFLAGS"
RUN apt-get update && apt-get install -y --no-install-recommends \
libmcrypt-dev mysql-client libmagickwand-dev git zip \
ghostscript imagemagick libmagickwand-dev \
&& rm -rf /var/lib/apt/lists/*
RUN pecl install imagick mcrypt-1.0.1 \
@Artem-Schander
Artem-Schander / removeDeepByKey.js
Created October 1, 2017 15:28
recursively remove object values by key - lodash mixin
import _ from 'lodash';
// usage: _.removeDeepByKey(someObject, 'unwantedKey');
_.mixin({
'removeDeepByKey': function(obj, keyToBeRemoved) {
return _.transform(obj, function(result, value, key) {
if (_.isObject(value)) {
value = _.removeDeepByKey(value, keyToBeRemoved);
}
@Artem-Schander
Artem-Schander / fileProxy.php
Last active July 14, 2017 07:14
Get files from folders out of public access and pass them to the browser
/**
* @param string $fileName
* @param string $mime
* @param bool $download
* @param string $path
* @return mixed
*/
function fileProxy($fileName, $mime = 'auto', $download = false, $path = '/some/folder/')
{
if (basename($fileName) != $fileName) {
@Artem-Schander
Artem-Schander / scanDir.php
Last active July 20, 2017 07:47
Find files in specific folder
function scanDir($dir, $contains = null, $sort = 'date', $order = 'ASC', $ignored = array('.', '..', '.htaccess'))
{
$files = array();
$finfo = finfo_open(FILEINFO_MIME_TYPE);
// die('/'.$contains.'(\D)/');
foreach (scandir($dir) as $i => $file) {
if (in_array($file, $ignored)) {
function slugify($text)
{
// replace non letter or digits by -
$text = preg_replace('~[^\pL\d]+~u', '-', $text);
// transliterate
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
// remove unwanted characters
$text = preg_replace('~[^-\w]+~', '', $text);
@Artem-Schander
Artem-Schander / linearEquation.js
Last active April 22, 2017 19:29
Linear equation
// f(x) = mx + n
// f(y) = (y - n) / m
var start = {
x: 1,
y: 12
}
var end = {
x: 4,
y: 0