Skip to content

Instantly share code, notes, and snippets.

View T99's full-sized avatar
:shipit:
programmin'

Trevor Sears T99

:shipit:
programmin'
View GitHub Profile
@T99
T99 / intersection.ts
Last active December 21, 2021 14:17
Simple Intersection Function in Typescript
/*
* Created by Trevor Sears <trevor@trevorsears.com> (https://trevorsears.com/).
* 9:05 AM -- December 21st, 2021
*/
/**
* Returns an array representing the intersection between all of the provided input sets.
*
* @param {Iterable<T>} sets The sets for which an intersection subset should be generated.
* @returns {T[]} An array representing the intersection between all of the provided input sets.
@T99
T99 / decode-html-entities.ts
Last active December 9, 2021 21:16
Decode HTML entities in TypeScript
/**
* A regular expression that matches HTML entities.
* - 1st Capture Group: The entire matched HTML entity.
* - 2nd Capture Group: The inner text of the entity (everything inside the '&' and ';' characters).
* - 3rd Capture Group: The integer value of the entity, if one is found (i.e. '&#1234;' --> '1234').
*/
const HTML_ENTITY_REGEX: RegExp = /&(#([0-9]+)|[a-zA-Z0-9]+);/;
/**
* Returns the input string after having converted all recognized HTML entities to their respective unicode characters.
@T99
T99 / json-to-csv.ts
Last active September 14, 2021 17:27
Convert JSON data to CSV data.
/*
* Created by Trevor Sears <trevor@trevorsears.com> (https://trevorsears.com/).
* 12:43 PM -- September 14th, 2021
*/
/**
* Returns all of the unique properties found in any of the objects within the given array.
*
* @param {T[]} jsonArray The array of objects over which to build a list of unique properties.
* @returns {Array<keyof T>} An array containing all of the unique properties found in any of the objects within the
@T99
T99 / ts-class-signature.regexp
Created August 25, 2021 20:26
TypeScript Class Signature Regexp
^\s*(?:(?<export>export)\s+)?(?<typeType>class|abstract\s+class|interface)\s+(?:(?<typeName>[a-zA-Z][a-zA-Z0-9]*)(?<typeGenerics><.*?>)?)\s+(?:extends\s+(?<extends>[a-zA-Z][a-zA-Z0-9]*(?:<.*?>)?)\s+)?(?:implements\s+(?<implements>[a-zA-Z][a-zA-Z0-9]*(?:<.*?>)?(?:,\s+[a-zA-Z][a-zA-Z0-9]*(?:<.*?>)?)*)\s+)?{
@T99
T99 / template.service
Last active August 7, 2021 19:09
Systemd Service File Template
# Check the following link for more info:
# https://www.freedesktop.org/software/systemd/man/systemd.service.html
[Unit]
# The descriptive text used to describe this service.
Description=description for this service
# The directory inside which this service should run.
WorkingDirectory=/home/web/web_trevorsears/@
@T99
T99 / quot.regexp
Last active July 27, 2021 12:12
Broken/Improperly-escaped quote character regex
// Matches broken 'quot' escaped strings (i.e. '&quot;' characters).
((^|[^&a-zA-Z])quot($|[^;a-zA-Z])|(^|[^&a-zA-Z])quot;|&quot($|[^;a-zA-Z]))
// Matches:
// - quot
// - quot;
// - &quot
// Does not match:
// - quote
@T99
T99 / rfc3986.regex
Created June 22, 2021 13:08
(Mostly) RFC3986 Compliant URL/URI Parsing Regex
/(?(DEFINE)
(?<defunreserved>[A-Za-z0-9-._~])
(?<defpctencoded>\%[0-9A-Fa-f]{2})
(?<defsubdelims>[!$&'()*+,;=])
(?<defpchar>((?P>defunreserved)|(?P>defpctencoded)|(?P>defsubdelims)|:|@))
(?<defscheme>[A-Za-z][A-Za-z0-9+-.]*)
(?<defhierpart>.*?)
(?<defquery>((?P>defpchar)|\/|\?)*)
(?<deffragment>((?P>defpchar)|\/|\?)*)
)
@T99
T99 / tslint.json
Last active July 29, 2021 01:30
My 'tslint.json' Configuration
{
"extends": "tslint:recommended",
"rules": {
"adjacent-overload-signatures": false,
"align": [true,
"elements",
@T99
T99 / lazy-fill-image
Created June 3, 2021 14:58
Have images in HTML that lazily expand.
<div class="lazy-image" style="background: url('...')">
<canvas width="1600" height="800"></canvas>
</div>
@T99
T99 / merged-overwrite-type.ts
Last active February 25, 2020 18:36
A TypeScript type used to merge together object types and always use the property types of the last specified object type.
type InternalMergedOverwriteType<T0 extends {} = {}, T1 extends {} = {}> = {
[K in keyof (T0 & T1)]: K extends keyof T0 ?
(K extends keyof T1 ?
T1[K] : T0[K]) :
(K extends keyof T1 ?
T1[K] : never);
};