Skip to content

Instantly share code, notes, and snippets.

@ukoloff
ukoloff / round.js
Last active August 29, 2015 13:58
Round floating point number
// including .
.toFixed(n).replace(/\.0*$|(\.[^0]+)0+$/, '$1')
// leaving 1 decimal digit
.toFixed(n).replace(/(\..+?)0+$/, '$1')
@ukoloff
ukoloff / mail.coffee
Created April 21, 2014 17:28
E-mail regexp
# Backbone.Validator
# jQuery Validation (?)
///^
(
(
([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+
(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*
)
|
((\x22)(
@ukoloff
ukoloff / install.sh
Created July 8, 2014 06:18
Ruby@Debian prerequisites
apt-get install autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libsqlite3-dev
@ukoloff
ukoloff / i2path.rb
Last active December 21, 2015 05:19
Directory name for attachment #
def p(i)
(i.to_s.chop.chars<<'%02d'%i).join '/'
end
@ukoloff
ukoloff / random.coffee
Last active December 22, 2015 12:28
Random string
String.random=(q=1)->
s=''
while s.length<q
n=Math.floor 62*Math.random()
s+=String.fromCharCode n%26+'Aa0'.charCodeAt n/26
s
@ukoloff
ukoloff / b64decode.pas
Created September 7, 2013 08:34
Base64 decoder
Function Base64Decode(Const S: String): String;
Var
i: Integer;
W, Buffer, Bits: Word;
Begin
Result:='';
Bits:=0; Buffer:=0; W:=0;
For i:=1 To Length(S) Do
Begin
Case S[i] Of
@ukoloff
ukoloff / wordWrap.coffee
Last active December 24, 2015 02:19
Wrap long line into window
String::wordWrap=(cols=75, rows=1)->
@match(RegExp ".{1,#{cols}}(\\s|$)|.{#{cols}}|.+$", 'g').slice(0, rows).join '\n'
@ukoloff
ukoloff / declension.coffee
Last active December 25, 2015 07:19
Склонение русских числительных. Возвращает число [0-2], то есть 0 предметов, 1 предмет или 2 предмета.
Number::declension=(forms)->
n=/[.,]|1?[1-4]$|$/.exec(@)[0]
n=if 1!=n.length then 0 else if '1'==n then 1 else 2
if arguments.length>1 then arguments[n] else if forms then forms[n] else n
@ukoloff
ukoloff / new.js
Last active December 26, 2015 15:08
JavaScript constructor call/apply
Function.prototype.new = function(){
var args = arguments
var constructor = this
function Fake(){
return constructor.apply(this, args)
}
Fake.prototype = this.prototype
return new Fake
}
@ukoloff
ukoloff / number.js
Last active December 27, 2015 00:08
Regexp for floating number
positive = /^(?!0\d|\.?$)\d*\.?\d*$/
number = /^[-+]?(?!0\d|\.?$)\d*\.?\d*$/