Skip to content

Instantly share code, notes, and snippets.

@ivenmarquardt
ivenmarquardt / todo-app.js
Last active May 14, 2022 03:36
Functional Reactive Programming (FRP) implemented with a couple rules, types and combinators
// Based on this blog post: https://medium.com/@iquardt/taming-the-dom-50c8f1a6e892
/* RULES
* Only the program may manipulate the output display, never the user
* User input is presented in the form of events
* GUI elements generate events only in response to user input, never in response to program output
*/
@WebReflection
WebReflection / i18n.js
Created October 22, 2017 15:58
i18n in 10 lines of code
function i18n(template) {
for (var
info = i18n.db[i18n.locale][template.join('\x01')],
out = [info.t[0]],
i = 1, length = info.t.length; i < length; i++
) out[i] = arguments[1 + info.v[i - 1]] + info.t[i];
return out.join('');
}
i18n.locale = 'en';
i18n.db = {};
@SherryH
SherryH / jestMock1.js
Last active July 25, 2018 19:07
A Jest Mock Example
import React from 'react';
import renderer from 'react-test-renderer';
//children mock need to be defined before parent module import
jest.mock('react-bootstrap-table', () => {
return {
BootstrapTable: 'BootstrapTable',
TableHeaderColumn: 'TableHeaderColumn',
};
});
@WebReflection
WebReflection / state.js
Created May 13, 2016 10:16
Prototypal inheritance used to define states.
/*! (c) 2016 Andrea Giammarchi - MIT Style License */
// simple state-like objects handler
// based on prototypal inheritance
function State() {'use strict';}
// States are serializable dictionaries
// toJSON and toString are the only reserved keywords
// every other name can be used as name (included __proto__)
@cjohansen
cjohansen / reactorama.js
Created May 27, 2015 10:07
createComponent is 18 lines of JavaScript that puts a lean and enjoyable interface on top of React's somewhat clunky and non-JSX-hostile API.
// Most components are defined fully by their render function,
// and all they need to access is the props
var myComponent = createComponent(function (props) {
return React.DOM.h1({}, "Hello " + props.name);
});
// ...which can be done very succinctly with ES6:
const {h1, div} = React.DOM;
const myComponent = createComponent(({name}) => h1({}, `Hello ${name}`));
var path = require('path');
var fs = require('fs');
var cheerio = require('cheerio');
var createTemplate = function(id, markup) {
var $ = cheerio.load('<script type="text/ng-template"></script>');
$('script').attr('id', id).html(markup).html();
return $.html();
};
@WebReflection
WebReflection / html-escape.md
Last active August 21, 2022 16:27
How to escape and unescape from a language to another

update

I've created a little repository that simply exposes the final utility as npm module.

It's called html-escaper


there is basically one rule only: do not ever replace one char after another if you are transforming a string into another.

@calvinmetcalf
calvinmetcalf / es5-readable.js
Created January 13, 2015 15:35
whatwg readable streams translated to es5
var Promise = global.Promise || require('lie');
function noop(){}
function typeIsObject(x) {
return (typeof x === 'object' && x !== null) || typeof x === 'function';
}
function ExclusiveStreamReader () {
throw new Error('not implimented');
}
function ReadableStream(opts) {
var start = opts.start || noop;
@sstur
sstur / dom-to-json.js
Last active October 8, 2023 04:17
Stringify DOM nodes using JSON (and revive again)
function toJSON(node) {
let propFix = { for: 'htmlFor', class: 'className' };
let specialGetters = {
style: (node) => node.style.cssText,
};
let attrDefaultValues = { style: '' };
let obj = {
nodeType: node.nodeType,
};
if (node.tagName) {
@jamonholmgren
jamonholmgren / user_controller.rb
Created October 23, 2013 19:06
Cleaner params
class UserController < ApplicationController
def create
@user = User.create(UserInput.new(params).create)
end
def update
@user = User.find(params[:id].to_i)
@user.update_attributes(UserInput.new(params).update)
end