Skip to content

Instantly share code, notes, and snippets.

Avatar

Taher nullhook

  • Los Angeles
View GitHub Profile
@nullhook
nullhook / chars.cc
Last active Jun 9, 2021
char type conversions in c++
View chars.cc
#include <iostream>
#include <locale>
#include <string>
#include <fstream>
#include <codecvt>
// utf8/utf16/utf32 can be directly written to file without conversions
// sizeof(T) gives you bytes of the type
// .size() .length() gives count of chars
// if char16_t is stored the open the file with utf16 encoding
@nullhook
nullhook / rotate2dmatrix.js
Created Dec 28, 2020
How to rotate this two-dimensional array?
View rotate2dmatrix.js
class Matrix extends Array {
transpose() {
if (this.length === 1) return this;
const getCol = (n) => this.map((v) => v[n]);
const totalCols = this[0].length;
const cols = [];
for (let i=0; i<totalCols; i++) {
cols.push(getCol(i));
}
return cols;
@nullhook
nullhook / .js
Last active Jun 30, 2020
tiny helper to create DOM elements
View .js
const $T = text => document.createTextNode(text)
function $E(tag, props, kids) {
const elem = document.createElement(tag)
for (const k in props) {
elem[k] = props[k]
}
for (const kid of kids) {
elem.appendChild(kid)
}
@nullhook
nullhook / .js
Last active Mar 1, 2020
idea: match props in styled-component
View .js
const Foo = styled.div`
display: block;
opacity: 0;
color: ${by('color')};
${eq('red')`
background-color: red;
`};
${isEqOr('left', 'right')`
@nullhook
nullhook / .jsx
Created Dec 15, 2019
media queries with styled components
View .jsx
// ****************************************/
// Media Queries
// ****************************************/
// -----------------------------------------
import { css } from 'styled-components';
// Media breakpoints
const desktopXL = 1680;
const desktop = 1280;
@nullhook
nullhook / A.jsx
Last active Nov 1, 2019
Advance React Patterns
View A.jsx
// A component with accessors
// The accessors can only be accessed inside the actual component
function C(props) {
// ...
}
C.bar = () => {
// this method can be accessed within the component
}
View media-queries.sass
// ****************************************/
// Media Queries
// ****************************************/
// Media breakpoints
$mq-desktop-xl: 1680px
$mq-desktop: 1382px
$mq-desktop-small: 1280px
$mq-tablet-landscape: 1024px
$mq-tablet-portrait: 768px
@nullhook
nullhook / selector.js
Last active Mar 20, 2019
a small utility function for adding styles to the DOM
View selector.js
function select(elem){
this.element = document.getElementById(elem);
this.getStyles = function() {
return this.element.style;
}
this.addStyles = function(obj) {
const keys = Object.keys(obj);
View wordStats.js
function wordCountStats(str) {
const arr = str
.split(' ')
.map(word => word.replace(/\'|"|,|\./g, '' ).toLowerCase());
return arr.reduce((acc,word) => {
if(!acc.hasOwnProperty(word)) {
acc[word] = 0;
}
View createLoader.js
function waitFor(delay) {
return new Promise(resolve => {
setTimeout(resolve, delay);
});
}
function createLoader(delay, loader, error) {
return () => {
return waitFor(delay).then(() => {
if (loader) {