Skip to content

Instantly share code, notes, and snippets.

@gobwas
gobwas / Combine.js
Last active December 22, 2015 00:19
Combine strings algorithm.
/**
* Combine algorithm.
*
* @author Sergey Kamardin <gobwas@gmail.com>
*
* Usage:
*
* combine(['a','b'])
*
* Will return ['ab', 'ba'];
@gobwas
gobwas / is.js
Created September 30, 2013 09:50
"is" function for smacss.
/**
* My Module.
*
* @package
* @author Sergey Kamardin <s.kamardin@tcsbank.ru>
*/
define(
[
],
function() {
@gobwas
gobwas / inherits.js
Last active February 11, 2024 23:05
Inheritance function. Inspired by backbone and goog.
(function() { "use strict";
var global = this;
/**
* Each iterator.
*
* @param {object} props
* @param {function} func
* @param {object} [context]
@gobwas
gobwas / combinations.coffee
Last active January 4, 2016 02:29
Returns combinations of given values.
combine = (list, index = 0, combination = [], result = []) ->
if (list[index])
for value in list[index]
combination[index] = value;
combine(list, index + 1, combination, result);
else
result.push(combination[..]);
return result;
function unflatten(path, obj, value) {
var key, child, result;
if (Object.prototype.toString.call(path) == '[object Object]') {
result = {};
for (key in path) {
if (path.hasOwnProperty(key)) {
unflatten(key.split('.'), result, path[key]);
}
}
@gobwas
gobwas / regexp.escaper.js
Created February 5, 2014 10:24
RegExp escaper
var escaper = function(value) {
assert(_.isString(value), "Can escape only the strings");
return value.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}
@gobwas
gobwas / random.hex.js
Created February 5, 2014 10:26
Random hex number
function randomHex() {
return (((1<<24) * Math.random())|0).toString(16);
}
@gobwas
gobwas / assert.js
Created May 29, 2014 09:11
assert.js
(function(global) { "use strict";
var each = function(obj, iterator, context) {
if (obj == null) return;
if (Array.prototype.forEach && obj.forEach === Array.prototype.forEach) {
obj.forEach(iterator, context);
} else if (isArray(obj)) {
for (var i = 0, l = obj.length; i < l; i++) {
if (iterator.call(context, obj[i], i, obj) === {}) return;
}
{
flatten: function(obj) {
function flatten(obj) {
return _.reduce(obj, function(memo, value, key) {
if (_.isObject(value)) {
_.forEach(flatten(value), function(v, k) {
memo[key + "." + k] = v;
});
} else {
memo[key] = value;
@gobwas
gobwas / server.js
Created September 2, 2015 17:02
File receiver
var http = require("http");
var fs = require("fs");
var stat = fs.statSync("./index.html");
var server = http.createServer(function(req, res) {
switch(req.url) {
case "/": {
res.writeHead(200, {
'content-type': 'text/html',