Skip to content

Instantly share code, notes, and snippets.

View nicoespeon's full-sized avatar

Nicolas Carlo nicoespeon

View GitHub Profile
@nicoespeon
nicoespeon / introducing-point-free-style.js
Last active April 11, 2016 21:29
Blog - Achieving point-free JavaScript - introducing point-free style programming
// From https://github.com/MostlyAdequate/mostly-adequate-guide/blob/master/ch5.md#pointfree
// Not point-free because we mention the data: name
let initials = (name) => name.split(' ').map(compose(toUpperCase, head)).join('. ');
// Point-free style
let initials = compose(
join('. '),
map(compose(toUpperCase, head)),
split(' ')
@nicoespeon
nicoespeon / behavior-test.js
Created March 23, 2016 23:53
Blog - Testing Marionette.js Behaviors - behavior test factory with context
function addOnClickTests ( context ) {
let model, view, behavior, options;
beforeEach( () => {
model = new context.ModelClass();
view = new context.ViewClass( { model: model } );
// Retrieve instantiated behavior and its actual options under this context.
behavior = _.findWhere( view._behaviors, { id: "addOnClick" } );
@nicoespeon
nicoespeon / view-test.js
Created March 23, 2016 23:52
Blog - Testing Marionette.js Behaviors - use behavior test factory
describe( "Like View", () => {
const View = LikeView.extend( { template: _.template( "" ) } );
describe( "AddOnClick Behavior", () => {
addOnClickTests( { ViewClass: View, ModelClass: LikeModel } );
} );
@nicoespeon
nicoespeon / behavior-test.js
Created March 23, 2016 23:51
Blog - Testing Marionette.js Behaviors - behavior test factory
function addOnClickTests ( context ) {
let model, view;
beforeEach( () => {
model = new context.ModelClass();
view = new context.ViewClass( { model: model } );
} );
it( "should increase the model size by 1 when we click on the view", () => {
@nicoespeon
nicoespeon / test.js
Created March 23, 2016 23:50
Blog - Testing Marionette.js Behaviors - mock view test
describe( "Alert Behavior", () => {
let view;
beforeEach( () => {
view = Marionette.ItemView.extend( {
template: _.template( "" ),
behaviors: {
@nicoespeon
nicoespeon / view.js
Created March 23, 2016 23:49
Blog - Testing Marionette.js Behaviors - instantiated behavior
const ShareView = Marionette.ItemView.extend( {
template: "#card",
behaviors: {
AlertOnShare: {
behaviorClass: AlertBehavior,
title: "Shared",
message: "Your message has been shared!"
}
@nicoespeon
nicoespeon / plopfile.js
Last active March 23, 2016 23:48
Blog - Plop — a micro-generator to ease your daily life - setGenerator
module.exports = ( plop ) => {
// We declare a new generator called "module"
plop.setGenerator( "module", {
// Succintly describes what generator does.
description: "Create a new module",
// Get inputs from the user.
// That's Inquirer.js doing the job behind the hood.
@nicoespeon
nicoespeon / plopfile.js
Last active March 23, 2016 23:48
Blog - Plop — a micro-generator to ease your daily life - prompts
import {trimRight, isEmpty} from "lodash";
const ensurePlural = ( text ) => trimRight( text, "s" ) + "s";
const isNotEmptyFor = ( name ) => {
return ( value ) => {
if ( isEmpty( value ) ) return name + " is required";
return true;
}
}
@nicoespeon
nicoespeon / plopfile.js
Last active March 23, 2016 23:47
Blog - Plop — a micro-generator to ease your daily life - concrete example
const modulePath = "app/modules/{{camelCase name}}.js";
module.exports = ( plop ) => {
plop.setGenerator( "model", {
// …
actions: [
// Add a new model + tests boilerplate.
@nicoespeon
nicoespeon / model.js
Last active March 23, 2016 23:47
Blog - Plop — a micro-generator to ease your daily life - plop-template
/**
* TODO - Describe what your model does.
*
* @class {{pascalCase name}}.Model
* @module {{pascalCase name}}
* @constructor
*/
import {Model} from "backbone";
export default Model.extend( {