Skip to content

Instantly share code, notes, and snippets.

@amite
amite / import.php
Created June 30, 2016 07:53 — forked from lukeholder/import.php
Basic example to import Products and their variants into Craft Commerce
<?php
namespace Craft;
// This file could be placed into your public_html folder and visited to import a cheese product.
$craft = require '../craft/app/bootstrap.php';
$craft->plugins->loadPlugins();
$newProduct = new Commerce_ProductModel();
@amite
amite / readline.js
Created January 27, 2017 06:17
Excerpt from readline source library
function Interface(input, output, completer, terminal) {
if (!(this instanceof Interface)) {
return new Interface(input, output, completer, terminal);
}
this._sawReturnAt = 0;
this.isCompletionEnabled = true;
this._sawKeyPress = false;
this._previousKey = null;
@amite
amite / App.jsx
Created March 21, 2017 07:36
Changing text color in react native
export default class App extends React.Component {
render() {
return (
<View style={styles.container}>
<Text>Open up App.js to start working on your app!</Text>
<Text style={styles.bigblue}>Changes you make reload.</Text>
<Text>Shake your phone to open the developer menu.</Text>
</View>
);
}
@amite
amite / webpack.config.js
Created April 3, 2017 03:45
simple webpack config that loads jQuery
const webpack = require('webpack');
const path = require('path');
module.exports = {
entry: './app/src/index.js',
output: {
path: './app/build',
filename: 'bundle.js',
publicPath: '/'
},
@amite
amite / processMovieDetailsResponse.js
Created April 3, 2017 04:25
refactoring a JSON response
function processMovieDetailsResponse(movie) {
const movieDetailTemplate = `
<div class="movie-detail" data-movie-id="${movie.id}">
<p><strong>${movie.original_title}</strong></p>
<img src="https://image.tmdb.org/t/p/w185${movie.poster_path}" />
<p>
<em>Genres:</em>
<ul>
${displayGenres(movie.id, movie.genres)}
</ul>
@amite
amite / combos.js
Created April 20, 2017 15:14
Faker Factories
let faker = require('faker')
const getRandom = (arr) => arr[Math.floor(Math.random()*arr.length)]
const createCombos = (foodNames, foodTypes) => foodNames.map( (item, index) => `${getRandom(foodTypes)} ${item}`)
const foodNames = ['Samosa', 'Sandwich', 'Roll', 'Pattice']
const foodTypes = ['Non Veg', 'Veg', 'Jain']
function Combo() {
return {
@amite
amite / Names.js
Created April 22, 2017 09:16
Names statistics solution
```
var names = ['Alice', 'Bob', 'Tiff', "Sonny", 'Bruce', 'Alice', "Sonny"];
//output shoule be { 'Alice': 2, 'Bob': 1, 'Tiff': 1, 'Bruce': 1 }
var initialValue = {};
var reducer = (names, stats) => {
(!names[stats]) ? names[stats] = 1
: names[stats] = names[stats] + 1;
@amite
amite / actions.test.js
Created April 25, 2017 05:39
testing action creators in redux
import expect from 'expect';
import * as courseActions from './courseActions';
import * as types from './actionTypes';
import thunk from 'redux-thunk';
import nock from 'nock';
import configureMockStore from 'redux-mock-store';
// Test a sync action
describe('Course Actions', () => {
@amite
amite / functional-setstate.js
Last active September 18, 2017 09:14
Immutable Updates to nested data with setState in React
// Functional setState (Recommended)
this.setState(prevState => {
const newState = {
...prevState.data,
transactions: [...data, prevState.data.transactions]
}
return { data: newState }
})
@amite
amite / handleChange.js
Last active September 29, 2017 03:42
Curry Examples
const handleChange = (fieldName) => (event) => {
this.setState(fieldName, event.target.value)
}
<input type="text" onChange={handleChange('email')} ... />