Skip to content

Instantly share code, notes, and snippets.

View Kosmin's full-sized avatar

Cosmin Atanasiu Kosmin

View GitHub Profile
@Kosmin
Kosmin / pureComponentWithImmutable.jsx
Last active March 7, 2017 23:29
React Pure Component with Immutable Props Example
import React, { PropTypes, PureComponent } from 'react';
class Car extends PureComponent {
render() {
return (
<div>
{`Your object: ${details.name} ${details.color}`}
</div>
);
}
@Kosmin
Kosmin / regularComponentWithPureRenderImmutableExample.jsx
Last active March 7, 2017 23:27
Regular Component with Pure Render and Immutable.JS
import React, { PropTypes, Component } from 'react';
class Car extends Component {
shouldComponentUpdate(nextProps) {
return this.props.details === nextProps.details;
}
render() {
return (
<div>
@Kosmin
Kosmin / regularComponentWithPureRenderExample.jsx
Created March 7, 2017 23:24
React Regular Component With Pure Render Example
import React, { PropTypes, Component } from 'react';
class Car extends Component {
shouldComponentUpdate(nextProps) {
return this.props.details.name === nextProps.details.name &&
this.props.details.color === nextProps.details.color;
}
render() {
@Kosmin
Kosmin / reactRegularComponentExample.jsx
Created March 7, 2017 23:15
React Regular Component Example
import React, { PropTypes, Component } from 'react';
class Car extends Component {
render() {
return (
<div>
{`Your object: ${details.name} ${details.color}`}
</div>
);
}
@Kosmin
Kosmin / reactContainerExample.jsx
Last active March 7, 2017 22:55
React Container Example
import React, { Component }, from 'react';
class Container extends Component {
render() {
console.log('rendering Container');
return (
<div>
<Car details={this.props.firstCarDetails} />
<Car details={this.props.secondCarDetails} />
</div>
@Kosmin
Kosmin / pureFunction.jsx
Last active March 7, 2017 22:35
React Pure Function Component Example
import React from 'react';
const Car = ({details}) => {
console.log('rendering Car component');
return (
<div>
{`Your object: ${details.name} ${details.color}`}
</div>
);
}
// This comes in handy whenever we want to uniquely identify JSON objects on the client side
// - works with ImmutableJS objects or ES6 Maps
export default (object) => {
if (object.get) {
return `${object.get('type')}_${object.get('id')}`;
}
return `${object.type}_${object.id}`;
};
@Kosmin
Kosmin / promisable.js
Last active October 7, 2016 21:55
Wrap any function in a promise to use `.then` regardless of whether that function returns a promise
// Simple interface used to execute a callback in different ways depending on
// whether or not a function returns a promise or not.
// Usage:
// import Promisable from 'promisable';
//
// Promisable(someFunction()).then(() => {
// promiseWasSuccessful()
// }, promiseFailed());
//
@Kosmin
Kosmin / promise_queuer.js.coffee
Last active September 15, 2015 19:33
Angular Promise Queuer
module = angular.module 'helpers.promise_queuer'
# Lets us ensure that some promises don't execute if others are in progress
# within a given context
#
# Notes:
# This also helps us decouple the queuing logic from our promises.
###
Architecture Notes:
@Kosmin
Kosmin / deep_compare.coffee
Last active August 29, 2015 14:23
Simple Javascript deep object comparison (shows diff)
# Shows in an understandable format what the diff is between two hashes.
# I've only really used this for debugging purposes
deepCompare = (source, destination, stack = []) ->
# This relies on angular, but this line can be replaced with return if source == destination
return if angular.equals(source, destination)
# Object and not array
if typeof source == 'object' && source.constructor != Array
# invalid destination or different types
if !destination? || (typeof destination != 'object' || destination.constructor == Array)