Skip to content

Instantly share code, notes, and snippets.

View baptistemanson's full-sized avatar
🌼
Blooming

Baptiste Manson baptistemanson

🌼
Blooming
View GitHub Profile
@baptistemanson
baptistemanson / gist:b2e1dd24022b493511203a217a215c39
Last active July 9, 2020 17:45
How many collision can I test in JS per frame.js
function isInside(pos, rect) {
return (
pos[0] > rect[0] && pos[1] > rect[1] && pos[0] < rect[2] && pos[1] < rect[2]
);
}
const rect1 = new Uint16Array([10, 10, 100, 1000]);
const rect2 = new Uint16Array([0, 0, 10, 10]);
const pos1 = new Uint16Array([250, 50]);
const pos2 = new Uint16Array([20, 20]);
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'})