Skip to content

Instantly share code, notes, and snippets.

View C-Rodg's full-sized avatar
👽

Curtis C-Rodg

👽
View GitHub Profile
@C-Rodg
C-Rodg / Basics.dart
Created May 20, 2020 19:34
Dart Basics
// This guide encompasses the basics of the Dart programming language.
// It is heavily based off of the Dart guides listed below:
// Library Tour:
// https://dart.dev/guides/libraries/library-tour
// Language Tour:
// https://dart.dev/guides/language/language-tour
// To further explore any of the code, just head to the site:
@C-Rodg
C-Rodg / buttonConstraint.swift
Created May 16, 2018 23:43
A sample of programmatically assign constraints to a UIButton in Swift.
import UIKit
class ViewController: UIViewController {
struct LayoutProperties {
static let sideSpacing: CGFloat = 20
static let cornerRadius: CGFloat = 8
static let buttonHeight: CGFloat = 60
}
@C-Rodg
C-Rodg / Shuffle.js
Last active May 14, 2018 23:29
The Fisher-Yates (Knuth) Shuffle implemented in Javascript.
const shuffle = (arr) => {
let currentIndex = arr.length;
let tempValue, randomIndex;
while (0 !== currentIndex) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
tempValue = arr[currentIndex];
arr[currentIndex] = arr[randomIndex];
arr[randomIndex] = tempValue;
@C-Rodg
C-Rodg / DFSgenerators.js
Created March 25, 2018 06:00
Depth-first searching of a tree using generators.
class Tree {
constructor(value = null, children = []) {
this.value = value;
this.children = children;
}
*printValues() {
yield this.value;
for(let child of this.children) {
yield* child.printValues;
@C-Rodg
C-Rodg / StringToBytesToBase64.js
Created November 1, 2017 23:03
Convert a string to a byte array and then encode with Base64. Useful for putting complex data structures in XML.
const convertToBytes = (str) => {
return new TextEncoder('utf-8').encode(str);
};
const dataString = 'TT12345,1:49:52 PM,10/15/13';
// ODQsODQsNDksNTAsNTEsNTIsNTMsNDQsNDksNTgsNTIsNTcsNTgsNTMsNTAsMzIsODAsNzcsNDQsNDksNDgsNDcsNDksNTMsNDcsNDksNTE=
btoa(convertToBytes(dataString));
@C-Rodg
C-Rodg / ColorHexToRGB.js
Created October 31, 2017 22:24
A quick script using a bitwise operator to convert a color hex string into RGB.
const convertHexToRGB = (hex) => {
hex = hex[0] === '#' ? hex.substr(1) : hex;
const rgb = parseInt(hex, 16);
return {
Red: (rgb >> 16) & 0xFF,
Green: (rgb >> 8) & 0xFF,
Blue: rgb & 0xFF
};
};
@C-Rodg
C-Rodg / compareObjectOrArray.js
Last active October 17, 2017 15:58
A function to fully compare objects and arrays.
const isEqual = (value, other) => {
// Tests - same object type, same length, same items
const type = Object.prototype.toString.call(value);
if (type !== Object.prototype.toString.call(other)) {
return false;
}
if (['[object Array]', '[object Object]'].indexOf(type) < 0) {
return false;
@C-Rodg
C-Rodg / doOnLoad.js
Created October 13, 2017 21:24
A quick snippet using Javascript's requestAnimationFrame to determine when the document is ready or a library is loaded.
const ready = () => {
// Example of checking if library is available
if ('jQuery' in window) {
return;
}
// Example of checking if document is loaded
if (document.body) {
// Do code here...
@C-Rodg
C-Rodg / AsyncSyncPatterns.js
Created October 10, 2017 22:54
Example of doing a typically async action as synchronous with reduce() or async/await.
const itemIds = [1,2,3,4,5,6];
// Using reduce
itemIds.reduce((promise, id) => {
return promise.then(_ => api.deleteItem(id));
}, Promise.resolve());
// Using Async/Await
itemIds.forEach(async (item) => {
await api.deleteItem(item);
@C-Rodg
C-Rodg / MicroLibrary.js
Last active October 9, 2017 17:46
A small micro-library that shows how libraries like jQuery are made.
const get = (selector, context) => {
// Select the items to manipulate
const GetNodes = function() {
this.nodes = context ? context.querySelectorAll(selector) : document.querySelectorAll(selector);
};
// Add new class to nodes
GetNodes.prototype.addClass = function(className) {
for (let i = 0, j = this.nodes.length; i < j; i++) {
this.nodes[i].classList.add(className);