Skip to content

Instantly share code, notes, and snippets.

View gskema's full-sized avatar

Gytis Šk. gskema

  • Kaunas, Lithuania
View GitHub Profile
@gskema
gskema / script.js
Created May 19, 2016 18:14
Bootstrap screen width change event JS
var resizeBuffer = null;
var prevWidthName = getWindowWidthName();
$(window).on('resize', function () {
clearTimeout(resizeBuffer);
resizeBuffer = setTimeout(function() {
var newWidthName = getWindowWidthName();
if (newWidthName != prevWidthName) {
prevWidthName = newWidthName;
$(document).trigger(newWidthName);
}
@gskema
gskema / color-gradient.js
Last active March 2, 2024 22:14
Generate gradient color from 2 or 3 colors using JavaScript. Example: https://jsfiddle.net/e18g5f3L/
/**
* You may use this function with both 2 or 3 interval colors for your gradient.
* For example, you want to have a gradient between Bootstrap's danger-warning-success colors.
*/
function colorGradient(fadeFraction, rgbColor1, rgbColor2, rgbColor3) {
var color1 = rgbColor1;
var color2 = rgbColor2;
var fade = fadeFraction;
// Do we have 3 colors for the gradient? Need to adjust the params.
@gskema
gskema / NotificationRepository.php
Last active August 5, 2016 12:39
[SQL, PHP]: Fetch rows from two column ordered lists by specifying IDs
<?php
/**
* Returns customer notifications
*
* @param int|null $limit
* @param int|null $afterId
* @param int|null $beforeId
*
* [0]*************************************[totalRows]
@gskema
gskema / lpad.js
Created August 31, 2016 18:09
Left pad JavaScript string
export function lpad(pad, string) {
var trueString = '' + string;
return pad.substring(0, pad.length - trueString.length) + trueString;
}
@gskema
gskema / script.js
Created October 16, 2016 12:58
Cached jQuery .find and .closest methods. On repeated invokation, returns jQuery objects / DOM references from cache.
// jQuery warriors, assemble!
$.fn.cachedFind = function (selector) {
var cache = this.data('cached-find') || {};
if (undefined === cache[selector]) {
cache[selector] = this.find(selector);
this.data('cached-find', cache);
}
return cache[selector];
};
@gskema
gskema / line_count.php
Created October 17, 2016 07:42
Counts the number of lines in a container where character count per line is known or approximate.
<?php
/**
* Counts the number of lines in a container where character count per line is known or approximate.
* It assumes that words can be broken.
*
* @param string $text
* @param int $perLine
*
* @return int
@gskema
gskema / letters.php
Last active October 19, 2016 11:07
Expresses a given decimal number in an arbitrary unique letter base. PHP
<?php
/**
* Expresses a given number in a a-z letter base, e.g.:
* 0 -> a, 27 -> ba, 932 -> bjw, ...
*
* @param int $number
*
* @return string
*/
@gskema
gskema / float.js
Created October 24, 2016 12:55
Correctly rounds JavaScript number (float) to 2 decimals.
function roundFloat(number, decimals) {
return (Math.round(number * (Math.pow(10, decimals))) / Math.pow(10, decimals)).toFixed(decimals)
}
@gskema
gskema / html_trim_whitespace.php
Last active August 4, 2018 06:44
HTML whitespace trim RegEx (regular expression) for PHP. Useful for trimming HTML content before TCPDF processing.
<?php
// Trims whitespaces after any tags. Assumes that a tag ends with '/>', '\w>', '">'.
$html = preg_replace('#(["|\/|\w]>)(\s+)#', '$1', $html);
// Trims whitespaces before any tags. Assumes that tags start with '<\w+'
$html = preg_replace('#(\s+)(<\/?\w+)#', '$2', $html);
// Trims spaces between tags (both opening and closing).
// Assumes that a tag ends with '/>', '\w>', '">' and starts with '<\w+'
@gskema
gskema / escape_date_math_index.php
Last active January 10, 2017 15:08
ElasticSearch escape date math index names in request path
<?php
// @see https://www.elastic.co/guide/en/elasticsearch/reference/5.x/date-math-index-names.html
// @TODO <elastic\\{ON\\}-{now/M}>
function escapePathWithDateMath($path)
{
$symbols = ['<', '>', '/', '{', '}', '|', '+', ':', ','];
$escaped = ['%3C', '%3E', '%2F', '%7B', '%7D', '%7C', '%2B', '%3A', '%2C'];
// Check if the string is already escaped