Skip to content

Instantly share code, notes, and snippets.

View CarlMungazi's full-sized avatar
🕵️‍♂️
Digging into source code...

Carl Mungazi CarlMungazi

🕵️‍♂️
Digging into source code...
View GitHub Profile
@CarlMungazi
CarlMungazi / reverse.js
Last active July 31, 2017 16:12
Scraping and reversing headlines with node.js
//require modules
var request = require('request');
var cheerio = require('cheerio');
var fs = require('fs');
//prepare to reverse those strings
function reverseString(str) {
return str
.split('') //split string into an array
.reverse() // reverse the array values
@CarlMungazi
CarlMungazi / string-reverse.md
Last active November 3, 2018 21:22
Many Ways To Split A String

Task: Reverse the provided string in as many different ways as possible.

Tests: All the solutions should pass the following tests:

  • "hello" should return a string.
  • "hello" should become "olleh".
  • "Howdy" should become "ydwoH".
  • "Greetings from Earth" should return "htraE morf sgniteerG"

Code:

1. All you need is one line of code

@CarlMungazi
CarlMungazi / palindrome.md
Last active November 24, 2016 20:05
FCC Bonfire - Check for Palindromes

Task: Write a function which returns true if the given string is a palindrome. Otherwise, return false.

Pseudocode:

  • Get the string value as a parameter
    • If the user has entered a string
      • Remove non-alphanumeric characters and make the string lowercase
      • Reverse the string
      • If the string is a palindrome
        • return the boolean value true
@CarlMungazi
CarlMungazi / factorialize.md
Last active July 23, 2017 22:41
Factorialize a Number

Task: Return the factorial of the provided integer.

Pseudocode:

  • Get the integer value (n) as a parameter
    • If the the number is equal to 0
      • return 1
    • return the value of n multiplied by result of n - 1

Actual Code:

@CarlMungazi
CarlMungazi / source-code-links.md
Last active August 1, 2017 21:16
Blogs/articles about reading source code
@CarlMungazi
CarlMungazi / mithril-hyperscript.md
Last active May 16, 2018 07:54
Understanding mithril's hyperscript function

Earlier this year at work we re-wrote an internal framework we used to create SPA e-learning courses. After briefly trying out React, Angular 2, Ember and Vue, we settled on mithril (https://mithril.js.org). If I were to compare it to the frameworks we tried out, I would say it's more like React but with a simpler, smaller codebase. By the way, if you like geeking out on code articles, the articles from mithril's old site have some real nuggets of gold (http://lhorie.github.io/mithril-blog/).

A few months after the re-write was done, I dug into mithril's codebase to gain a deeper understanding and this is what I found...

The main entry point into mithril's source code is the m() function, which is a hyperscript function that, according to the docs (https://mithril.js.org/hyperscript.html), represents an element in a mithril view. It's demonstrated below as:

m("div", {id: "box"}, "hello")
// equivalent HTML:
// <div id="box">hello</div>
@CarlMungazi
CarlMungazi / mithril-recommends-1.md
Last active July 26, 2017 17:01
Recommendations from mithril's docs

Before reading a library or framework's source code, it's advisable to go through the documentation. Thankfully, mithril's size means reading through the main APIs is not that arduous a task. When reading the docs, you will often find notes from the authors detailing recommended ways of using the available features. This is useful when you read the source because you can more easily understand why certain bits of code exist. I've noted below five recommendations I came across during my latest round of reading the mithril docs. I found more than five but I'll go through the rest at a later time. The example code below each recommendation has been taken from the docs.

  1. Use CSS selectors for attributes with unchanging values and pass an attributes object for attributes with values that can change
m("div#hello") // static

m("a.link[href=/]", { 
	class: currentURL === "/" ? "selected" : ""
}, "Home") // dynamic
@CarlMungazi
CarlMungazi / mithril-vnodes.md
Last active August 4, 2017 02:19
Deep dive into mithril vnodes

In my first explainer of Mithril's source code, I broke down how the hyperscript function worked. It's main task is the creation of virtual DOM nodes (vnodes), the objects which represent DOM elements or parts of the DOM. So how are these objects created?

Vnode functions

This is the function which creates a vnode:

function Vnode(tag, key, attrs, children, text, dom) {
	return {tag: tag, key: key, attrs: attrs, children: children, text: text, dom: dom, domSize: undefined, state: undefined, _state: undefined, events: undefined, instance: undefined, skip: false}
}

This function is assisted in its work by these two helper functions:

@CarlMungazi
CarlMungazi / design-guidelines.md
Last active April 1, 2019 13:32
Design guidelines

Typography

  • Use font sizes between 15-25px for body text
  • Use bigger font sizes for headlines. Decrease font-weight as font-size increases.
  • Use 120-150% of font-size for line-height
  • Allow 45-90 characters per line. Increase font-size and/or whitespace or use a multi-column to help with this
  • Use sans-serif fonts for UI elements and most other things. Use serif fonts for content and long text sections
  • Do not put text on images you do not control
  • Text shadows can improve legibility
  • 4:5:1 contrast ratio for text below 24px and 3:1 for text above 24px
  • Do not mix text effects which add emphasis. For example, do not underline and italicise text which is also bold
@CarlMungazi
CarlMungazi / react-dom-render.js
Created December 21, 2018 17:58
React Dom Render
ReactDOM.render(
<h1>Hello, world!</h1>,
document.getElementById('root'),
() => {}
);