Skip to content

Instantly share code, notes, and snippets.

View hubertcross's full-sized avatar

Hubert Cross hubertcross

View GitHub Profile
@hubertcross
hubertcross / json_decoding_city.js
Last active October 23, 2018 02:49
Playground JSON Initializer test
// Testing City model with JSON initializer against my NodeJS/Postgres API
// Hubert Cross 2018
import UIKit
import PlaygroundSupport
print("Playground RUNNING")
struct City : Codable {
let name: String
@hubertcross
hubertcross / doGet.swift
Created October 20, 2018 19:16
How to avoid this error? Trying to implement a JSON initializer
struct Todo : Codable {
let title: String
let userid: Int
let id: Int
let completed: Bool
}
enum SerializationError: Error {
case missing(String)
case invalid(String, Any)
@hubertcross
hubertcross / code
Created October 19, 2018 07:07
how to use JSONs in swift4? how to access 'rows' in response as data, and not just text?
// parse the result as JSON, since that's what the API provides
do {
guard let returnObject = try JSONSerialization.jsonObject(with: returnData, options: [])
as? [String: Any] else {
print("error trying to convert data to JSON")
return
}
// now we have the return object (it's a dictionary)
@hubertcross
hubertcross / City.swift
Last active December 28, 2020 02:07
City Codable class is sent in HTTP POST Request. Good example of generic type constraints in WebRestCommunication.makeHTTPPostRequest()
import Foundation
class City: NSObject, Codable {
var name: String = ""
var countrycode: String = ""
var district: String = ""
var population: Int = 0
init(name: String, countrycode: String, district: String, population: Int) {
self.name = name
@hubertcross
hubertcross / hubert-iterative.js
Last active October 5, 2018 18:51
sum branches of a tree iteratively
/*
https://jsbin.com/yakaxuwugo/edit?js,console
You have a tree represented by an array.
The 0th element of the array is the top node of the tree
The 1st element is the left child, the 2nd is the right
The 3rd and 4th element are the children of the 1st element
The 5th and 6th elements are the children of the 2nd element, and so on.
Find out which branch of the top node has the the child nodes with the greatest sum
@hubertcross
hubertcross / Pagination attempt
Created October 4, 2018 04:18
Attempting to write my own pagination component
const PageItem = props => {
const liClassName = (props.activePage === props.pageName) ? "page-item active" : "page-item";
return (
<li className={liClassName}><a className="page-link" href="#">{props.pageName}</a></li>
);
}
class Pagination extends React.Component {
constructor(props) {
@hubertcross
hubertcross / histogram_canvas
Last active September 25, 2018 10:54
solution to a typical interview question, take random data and make a frequency histogram. done with canvas2d js API
<!DOCTYPE html>
<html>
<head>
<title>canvas tinkering</title>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
@hubertcross
hubertcross / cities.js
Created July 31, 2018 02:35
Just one connection pooling method for pg
const pool = require('../services/pgdatabase');
module.exports.get = function get(req, res, next) {
console.log("Cities GET service");
console.log("wtf1");
return pool().query('SELECT * FROM city LIMIT 10;')
.then(function(res) {
console.log("debug1");
console.log(res.rows)
@hubertcross
hubertcross / gist:481dcda7e563ad2fa3e26eacef9cd5c4
Created July 11, 2018 02:30
How to do async for each loop!!
async function asyncForEach(array, callback) {
const results = [];
for (let index = 0; index < array.length; index +=
results.push(callback(array[index], index, array)
}
await Promise.all(results);
}
@hubertcross
hubertcross / SAVNavbar.js
Created July 8, 2018 00:44
NavItem href not working
import React from 'react';
import Navbar from 'react-bootstrap/lib/Navbar';
import Nav from 'react-bootstrap/lib/Nav';
import NavItem from 'react-bootstrap/lib/NavItem';
import NavDropdown from 'react-bootstrap/lib/NavDropdown';
import MenuItem from 'react-bootstrap/lib/MenuItem';
import { Link } from 'react-router-dom';