Skip to content

Instantly share code, notes, and snippets.

View baptistemanson's full-sized avatar
🌼
Blooming

Baptiste Manson baptistemanson

🌼
Blooming
View GitHub Profile
async function scrapeProduct (url) {
// the same code as you have ...
return {srcText, price, title};
}
async function main() {
const results = await scrapeProduct("...");
// here you can do something with results = {srcText, price, title};
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://shone.com/anorak.json",
"title": "Anorak",
"description": "Portable Realtime Maritime Navigational Data Protocol",
"oneOf": [
{ "$ref": "#subscribeMessage" },
{ "$ref": "#unsubscribeMessage" },
{ "$ref": "#getMessage" },
{ "$ref": "#deltaMessage" },
@baptistemanson
baptistemanson / night-day.jsx
Created July 17, 2018 01:27
changing component with time
// css is
// .day {color: black; background-color:white}
// .night {color: white; background-color:blue}
const DivChanging = ({ text }) => {
if (new Date().getHours() > 12) {
return <div class="night">{text}</div>;
} else {
return <div class="day">{text}</div>;
}
import React, { Component } from "react";
import logo from "./logo.svg";
import "./App.css";
const Child = ({ onClick }) => <button onClick={onClick}>Click me</button>;
class Parent extends Component {
field = "hello";
handleClick = e => {
@baptistemanson
baptistemanson / parallel.js
Created June 29, 2018 07:16
Node can use several threads, even if you only write JS
const fs = require("fs");
const util = require("util");
const readFile = util.promisify(fs.readFile);
async function long() {
const content = await readFile("./dummy1000000.csv", "utf-8");
content.split(/\n/);
}
const arr = [];
const getGroupsById = (state, ids) => ids.map(id => state.groups[id])
const getUsersWithGroupsById = (state, ids) => ids.map(id => ({...state.users[id], groups: getGroupsById(state.users[id].groups)}))
// 1 - instead of
const state = {
users: [
{ id: 1, name: "Bat", groups: [{ name: "admin" }, { name: "regular" }] },
{ id: 2, name: "Vince", groups: { name: "admin" } },
{ id: 1, name: "Romain", groups: { name: "normal" } }
]
};
// 2- Normalize and index by primary key
// 1 - instead of
const mapDispatchToProps = dispatch => {
return {
addUser: payload => dispatch({type:'my-app/users/ADD',payload})
}
}
const connect(null, mapDispatchToProps)(MyComponent)
// 2 - with action creators
import {addUser} from './redux/users'
// 1 - instead of
const action = {type: 'my-app/users/ADD', {id: 1, name:'Bat'}}
/* we can */
// 2 - we define an action creator in the users file
export const addUser = payload => ({type: 'my-app/users/ADD', payload})
// we can get the action in another file when we need it
import {addUser} from '../redux/users'
const action = addUser({id: 1, name:'Bat'})
import reducer, { * as actionCreators } from './users'
describe('actions', () => {
it('adds a user', () => {
const user = {id:1, name:'Bat'}
const action = actionCreators.addUser(user)
expect(action).toEqual({type:'my-app/users/ADD', payload:user })
expect(reducer(undefined, action)).toMatchSnapshot('adds a user')
})