Skip to content

Instantly share code, notes, and snippets.

@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*$/
@ukoloff
ukoloff / fa.coffee
Last active December 28, 2015 11:59
Extract icon names from Font Awesome
$.fontAwesome = ->
do fontAwesome
return
fontAwesome = ->
$('body').append do withOut.compile ->
div id: 'font-awesome-icons', ->
button
type: 'button'
class: 'close'
@ukoloff
ukoloff / pager.coffee
Last active December 28, 2015 11:59
Pagination for bootstrap
$.pager = pager = (pages, page, click)->
z = $ tPager pages, page
x = ul z
x.find('a').click ->
x.replaceWith ul pager pages, n = Number(@title or $(@).text()), click
click? n, pages
false
z
ul = (pager)->
@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')