Skip to content

Instantly share code, notes, and snippets.

View adammagana's full-sized avatar
🏖️
Working on Marco Polo :)

Adam Magaña adammagana

🏖️
Working on Marco Polo :)
View GitHub Profile
@adammagana
adammagana / UpdatePreferences.kt
Created March 19, 2021 16:01
An Android SharedPreferences function extension that simplifies the edit-and-apply flow
import android.content.SharedPreferences
/**
* Simple extension to remove the need to call `SharedPreferences.edit()` and `SharedPreference.Editor.apply()` for
* every preferences change.
*/
fun SharedPreferences.update(updateBlock: SharedPreferences.Editor.() -> Unit) {
val editor = edit()
updateBlock(editor)
@adammagana
adammagana / UpdateConstraints.kt
Created October 19, 2020 13:40
Helper method for updating constraints on a ConstraintLayout with Transitions.
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.ConstraintSet
import androidx.transition.Transition
import androidx.transition.TransitionManager
/**
* Runs the given `updateBlock` on the current `ConstraintSet` and applies them back to the
* [ConstraintLayout].
*
* @param animated Whether or not to run [TransitionManager.beginDelayedTransition] on the next
@adammagana
adammagana / Logging.swift
Last active September 5, 2023 20:29
A convenient Swift protocol and wrapper for OSLog.
import os
private let subsystem = "<ENTER-YOUR-SUBSYSTEM-STRING-HERE>"
// MARK: - Protocol
protocol LogProducer {
func debug(_ message: StaticString, _ messageArguments: CVarArg...)
func info(_ message: StaticString, _ messageArguments: CVarArg...)
func warn(_ message: StaticString, _ messageArguments: CVarArg...)
@adammagana
adammagana / Logger.kt
Created August 15, 2020 21:06
A means to preserve logging on Android without breaking simple unit tests.
import android.util.Log
import kotlin.reflect.KClass
/**
* An interface meant to trivialize the mocking of Android's [Log] methods.
*/
interface Logger {
fun v(message: String)
fun d(message: String)
@adammagana
adammagana / ResourceProvider.kt
Last active August 16, 2020 19:51
An Android "Resource Provider" pattern that I often use. Peep the Javadoc comments for details.
import android.content.Context
import android.graphics.drawable.Drawable
import androidx.annotation.ColorInt
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.core.content.ContextCompat
import java.lang.ref.WeakReference
/**
@adammagana
adammagana / SkuDetails.PricePerMonth.kt
Last active March 15, 2021 22:21
An Android SkuDetails property extension that calculates price-per-month.
import com.android.billingclient.api.SkuDetails
import java.lang.Exception
import java.text.NumberFormat
import java.util.*
/**
* Currency code for the U.S. dollar
*/
private const val USD = "USD"
(function () {
'use strict';
var colorList = [
'#da1f2d',
'#00a550',
'#00acdc',
'#f37720',
'#fbaf17',
'#5c5da9',

Question:

What is the difference between a function declaration function myVar() {} and a function expression var myVar = function() {};?

Answer:

In order to properly understand the (subtle) differences between a function declaration and a function expression we'll need to dive into the inner workings of JavaScript a little bit.

Consider this:

console.log(myVar); // ReferenceError: myVar is not defined
@adammagana
adammagana / parse_url.js
Created August 13, 2012 20:45
Parse all URLs into anchor tags in a string of text.
function parseURL(raw_text) {
var urlRegex = /[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&~\?\/.=]+/g,
parsed_text = '';
parsed_text = raw_text.replace(urlRegex, function(url) {
return url.link(url);
});
return parsed_text;
}
@adammagana
adammagana / parse-tweets.js
Created March 19, 2012 17:57
Replaces Twitter username, hashtag, and link text with links.
function parseTweet(tweet) {
var urlRegex = /[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&~\?\/.=]+/g,
userRegex = /[@]+[A-Za-z0-9-_]+/g,
hashRegex = /[#]+[A-Za-z0-9-_]+/g;
tweet = tweet.replace(urlRegex, function(url) {
return url.link(url);
});
tweet = tweet.replace(userRegex, function(username) {