Skip to content

Instantly share code, notes, and snippets.

@tborychowski
tborychowski / numberFormat.js
Created March 6, 2012 21:47
JS :: Format a number
/**
* Formats numbers (or string numbers)
* @param number int or int-parsable string
* @param prec decimal precision
* @returns formatted number as string
*/
numberFormat : function (number, prec) {
var ext, name, numS, rgx = /(\d+)(\d{3})/;
number = number || '0';
prec = prec || 0;
@tborychowski
tborychowski / _lib.less
Created September 28, 2012 09:03
LESS :: _lib - mix-in library
/**
* LESS mix-in library
* vendor prefixes verified with prefixr.com and caniuse.com [2012-09-28]
* https://gist.github.com/3798764
*/
@img-base: "../img";
@icon-base: "../img/icons";
.b(@v) when (@v = 0) { font-weight: normal; }
@tborychowski
tborychowski / utils.js
Last active September 13, 2018 15:53
JS :: Utils - helper library
/**
* https://gist.github.com/tborychowski/3799025
*/
var UTILS = {
/*jshint maxlen: 500, onevar: false */
isTouch : (/iPhone|iPod|iPad/ig).test(navigator.userAgent),
/**
* Modified & lintified version of https://gist.github.com/527683
@tborychowski
tborychowski / js-array-remove.js
Created October 10, 2012 10:33 — forked from danro/js-array-remove.js
JS :: Array Remove
// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest);
};
@tborychowski
tborychowski / import_sql_file_to_db.php
Created October 11, 2012 14:13
PHP :: Import SQL file to DB
// import sql file
$file_content = file('updates.sql');
$query = "";
foreach($file_content as $sql_line){
if(trim($sql_line) != "" && strpos($sql_line, "--") === false){
$query .= $sql_line;
if (substr(rtrim($query), -1) == ';'){
$result = $dbo->dbEdit($query);
echo $query.'<br>Result: '.($result==1?'OK':$result).'<br><br><br>';
$query = "";
@tborychowski
tborychowski / get_week_number.js
Created October 11, 2012 14:14
JS :: Get Week Number (ISO 8601)
//Returns ISO 8601 week number and year
Date.prototype.getFullWeek = function(){
var jan1, w, d = new Date(this);
d.setDate(d.getDate()+4-(d.getDay()||7)); // Set to nearest Thursday: current date + 4 - current day number, make Sunday's day number 7
jan1 = new Date(d.getFullYear(),0,1); // Get first day of year
w = Math.ceil((((d-jan1)/86400000)+1)/7); // Calculate full weeks to nearest Thursday
return {y: d.getFullYear(), w: w };
};
//Returns ISO 8601 week number
Date.prototype.getWeek = function(){
@tborychowski
tborychowski / form_validation.js
Last active October 11, 2015 14:28
JS :: Simple form validation
function validate(f,v,a,x){
for(a=0;x=f[a++];)
if((v=window[x.getAttribute('valid')])&&!v(x.value)){
alert(x.getAttribute('alert'));
return !x.focus();
}
}
function notempty(x){return x>''}
function ismail(e){return /^[\w\.-]{2,}@[\w\.-]+\.[a-z]{2,5}$/i.test(e)}
@tborychowski
tborychowski / quick-string.js
Created November 28, 2012 13:10
JS :: Quickly create a string of dots
new Array(20).join('.')
@tborychowski
tborychowski / js caret position.js
Created February 22, 2013 13:09
JS :: get and set caret position inside input box
function getCaretPosition (ctrl) {
var caretPos = 0;
// IE
if (document.selection) {
ctrl.focus ();
var sel = document.selection.createRange();
sel.moveStart ('character', -ctrl.value.length);
caretPos = sel.text.length;
}
// Firefox
@tborychowski
tborychowski / detect-ie-quickhack.js
Last active December 19, 2015 11:49
JS :: detect IE
// the quickest way - by the bug :-)
var IE = !+"\v1"