Skip to content

Instantly share code, notes, and snippets.

@vbarbarosh
vbarbarosh / shell_bash_vars_exists
Last active March 31, 2019 20:38
shell_bash_vars_exists – Check if variable contains something https://codescreens.com
#!/bin/bash
# http://www.gnu.org/software/bash/manual/bash.html#The-Set-Builtin
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -o nounset -o errexit -o pipefail
# https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion
# Check if $FOO is unset
test -z ${FOO+x} && echo FOO is unset
@vbarbarosh
vbarbarosh / js_html_escape.js
Last active April 27, 2020 17:45
js_html_escape – Make a string safe to put into html https://codescreens.com
// Make a string safe to put into html
// https://stackoverflow.com/a/6234804/1478566
//
// Consider using https://github.com/mathiasbynens/he for production
function html_escape(s)
{
return s
.replace(/&/g, '&')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
@vbarbarosh
vbarbarosh / js_regex_escape.js
Last active April 2, 2019 19:26
js_regex_escape – Make a string safe to put into regular expression https://codescreens.com
// Make a string safe to put into RegExp
// https://stackoverflow.com/a/6969486
// https://stackoverflow.com/a/3561711
function js_escape_regexp(s)
{
return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
@vbarbarosh
vbarbarosh / js_range.js
Last active April 3, 2019 19:55
js_range – Shortcuts for making ranges of numbers https://codescreens.com
// Shortcuts for making ranges of numbers
function range(begin, end)
{
return Array(end - begin).fill(0).map((v,i) => i + begin);
}
function range_step(begin, end, step)
{
const length = Math.ceil((end - begin)/step);
@vbarbarosh
vbarbarosh / shell_docker_browserless
Last active April 4, 2019 19:48
shell_docker_browserless – Simple way to use browserless in shell scripts https://codescreens.com
#!/bin/bash
# http://www.gnu.org/software/bash/manual/bash.html#The-Set-Builtin
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -o nounset -o errexit -o pipefail
url='https://www.youtube.com/watch?v=u2oKnsiXBr8'
# Using `-o-` instead of `-o /dev/stdout` might lead to
# curl: (23) Failed writing body (4096 != 16384)
@vbarbarosh
vbarbarosh / shell_docker_browserless_htmlpdf
Last active April 5, 2019 18:34
shell_docker_browserless_htmlpdf – Convert html to pdf using Browserless https://codescreens.com
#!/bin/bash
# http://www.gnu.org/software/bash/manual/bash.html#The-Set-Builtin
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -o nounset -o errexit -o pipefail
# usage: cat input.html | ./shell_docker_browserless_htmlpdf > a.pdf
html=`base64 -w0`
@vbarbarosh
vbarbarosh / php_function_alias.php
Last active April 6, 2019 19:50
php_function_alias – Create an alias to a function https://codescreens.com
<?php
# Create an alias to a function
function foobar($a)
{
echo 'hello ', $a, PHP_EOL;
}
function foobar_alias()
@vbarbarosh
vbarbarosh / php_uuid.php
Last active April 7, 2019 17:35
php_uuid – Generate UUID in PHP https://codescreens.com
<?php
# Generate UUID in PHP
# http://en.wikipedia.org/wiki/Universally_unique_identifier
# http://www.php.net/manual/en/function.uniqid.php#94959
function uuid_v4()
{
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
@vbarbarosh
vbarbarosh / php_function_options.php
Last active April 8, 2019 18:19
php_function_options – Default options in functions https://codescreens.com
<?php
# Default options in functions
function foobar($opt = [])
{
$opt += [
'name' => 'default_name',
'title' => 'Default Title',
];
@vbarbarosh
vbarbarosh / php_function_scope.php
Last active April 9, 2019 17:12
php_function_scope – Use a function as an scope for variables https://codescreens.com
<?php
# Use a function as an scope for variables
$foo = call_user_func(function () {
$aa = 'Fusce vitae orci lorem.';
$bb = 'In cursus nisi malesuada.';
$cc = 'Etiam auctor, lectus eu lacinia condimentum.';
return implode("\n", [$aa, $bb, $cc]);
});