Skip to content

Instantly share code, notes, and snippets.

View kirilloid's full-sized avatar

Kirill Sukhomlin kirilloid

View GitHub Profile
@kirilloid
kirilloid / languages-stats.txt
Created January 15, 2019 11:42
languages stats
el-GR: 9705
fr-FR: 9684
it-IT: 9394
de-DE: 9391
nl-NL: 9174
es-CL: 9076
ro-RO: 9073
ms-MY: 8998
id-ID: 8993
hu-HU: 8983
@kirilloid
kirilloid / cloneViaPostMessage.js
Last active January 7, 2018 04:25
deep clone test suite
var handlers = {};
self.addEventListener('message', function (event) {
if (event.origin !== location.origin) return;
var key = event.data.key;
var value = event.data.value;
if (!handlers[key]) return;
handlers[key](value);
delete handlers[key];
});
import java.util.Locale;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class HelloWorld
{
public static void main(String[] args)
{
Locale hiDflt = Locale.forLanguageTag("hi-IN");
function pascalize(string) {
return string.replace(/-([a-z])/g, (_, c) => c.toUpperCase());
}
function normalize(value) {
if (/^(null|true|false)$/.test(value)) {
return JSON.parse(value);
}
return value;
}
@kirilloid
kirilloid / assign.js
Created October 6, 2016 16:24
immutable
function assingImmutable(obj, toAdd) {
if (!_.isObjectLike(toAdd)) {
return toAdd;
}
key = _.first(path);
if (_.isArray(obj)) {
copy = _.slice(obj);
toAdd.forEach(function (value, idx) {
copy[idx] = assingImmutable(obj[idx], value);
});
@kirilloid
kirilloid / type.js
Last active July 29, 2022 19:42
getType
function getType (value) {
let type = typeof value;
if (type === 'object') {
return value ? Object.prototype.toString.call(value).slice(8, -1) : 'null';
}
return type;
}
[NaN, 0, 1, Infinity, // numbers
null, undefined, false, 'str', // other primitives
@kirilloid
kirilloid / walk-tree.js
Created March 31, 2016 20:15
Cormen 10.4.5
/**
* @param {Node} node
* @template T
*/
function Node (key, left, right) {
/** @property {T} key */
this.key = key;
if (left) left.parent = this;
if (right) right.parent = this;
/** @property {Node<T>} left */
@kirilloid
kirilloid / factory.js
Created March 18, 2016 16:52
es6 patterns
class Shape {
constructor (type, ...args) {
super();
switch (type) {
case 'rect':
return new Rectangle(...args);
case 'circle':
return new Circle(...args);
}
}
@kirilloid
kirilloid / main.js
Created April 21, 2015 17:34
lz77-alike
(function decompress (s) {
var i, c, out = "", len, offset;
for (i = 0; i < s.length; i++) {
c = s.charCodeAt(i);
if (c >= 256 && c <= 288) {
len = c - 256;
offset = s.charCodeAt(++i);
out += out.substr(-offset, len);
} else {
out += s.charAt(i);
@kirilloid
kirilloid / main.js
Created April 21, 2015 17:33
Randnomizer
(function (n, m) {
if (typeof m === 'undefined') { m = 1; }
if (typeof n === 'undefined') { throw new TypeError("Provide at least first number"); }
function randSort () {
return Math.random() * 2 - 1;
}
function getArr(n) {
var a = [];
for (var i = 1; i <= n; i++) { a.push(i); }