Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
Rolling my katamari ball

Christopher Pedersen topherPedersen

💭
Rolling my katamari ball
View GitHub Profile
@topherPedersen
topherPedersen / JSONFailGracefully.ts
Created Sep 23, 2021
TypeScript JSON.parse undefined
View JSONFailGracefully.ts
const json = '{"foo": { "bar": { "baz": {"a": 1, "b": 2, "c": 3}}}}';
const parsedJSON = JSON.parse(json);
/*
* DANGER! This line will run, but is not fail-safe, could crash if foo, bar,
* baz or a is missing or undefined
*/
console.log(parsedJSON.foo.bar.baz.a);
@topherPedersen
topherPedersen / mobile_friendly_flask_app.py
Created Jun 16, 2021
How to Serve Up Mobile and Desktop Versions of Your Website in Python with Flask
View mobile_friendly_flask_app.py
from flask import Flask, Response, render_template, request
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def index():
user_agent = request.headers.get('User-Agent')
user_agent = user_agent.lower()
@topherPedersen
topherPedersen / nestenum.ts
Created Jun 7, 2021
Nested "Enums" in TypeScript
View nestenum.ts
enum Foo {
A = "ActivityCode.Foo.A",
B = "ActivityCode.Foo.B",
C = "ActivityCode.Foo.C",
}
enum Bar {
A = "ActivityCode.Bar.A",
B = "ActivityCode.Bar.B",
C = "ActivityCode.Bar.C",
@topherPedersen
topherPedersen / MainActivity.java
Created Dec 16, 2020
Android Button OnClickListener
View MainActivity.java
package blog.topherpedersen.uselesscameraapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
View activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
@topherPedersen
topherPedersen / stripDuplicates.js
Created Nov 5, 2020
How to Strip Duplicate Items from a JavaScript Array
View stripDuplicates.js
// Duplicates: Bob, Aaron, Frank
const arrayWithDuplicates = ["Aaron", "Bob", "Chris", "Dave", "Edward", "Bob", "Aaron", "Frank", "George", "Frank", "Henry", "Bob", "Bob", "Aaron", "Aaron", "Frank"];
function stripDuplicates(arrayWithDuplicates) {
let uniques = [];
arrayWithDuplicates.forEach( (value, index) => {
const indexWhereValueFirstAppears = arrayWithDuplicates.findIndex( (value_) => {
return value_ === value;
});
if (index === indexWhereValueFirstAppears) {
@topherPedersen
topherPedersen / findDuplicates.js
Created Nov 5, 2020
How to Find Duplicate Items in a JavaScript Array
View findDuplicates.js
// Duplicates: Bob, Aaron, Frank
const arrayWithDuplicates = ["Aaron", "Bob", "Chris", "Dave", "Edward", "Bob", "Aaron", "Frank", "George", "Frank", "Henry", "Bob", "Bob", "Aaron", "Aaron", "Frank"];
function findDuplicates(arrayWithDuplicates) {
let duplicates = [];
arrayWithDuplicates.forEach( (value, index) => {
const indexWhereValueFirstAppears = arrayWithDuplicates.findIndex( (value_) => {
return value_ === value;
});
const indexWhereValueLastAppears = arrayWithDuplicates.lastIndexOf(value);
@topherPedersen
topherPedersen / unionTypes.ts
Created Nov 2, 2020
TypeScript Union Type Demo
View unionTypes.ts
// DEMO: Using Union-Types in TypeScript
// Question: What are Union Types???
// Answer: Union Types are TypeScript types which can be equal to many different types which often share 'union' properties amongst the various different types. For example, an Athlete Union-Type could be used to represent FootballPlayer, BasketballPlayer, and BaseballPlayer types
type FootballPlayer = {
firstName: string,
lastName: string,
jerseyNumber: number,
@topherPedersen
topherPedersen / App.tsx
Created Aug 31, 2020
React-Native TypeScript Class Component Template
View App.tsx
import React from 'react';
import {
SafeAreaView,
StyleSheet,
ScrollView,
View,
Text,
StatusBar,
} from 'react-native';
@topherPedersen
topherPedersen / HelloDjango.txt
Created Jul 25, 2020
Django Hello, World / Quickstart / Cheatsheet for Windows
View HelloDjango.txt
$ cd Desktop
$ mkdir HelloDjango
$ cd HelloDjango
// Create Virtual Environment with Python's venv
$ python -m venv env