Skip to content

Instantly share code, notes, and snippets.

View asleepace's full-sized avatar
🚀

Colin asleepace

🚀
View GitHub Profile
@asleepace
asleepace / try.ts
Last active September 3, 2024 20:37
A simple extension for TypeScript which enables the `.try(args)` method on functions. This works for both normal & async functions, and reduces a lot of boilerplate.
// Try.ts
// By Colin Teahan
// August 14th, 2024
//
// This TypeScript file enables usage of the .try() method which will return a result tuple of [T?, Error?]
// from any function or async function. This works by defining a new property on the global Object.prototype
// and then do some type-fu.
//
// Example usage:
// https://www.typescriptlang.org/play/?target=99#code/MYewdgziA2CmB0w4EMBOAKAlAKGwejwAJCAVVAT3gBcJ8jCAhcwgYRgEsxTZkALZMHWIBBAK4BzURCqEAjABYqvADSEATAAY18ukNK92EUuQAOsAMrBU7EzIBm7OIVhhkAIzhGpycbEIg7QiU-agosQgBbWCUQABNCAHcDYF5Ex2hCVGjRVC5kTNgIUWgZKlETJwDCAG0SAH5VAFFUVBBUOoBdPTtWiMIBZjtRMGAqdnB-VH6IchHCIZGx8Hh9Q0S2gGsjN2ZY2AcwTnF+wjBYBMITVrNUKmYJ4MJxaBA3ZAyAeTcAK1hR+CuICoQNMsD0AniwS4sRAhCgUSCoIAtEN4LoCMRGgAPZARCp+by+ABc6PoC1G4y4vioAFUILBUEwAJKxdDsWJE06iCJuBmYQgAbz0xGI7ECbPiAB4ALyEDT8pStC5nC7NVoYADknAAbu92YQpAzCOyNTgMSLiFkyrlBadcbBORqGK8Nap9QBfPSegh6UCQGTVQ2oVQM9UdQiy6l0hnM2KhcjoeWESVIpHOFptNLQDK8grWs6xX3gaQ1IMhjOocOR6LRxnkFnx9CyfkptNBwhOl3GoxWnIF0nEAByHxIjU5JAMRjW0nSJwSm2NXEB4iyECMEO7pyBcNE
/**
* GenericSubClass
*
* This class extends a base class (in this case FooType) without having to manually
* define all of the base classes properties and methods. This is accomplished using
* a Proxy wrapper and a factory method.
*
* When creating a new instance do not call new GenericSubClass directly, but instead
* use the static factory method GenericSubClass.new({ ... }) which will return the
* GenericSubClass with a proxy wrapper.
@asleepace
asleepace / GenericSortedType.ts
Created January 22, 2023 09:35
Generic Sorted Type - TypeScript 4.9.4
/**
* Sorted - Utility Type
*
* Takes a tuple of unsorted numbers and returns a sorted tuple of the
* same values in ascending order. Can handle negative as well as
* duplicate numbers.
*
* This works by destructuring the list into [List[0], ...List.slice(1)]
* sorting the list slice recursively until the slice is only one element.
* Then as we return we insert List[0] at each level into a sorted list.
@asleepace
asleepace / index.html
Last active September 13, 2022 21:13
Soladex
<html>
<head>
<title>Soladex Corp.</title>
<description></description>
<script>
</script>
</head>
@asleepace
asleepace / index.html
Created September 12, 2022 13:36
Soladex
<html>
<head>
<title>Soladex Corp.</title>
<script>
function getAddress(latitude, longitude) {
return new Promise(function(resolve, reject) {
var request = new XMLHttpRequest();
var method = 'GET';
var url = 'https://maps.googleapis.com/maps/api/geocode/json?latlng=' + latitude + ',' + longitude + '&sensor=true';