Skip to content

Instantly share code, notes, and snippets.

View kucherenko's full-sized avatar
Focusing

Andrey Kucherenko kucherenko

Focusing
View GitHub Profile
// Compose functions from right to left
const compose = (...fns) => x => fns.reduceRight((y, f) => f(y), x);
// Example
const lowercase = str => str.toLowerCase();
const capitalize = str => `${str.charAt(0).toUpperCase()}${str.slice(1)}`;
const reverse = str => str.split('').reverse().join('');
const fn = compose(reverse, capitalize, lowercase);
const curry = (fn, ...args) => {
return fn.length <= args.length ? fn(...args) : curry.bind(null, fn, ...args);
}
// Example
const sum = (a, b, c) => a + b + c;
curry(sum)(3)(4)(5); // 12
curry(sum)(3, 4, 5); // 12
const slugify = string => string.toLowerCase().replace(/\s+/g, '-').replace(/[^\w-]+/g, '');
const kebabToCamel = str => str.replace(/-./g, m => m.toUpperCase()[1]);
const camelToKebab = str => str.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase();
const toPascalCase = str => (str.match(/[a-zA-Z0-9]+/g) || [])
.map(w => `${w.charAt(0).toUpperCase()}${w.slice(1)}`).join('');
// Examples
// generate random string
const generateString =
(length, chars) => Array(length)
.fill('')
.map((v) => chars[Math.floor(Math.random() * chars.length)])
.join('');
// Example
generateString(10, '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
@kucherenko
kucherenko / bootstrap.js
Created February 12, 2014 15:01
Testing enviropment for sinon and jquery ajax
beforeEach(function() {
env = sinon.sandbox.create();
//stub all requests to server
$ajax = env.stub($, 'ajax');
});
afterEach(function() {
env.restore();
});
@kucherenko
kucherenko / mocha.html
Created August 8, 2013 07:08
Mocha in browser
<html>
<head>
<meta charset="utf-8">
<title>Mocha Tests</title>
<link rel="stylesheet" href="lib/mocha/mocha.css" />
<script src="lib/coffee-script.js"></script>
</head>
<body>
<div id="mocha"></div>
<script src="lib/mocha/mocha.js"></script>
@kucherenko
kucherenko / package.json
Created August 8, 2013 05:28
package.json with sinon support
{
"name": "coffee-examples",
"version": "0.0.1",
"scripts": {
"test": "./node_modules/.bin/mocha --compilers coffee:coffee-script"
},
"dependencies": {
"coffee-script": "*",
"mocha": "*",
"chai": "*",
@kucherenko
kucherenko / coffee.html
Created August 8, 2013 05:13
HTML file with CoffeeScript support
<!DOCTYPE html>
<html>
<head>
<title>CoffeeScript Examples</title>
<script src="http://coffeescript.org/extras/coffee-script.js" type="text/javascript"></script>
<script type="text/coffeescript">
class A
constructor: ->
alert "Hello CoffeeScript"
a = new A
@kucherenko
kucherenko / index.html
Last active December 20, 2015 17:49
installation test enviropment for coffescipt
{
"name": "coffee-examples",
"version": "0.0.1",
"scripts": {
"test": "./node_modules/.bin/mocha --compilers coffee:coffee-script"
},
"dependencies": {
"coffee-script": "*",
"mocha": "*",
"chai": "*"