Skip to content

Instantly share code, notes, and snippets.

function encrypt(text){
var cipher = crypto.createCipher('aes-256-cbc','d6F3Efeq')
var crypted = cipher.update(text,'utf8','hex')
crypted += cipher.final('hex');
return crypted;
}
function decrypt(text){
var decipher = crypto.createDecipher('aes-256-cbc','d6F3Efeq')
var dec = decipher.update(text,'hex','utf8')
@tborychowski
tborychowski / js-number-format.js
Created October 11, 2013 10:39
JS :: format number as currency
(2500).toLocaleString("en-GB", {style: "currency", currency: "GBP", minimumFractionDigits: 2})
@tborychowski
tborychowski / js-overrides.js
Last active December 20, 2015 00:09
JS :: some native objects "extensions"
/**
* JS OVERRIDES
*/
(function (window) {
'use strict';
String.prototype.trim = function (str) {return this.ltrim(str).rtrim(str); };
String.prototype.ltrim = function (str) { return this.replace(new RegExp('^' + (str ? str : '\\s') + '+'), ''); };
String.prototype.rtrim = function (str) { return this.replace(new RegExp((str ? str : '\\s') + '+$'), ''); };
String.prototype.ucfirst = function () {
@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"
@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 / quick-string.js
Created November 28, 2012 13:10
JS :: Quickly create a string of dots
new Array(20).join('.')
@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 / 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 / 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 / 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);
};