Skip to content

Instantly share code, notes, and snippets.

@jmporchet
jmporchet / index.js
Created February 15, 2021 10:12
Object iterator with a generator
const obj = {
sub: {
array: [1,2,3,4,5,6,7,8,9]
},
*[Symbol.iterator]() {
for (let i = 0; i < this.sub.length; i++) {
yield this.sub[i];
}
}
}
@jmporchet
jmporchet / index.js
Last active February 15, 2021 10:06
Object iterator property
const obj = {
sub: {
array: []
},
[Symbol.iterator]() {
let index = 0;
return {
next: () => {
// you could insert custom logic here, like stepping 3 by 3, filtering, etc.
if ( index >= this.sub.array.length ) return { done: true }
@jmporchet
jmporchet / index.html
Last active January 22, 2021 21:55
How to hide HTML elements if there's not enough space to display them integrally
<html>
<body>
<style type="text/css">
.container {
width: 300px;
height: 200px;
display: flex;
/* put the undesirable elements on a second column */
flex-flow: column wrap;
/* hide that second column */
@jmporchet
jmporchet / Component.stories.tsx
Created January 15, 2021 14:47
Add storybook addon-redux to an existing project with redux-sagas and connected-react-router
import React from 'react';
import { Story, Meta } from '@storybook/react/types-6-0';
import { Provider } from 'react-redux';
import addons from '@storybook/addons';
import withRedux from 'addon-redux/withRedux';
import { MyComponent } from '../components/MyComponent/MyComponent';
import store from '../store';
import { RootState } from '../types';
# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH
# Path to your oh-my-zsh installation.
export ZSH="/Users/jean-marieporchet/.oh-my-zsh"
# Set name of the theme to load --- if set to "random", it will
# load a random theme each time oh-my-zsh is loaded, in which case,
# to know which specific one was loaded, run: echo $RANDOM_THEME
# See https://github.com/robbyrussell/oh-my-zsh/wiki/Themes
@jmporchet
jmporchet / foodSaga.js
Created April 17, 2019 08:37
The correct way to make several network requests in one saga with yield
import { takeEvery, call, put, all, select } from 'redux-saga/effects';
import * as ActionTypes from '../actions';
import { networkService } from '../services/networkService';
export function* getAutocompleteInfo(action) {
const token = yield select(state => state.user.token);
const autocompleteSuggestions = yield call(
networkService.requestAutocompleteInfo,
action.query,
token
@jmporchet
jmporchet / cloneTest.js
Last active April 4, 2019 13:50
Spread operator vs lodash's cloneDeep()
// This is an illustration of when NOT to use the spread operator to copy variables.
// Check https://codesandbox.io/s/6w749171mz for a working copy of the code
import { cloneDeep } from "lodash";
import {
getDrinkNutrientsFromResult,
mapRecipesToNutrients
} from "../nutrition";
describe("helper methods", () => {
@jmporchet
jmporchet / EditPlanDetails.test.js
Created April 1, 2019 16:57
How to test componentDidUpdate with react-native-testing-library
import React from 'react';
import { render, fireEvent } from 'react-native-testing-library';
import { _EditPlanDetails as EditPlanDetails } from '../Settings/EditPlanDetails';
// Scrollviews are bugged in the current Expo/RN release and won't render in tests https://github.com/expo/expo/issues/2806#issuecomment-465373231
jest.mock('ScrollView', () => require.requireMock('ScrollViewMock'));
describe('EditPlanDetails', () => {
const props = {
subscription: {
import { AsyncStorage } from 'react-native';
class AsyncStorageService {
static async get(key) {
const data = await AsyncStorage.getItem(key);
return JSON.parse(data);
}
static set(key, value) {
return AsyncStorage.setItem(key, JSON.stringify(value));
@jmporchet
jmporchet / cloudSettings
Last active November 21, 2019 22:04
Visual Studio Code Settings Sync Gist
{"lastUpload":"2019-11-21T22:04:21.419Z","extensionVersion":"v3.4.3"}