Skip to content

Instantly share code, notes, and snippets.

View kottenator's full-sized avatar

Rostyslav Bryzgunov kottenator

  • Facebook
  • Seattle, WA
View GitHub Profile
@kottenator
kottenator / simple-pagination.js
Created July 13, 2015 20:44
Simple pagination algorithm
// Implementation in ES6
function pagination(c, m) {
var current = c,
last = m,
delta = 2,
left = current - delta,
right = current + delta + 1,
range = [],
rangeWithDots = [],
l;
@kottenator
kottenator / djrf-error-parser.js
Last active April 15, 2022 13:49
JS error parser for django-rest-framework
/**
* Specific error parser for django-rest-framework errors structure
* Tested on django-rest-framework v2.3.13
* Live example at http://jsbin.com/wiqumo
*
* Error structure examples:
*
* // The most simple case
* { detail: "Authentication is required" }
*
@kottenator
kottenator / multiple_select_field.py
Last active June 24, 2020 01:52
Django's multiple-choice model field with static choices
"""
Field with multiple *static* choices (not via m2m)
Value is stored in DB as comma-separated values
Default widget is forms.CheckboxSelectMultiple
Python value: list of values
Original Django snippet: https://djangosnippets.org/snippets/1200/
It's 6 years old and doesn't work with latest Django
Also it implements 'max_choices' functionality - I have removed it for simplicity
@kottenator
kottenator / js_observer.js
Last active August 12, 2019 15:17
JavaScript Simple Observer
/**
* Simple Observer pattern mixin (custom event dispatching & listening)
* Doesn't requires any 3rd-party JS libs. It's pure JS
*
* Usage:
*
* var MyObject = {...}; // your custom object
* Observer.apply(MyObject); // you also can apply Observer to a prototype
* MyObject.on('inited', function() { console.log("I'm alive!"); });
* MyObject.trigger('inited');
@kottenator
kottenator / select2-flat.css
Created April 23, 2014 10:49
Select2 styles for Flat UI
/*
* Select2 v3.4.6 styles customization for Flat UI
*/
/*----------------------------------------------- Main select element ------------------------------------------------*/
.select2-container .select2-choice {
height: 41px; /* Jobsy form controls have 37px total height */
border: 2px solid #bdc3c7;
border-radius: 6px;
outline: none;
font: 15px/38px "Lato", Liberation Sans, Arial, sans-serif;
@kottenator
kottenator / jQuery plugin with browserify-shim.md
Last active May 21, 2019 18:17
jQuery plugin with browserify-shim

Goal

Demonstrate how to configure browserify with browserify-shim to exclude jQuery from the bundle and to make jQuery plugin (that supports CommonJS modules) work correctly, using global window.jQuery object.

Step 1: create files

index.html:

<script src="node_modules/jquery/dist/jquery.js"></script>
@kottenator
kottenator / diff.js
Created April 15, 2018 19:54
Own custom diff
/*
Specific conditions:
- input: `oldString` and `newString` (e.g. `'old same'` and `'same new'`)
- output format: `'(old )same[ new]'`
- return diff of min length, not counting `()[]`
- return diff which is min among all diff variants by lexicographical order, with one remark: consider `\w` < `()` < `[]`
Taken from CodeFights: Dropbox bot challange.
*/
@kottenator
kottenator / depth-first graph traversal.js
Last active March 4, 2018 17:53
Depth-first graph traversal (DFS)
/**
* General implementation of depth-first graph traversal (OK to have loops),
* no recursion.
*/
function depthFirstTraverse(connections, i=0) {
let visited = {};
let traverseStack = [[-1, i]]; // edge from A to B vertex
let revisit = false;
while (traverseStack.length) {
@kottenator
kottenator / breadth-first traversal.js
Created March 4, 2018 17:53
Breadth-first graph traversal (BFS)
function breadthFirstTraverse(connections, start = 0) {
let stack = [[start, 0]]; // keep node index & current depth
let visited = new Set;
while (stack.length) {
let newStack = [];
log('Current stack:', stack);
for (let [i, depth] of stack) {
log(`Visit ${i} (depth: ${depth})`);
@kottenator
kottenator / decodeString solution.js
Last active March 4, 2018 16:53
CodeFights: decodeString - O(n) solution
/**
* Solution for this - https://codefights.com/interview-practice/task/dYCH8sdnxGf5aGkez
*/
function decodeString(s) {
return parse(s)[0];
}
function parse(string, startIdx=0) {
let i = startIdx;
let res = '';