Skip to content

Instantly share code, notes, and snippets.

@rimmer333
rimmer333 / nma.coffee
Last active Jun 13, 2016
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)
View nma.coffee
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:
@rimmer333
rimmer333 / Inline_helpers.html.haml
Last active Apr 26, 2016
"Inline" named helpers in HAML. You don't always need to **ck with .rb helpers or partials.
View Inline_helpers.html.haml
- # _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
@rimmer333
rimmer333 / html_whitelisting.js
Last active Aug 29, 2015
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.
View html_whitelisting.js
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];
@rimmer333
rimmer333 / amphiling.js
Created Feb 20, 2015
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.
View amphiling.js
// {} /*
/*/
body {
color: red;
}
body:before {
content: "This is CSS";
}
/*/
//
@rimmer333
rimmer333 / egrul_inn_check.rb
Created Feb 18, 2015
Проверка валидности ИНН юрлица на Ruby (согласно http://www.egrul.ru/test_inn.html)
View egrul_inn_check.rb
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
View new_gist_file
product = {
id: 10,
quantity: 3,
options: {
someTextOption: "optionVal",
someDateOption: new Date().getTime().toString()
},
callback: function(success, product, cart) {
// ...
}
View new_gist_file
var productId = 10;
Ecwid.Cart.addProduct(productId);
View new_gist_file
Ecwid.Cart.addProduct(productID, callback)
View gist:6497167
Ecwid.Cart.calculateTotal(function(order) {
if (!order)
alert('An error occurred!')
else
alert('Order total: ' + order.total);
});