Skip to content

Instantly share code, notes, and snippets.

View aofleejay's full-sized avatar
🐢
Keep going

aofleejay

🐢
Keep going
View GitHub Profile
@aofleejay
aofleejay / index.js
Created July 3, 2017 09:49
Lambda function to resize image from S3
var AWS = require('aws-sdk')
var gm = require('gm').subClass({ imageMagick: true })
var s3 = new AWS.S3()
exports.handler = function(event, context, callback) {
var prefix = 'resized-'
var srcBucket = event.Records[0].s3.bucket.name
var dstBucket = prefix + srcBucket
var srcKey = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, " "))
var dstKey = prefix + srcKey
@aofleejay
aofleejay / App.test.js
Created July 3, 2017 13:41
React Snapshot Testing
import React from 'react'
import renderer from 'react-test-renderer'
import shallowRenderer from 'react-test-renderer/shallow'
import App from './App'
describe('Test app component', () => {
it('shallow snapshot', () => {
const renderer = new shallowRenderer
const snapshot = renderer.render(<App />)
expect(snapshot).toMatchSnapshot()
@aofleejay
aofleejay / CommentList.js
Last active May 30, 2018 17:46
<CommentList />
import React, { Component } from 'react'
import Loading from './Loading'
class CommentList extends Component {
render() {
const { isLoading, comments } = this.props
if (isLoading) return <Loading />
return (
<ul>
@aofleejay
aofleejay / CommentList.js
Last active May 30, 2018 17:50
<CommentList /> with HOC
import React, { Component } from 'react'
import withLoading from '../hocs/withLoading'
class CommentList extends Component {
render() {
return (
<ul>
{
this.props.comments.map(({ id, body }) => <li key={id}>{body}</li>)
}
@aofleejay
aofleejay / withLoading.js
Last active May 30, 2018 17:49
withLoading HOC
import React, { Component } from 'react'
import Loading from '../components/Loading'
const withLoading = (WrappedComponent) => {
return class ComponentWithLoading extends Component {
render() {
const { isLoading } = this.props
if (isLoading) return <Loading />
return <WrappedComponent {...this.props} />
@aofleejay
aofleejay / server.js
Created July 29, 2017 08:01
hello world for express
const express = require('express')
const app = express()
app.get('/', (req, res) => {
res.send('Hello World')
})
app.listen(3000, () => {
console.log('Start server at port 3000.')
})
@aofleejay
aofleejay / db.json
Created July 29, 2017 08:09
Fake database for simple express project
[
{
"id": "1",
"name": "Game of thrones"
},
{
"id": "2",
"name": "Clash of kings"
}
]
@aofleejay
aofleejay / server.js
Created July 29, 2017 08:16
Import db.json and create route /books with method get
const books = require('./db')
app.get('/books', (req, res) => {
res.json(books)
})
@aofleejay
aofleejay / server.js
Created July 29, 2017 08:18
Import db.json and create route /books with method get
const books = require('./db')
app.get('/books', (req, res) => {
res.json(books)
})
@aofleejay
aofleejay / server.js
Created July 29, 2017 08:30
Get book with specific id
app.get('/books/:id', (req, res) => {
res.json(books.find(book => book.id === req.params.id))
})