Skip to content

Instantly share code, notes, and snippets.

View webuniverseio's full-sized avatar

Sergey Zarovskiy webuniverseio

View GitHub Profile
@webuniverseio
webuniverseio / handlebars-nodejs-extension.js
Created May 7, 2016 14:44
Node.js extension for handlebars templates. Useful for headless unit testing / server side rendering of handlebars templates / build process.
require.extensions['.hbs'] = function(module, filename) {
function compileHandlebars(filename, input) {
const handlebars = require('handlebars/lib/index');
const fs = require('fs');
return handlebars.compile(fs.readFileSync(filename, {encoding: 'utf8'}))(input);
}
return module._compile(
`module.exports = (input) => ${compileHandlebars.toString()}('${filename.replace(/\\/g, '/')}', input)`,
filename
);

Contributing

When contributing to this repository, please first discuss the change you wish to make via issue, email, or any other method with the owners of this repository before making a change.

Please note we have a code of conduct, please follow it in all your interactions with the project.

Pull Request Process

  1. Ensure any install or build dependencies are removed before the end of the layer when doing a
@webuniverseio
webuniverseio / Functors.js
Created December 20, 2016 15:54
Functors created by szarouski - https://repl.it/EtEk/1
const Functor = x =>
({
fold: f => f(x),
map: f => Functor(f(x))
});
const Left = (x) =>
({
chain: f => Left(x),
fold: (f, g) => f(x),
@webuniverseio
webuniverseio / combine-data.js
Created January 21, 2017 14:51
Data comprehension/generation that I thought I can use for unit tests. Unfortunately due to lack of constraints you get very high number of variants.
const util = require('util');
const {List} = require('immutable-ext');
const _ = require('lodash');
const logAndReturn = x => (console.log('log:', x),x);
var objKeysToApplicative = obj =>
[Reflect.ownKeys(obj)]
.map(keys =>
keys.join(' => ').concat(' => ').concat(`({${keys.join(',')}})`))
.map(eval)[0];
const generateData = (obj) =>
@webuniverseio
webuniverseio / DEBUG-JS.md
Last active January 26, 2017 20:29
Tiny wrapper for debug.js with no dependencies to make it write to stdout/stderr by default.

Tiny wrapper for debug.js with no dependencies to make it write to stdout & stderr by default. Usage example:

const {log, debug} = require('./debug')('app');
app.once('start', () => log('App started'));
db.on('error', debug.bind(null, 'connection error:'));

If you run program like that and redirect stdout/stderr it will actually work

@webuniverseio
webuniverseio / EXPRESS-JSX.md
Created January 24, 2017 00:02
Boilerplate for adding another folder to express-react-views engine.

With express-react-views adding another folder to jsx engine in such a way that files are not cached in development mode is crazy at the moment :(. If you're on windows it might be even worse because of path differences... So enjoy :)

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jsx');
const additionalJSXFolders = [path.join(__dirname, 'static/src/js')];
@webuniverseio
webuniverseio / app.js
Created January 24, 2017 13:17 — forked from sogko/app.js
gulp + expressjs + nodemon + browser-sync
'use strict';
// simple express server
var express = require('express');
var app = express();
var router = express.Router();
app.use(express.static('public'));
app.get('/', function(req, res) {
res.sendfile('./public/index.html');
@webuniverseio
webuniverseio / README.md
Last active March 19, 2017 03:19
Cache node-sass on windows
  • npm i
  • npm shrinkwrap --dev
  • npm run shrinkpack
  • copy files from node_modules/node-sass/vendor/** to root/node-sass-cache/**
  • create a .bat file which has something like SASS_BINARY_PATH=%cd%\node_sass_bin_cache\win32-x64-48\binding.node && npm i
  • use this file for installation instead of npm i
  • to verify run this bat without internet connection
@webuniverseio
webuniverseio / composeReducers.js
Created April 2, 2017 02:18
composeReducers.js
const composeReducers = (...reducers) =>
(state = {}, action) => {
const {type} = action;
const result =
[...reducers]
.map(x => () => type in x.types ? x.reducer(state, action): false)
.map(x => x())
.filter(x => x);
if (result.length > 1) {throw new Error(`Action type '${type}' must be unique.`);}
@webuniverseio
webuniverseio / forceUpdate.js
Created April 2, 2017 16:01
react-redux forceUpdate
componentDidMount() {
let prevState = this.props.getState();
this.unsubscribe =
this.props.subscribe(() => {
const state = this.props.getState();
if (state != prevState) {
this.forceUpdate();
prevState = state;
}
});