Skip to content

Instantly share code, notes, and snippets.

@genee19
genee19 / nma.coffee
Last active June 13, 2016 09:07
Non-mutating actors for Array and Object in CoffeeScript / JavaScript (like immutable, but not completely, results are still usual Array or Object that you can mutate)
NMA =
Array:
push: (array, items...) ->
array.concat(items)
set: (array, index, item) ->
array[...index].concat([item]).concat(array[index+1...])
del: (array, index) ->
(item for item, i in array when i isnt index)
Object:
@genee19
genee19 / Inline_helpers.html.haml
Last active April 26, 2016 07:46
"Inline" named helpers in HAML. You don't always need to **ck with .rb helpers or partials.
- # _hamlout is the secret sauce
- def test text, _hamlout
%h1 You gave me this:
-# the usual HAML structures can live here
%pre=text
- # remember I told of _hamlout? Kind of annoying, yeah, I know. But anyway.
- test "ohhai", _hamlout
- # notice that we don't =test, we call it as -test, or else you will see an unwanted 0
@genee19
genee19 / html_whitelisting.js
Last active August 29, 2015 14:16
Lightweight client side HTML whitelisting in JavaScript. By no means this is a strong security measure, I use it to simply clean up the (ugly) code from many WYSIWYGs out there.
var whiteListHTML = function(content, whiteList) {
var sanitizeInPlace = function(DOMElement, keepTop) {
var allowed, item, i;
for (i = DOMElement.children.length-1; i >= 0 ; i--) {
item = DOMElement.children[i];
sanitizeInPlace(item, false);
}
allowed = keepTop || whiteList[DOMElement.localName];
@genee19
genee19 / amphiling.js
Created February 20, 2015 11:25
A file that is a valid CSS and JS at the same time. Include the same URL into HTML via <link> and <script src> and spare a request.
// {} /*
/*/
body {
color: red;
}
body:before {
content: "This is CSS";
}
/*/
//
@genee19
genee19 / egrul_inn_check.rb
Created February 18, 2015 17:58
Проверка валидности ИНН юрлица на Ruby (согласно http://www.egrul.ru/test_inn.html)
def valid_le_tax_id? (tax_id)
tax_id = tax_id.to_s.split('')
weights = [2,4,10,3,5,9,4,6,8,0]
sum = tax_id.each_with_index.reduce(0){|result,item|
result += item[0].to_i * weights[item[1]]
}%11%10
return sum == tax_id[9].to_i
end
product = {
id: 10,
quantity: 3,
options: {
someTextOption: "optionVal",
someDateOption: new Date().getTime().toString()
},
callback: function(success, product, cart) {
// ...
}
var productId = 10;
Ecwid.Cart.addProduct(productId);
Ecwid.Cart.addProduct(productID, callback)
Ecwid.Cart.addProduct(product)
Ecwid.Cart.calculateTotal(function(order) {
if (!order)
alert('An error occurred!')
else
alert('Order total: ' + order.total);
});