Skip to content

Instantly share code, notes, and snippets.

View oleksmarkh's full-sized avatar

Oleksii oleksmarkh

View GitHub Profile
@oleksmarkh
oleksmarkh / jquery-just-html.js
Last active December 20, 2015 07:39
a simple jquery plugin that wraps styled tags with specified tags
(function ($){
$.fn.wrapInHtml = function (options) {
var tags = $.extend({i: {css: 'font-style',
val: 'italic'},
b: {css: 'font-weight',
val: 'bold'},
u: {css: 'text-decoration',
val: 'underline'}},
options);
@oleksmarkh
oleksmarkh / main.js
Last active August 26, 2016 21:02
reaching "gBrowser" object from Firefox add-on main code (e.g. to handle URL change)
import { Cc, Ci } from 'chrome';
const mediator = Cc['@mozilla.org/appshell/window-mediator;1'];
const gBrowser = mediator.getService(Ci.nsIWindowMediator).getMostRecentWindow('navigator:browser').gBrowser;
function onLocationChange(aProgress, aRequest, aURI) {
console.log(aURI.spec);
};
gBrowser.addProgressListener({ onLocationChange }, Ci.nsIWebProgress.NOTIFY_LOCATION);
def indexof(fromstr, substr):
f = s = 0
while f < len(fromstr):
if fromstr[f] == substr[s]:
if s == len(substr) - 1:
return f - s
s += 1
else:
// <div id="block"></div>
// #block {
// position: relative;
// left: 200px;
// width: 40px;
// height: 40px;
// background-color: #456;
// }
@oleksmarkh
oleksmarkh / api.js
Last active August 15, 2020 09:21
transport layer (abstraction over "axios")
import axios from 'axios';
import { mapKeys, mapValues, camelCase, snakeCase } from 'lodash';
const { API_URL } = process.env;
function getAccessToken() {
// @todo: load access token from cookie
return 'token';
}
@oleksmarkh
oleksmarkh / api.md
Last active December 6, 2016 20:16
typical API "request -> ... -> response" flow
router          <- method + URL
authentication  <- middleware
controller      <- function/class
  request         <- object
  command         <- pattern
    validation      <- rules
    CRUD/search     <- models
    authorization   <- throwing exception
 quota &lt;- filter
@oleksmarkh
oleksmarkh / user.js
Created January 11, 2017 20:16
generic access control approach (user + resource [+ action])
import { has } from 'lodash';
// "user" assumed to always represent currently authenticated user
// once this assumption isn't sufficient, the politic has to be extended
// with something like:
// * isCurrentUser(user, currentUser)
// * belongsToCurrentUser(user, currentUser, resource)
// a "resource" should at least have the "type" attribute for matching purposes
// "user" is one possible detalization of a "resource" (e.g. 'const user = {type: "admin", profileId: 1, email: "[email protected]", ...}'),
@oleksmarkh
oleksmarkh / aggregate.py
Created February 9, 2017 13:59
groups values applying an aggregation function
table = [
{'age': 32, 'gender': 'm', 'country': 'Germany', 'transactions': 4233},
{'age': 23, 'gender': 'f', 'country': 'US', 'transactions': 11223},
{'age': 31, 'gender': 'f', 'country': 'France', 'transactions': 3234},
{'age': 41, 'gender': 'm', 'country': 'France', 'transactions': 2230},
{'age': 19, 'gender': 'm', 'country': 'Germany', 'transactions': 42},
{'age': 21, 'gender': 'f', 'country': 'France', 'transactions': 3315},
{'age': 23, 'gender': 'm', 'country': 'Italy', 'transactions': 520}
]