Skip to content

Instantly share code, notes, and snippets.

Kevin Ndung'u kevgathuku

Block or report user

Report or block kevgathuku

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@kevgathuku
kevgathuku / App.js
Created Jun 25, 2019
Embedding Elm 0.19 into a React app using react-elm-components
View App.js
import React from 'react';
import logo from './logo.svg';
import './App.css';
// Add these two imports
import Elm from 'react-elm-components';
import Main from "./Main";
function App() {
return (
@kevgathuku
kevgathuku / Main.elm
Last active Jun 25, 2019
Elm 0.19 Buttons example
View Main.elm
-- Extracted from: https://guide.elm-lang.org
-- Read more about this program in the official Elm guide:
-- https://guide.elm-lang.org/architecture/buttons.html
module Main exposing (main)
import Browser
import Html exposing (Html, button, div, text)
import Html.Events exposing (onClick)
@kevgathuku
kevgathuku / devToolsDetect.js
Last active May 6, 2019
Detect if Chrome Devtools is open (Doesn't work on Firefox)
View devToolsDetect.js
// https://stackoverflow.com/a/30638226
// When printing an “Element” Chrome developer tools will request the 'id' property
// If the 'id' property is fetched, then the devtools is open
let checkStatus;
let customElement = document.createElement('p');
document.body.appendChild(customToString);
let element = new Image();
Object.defineProperty(element, 'id', {
View whyDidYouUpdate.js
componentDidUpdate(prevProps, prevState) {
Object.entries(this.props).forEach(([key, val]) =>
prevProps[key] !== val && console.log(`Prop '${key}' changed`)
);
Object.entries(this.state).forEach(([key, val]) =>
prevState[key] !== val && console.log(`State '${key}' changed`)
);
}
View collatz-pre.clj
(defn compute-collatz [num steps]
(cond
(= num 1) steps
(even? num) (compute-collatz (/ num 2) (inc steps))
(odd? num) (compute-collatz (+ 1 (* num 3)) (inc steps))))
(defn collatz [num]
{:pre [(pos? num)]}
(compute-collatz num 0))
View collatz-first.clj
(defn compute-collatz [num steps]
(cond
(= num 1) steps
(even? num) (compute-collatz (/ num 2) (inc steps))
(odd? num) (compute-collatz (+ 1 (* num 3)) (inc steps))))
(defn collatz [num]
(if
(pos? num) (compute-collatz num 0)
@kevgathuku
kevgathuku / EventComponent.spec.js
Created Nov 5, 2018 — forked from hartzis/EventComponent.spec.js
Touch Event Testing React Jest Enzyme
View EventComponent.spec.js
import React from 'react';
import EventComponent from './EventComponent';
import { mount } from 'enzyme';
import {
createStartTouchEventObject,
createMoveTouchEventObject
} from './EventHelpers.js';
describe('EventComponent', () => {
@kevgathuku
kevgathuku / Main.elm
Created Oct 1, 2018 — forked from sch/Main.elm
Minimum Elm boilerplate
View Main.elm
module Main exposing (main)
import Browser
import Html exposing (Html, button, div, text)
import Html.Events exposing (onClick)
type alias Model =
{ count : Int }
View closures.js
let arr = [];
for (var i = 0; i < 10; i++) {
arr.push(function() {console.log(i)});
}
let x = 0;
while (x < 10) {
arr[x]();
x++;
}
View jest.md

Mocking an imported function to return different values for different tests

In the top level:

import getVisibleDays from 'react-dates/lib/utils/getVisibleDays';

jest.mock('react-dates/lib/utils/getVisibleDays', () => {
  const moment = require('moment');
  return jest.fn().mockReturnValue({
    '2018-01': [
You can’t perform that action at this time.