Skip to content

Instantly share code, notes, and snippets.

View sandcastle's full-sized avatar
🍦

Glenn Morton sandcastle

🍦
View GitHub Profile
@sandcastle
sandcastle / copy-to-clipboard.ts
Created July 17, 2017 06:38
A copy to clipboard function (in typescript)
export const copyToClipboard = (url: string) => {
document.addEventListener('copy', (e: ClipboardEvent) => {
e.clipboardData.setData('text/plain', url);
e.preventDefault();
document.removeEventListener('copy');
});
document.execCommand('copy');
};
@sandcastle
sandcastle / GUID_REGEX.js
Created July 20, 2017 05:50
Guid / UUID regex
const GUID_REGEX = /([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/ig;
@sandcastle
sandcastle / fsAsync.js
Created July 20, 2017 06:45
Async fs in node.js
const {promisify} = require('util');
const fs = require('fs');
const readFileAsync = promisify(fs.readFile);
const filePath = process.argv[2];
async function main() {
try {
const text = await readFileAsync(filePath, {encoding: 'utf8'});
@sandcastle
sandcastle / LinqExtensions.Partition.cs
Created August 9, 2017 14:33
Partition a list into many partitions of equal size. Handy for things like migrations where you want to break things into "buckets"
internal static class LinqExtensions
{
public static List<List<T>> Partition<T>(this IEnumerable<T> list, int partitionSize)
{
var i = 0;
var splits = from item in list
group item by i++ / partitionSize into part
select part.ToList();
return splits.ToList();
}
@sandcastle
sandcastle / DateService.ts
Last active August 17, 2017 00:12
Date and time service to help with UTC handling and formatting.
import { ISecurityService } from './security';
import * as moment from 'moment-timezone';
const UTC_DATE_PARSE: string = 'YYYY-MM-DD';
export const DEFAULT_TIMEZONE: string = 'UTC';
/**
* Provides time zone, date conversion and formatting support
* based on the user's preferences.
*/
@sandcastle
sandcastle / redirect-ingress.yml
Created August 27, 2017 02:07
An example of a ingress redirect using kubernetes and nginx `configuration-snippet`
kind: Ingress
apiVersion: extensions/v1beta1
metadata:
name: redirect-ingress
annotations:
ingress.kubernetes.io/configuration-snippet: |
if ($host ~ ^(.+)\.somedomain\.io$) {
return 301 https://$1.domain.io$request_uri;
}
spec:
@sandcastle
sandcastle / parseEmailList.js
Last active September 27, 2017 04:29
Parse a list of emails separated by comma (,) or semicolon (;)
/**
* Regex source: https://html.spec.whatwg.org/multipage/forms.html#valid-e-mail-address
*/
const EMAIL_REGEX = /[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*/g;
/**
* Parse a list of valid emails from the specified text.
*
* @param {String} text The string of email addresses to parse.
* @returns {[{email: String, name: String}]}
@sandcastle
sandcastle / timezones.ts
Created January 29, 2018 00:19
A timezone list with grouping by timezone, in TypeScript.
export interface IanaTimezone {
group: string;
timezone: string;
label: string;
}
export const IANA_TIMEZONES = [
// UTC+14:00
{ group: 'UTC+14:00', timezone: 'Pacific/Kiritimati', label: 'Pacific/Kiritimati (+14)' },
// UTC+13:00
@sandcastle
sandcastle / inIframe.js
Last active May 2, 2018 12:32
Checks if a given window is within an iframe in a safe way.
/**
* Determines if the the current window is within an iframe
* in a safe cross browser model.
*
* @returns {Boolean} True if in an iframe, else false.
*/
inIframe() {
try {
return window.self !== window.top;
} catch (e) {
@sandcastle
sandcastle / text_replace.sh
Created July 1, 2018 08:15
Cross platform text replacement recursively in a directory
function text_replace() {
case "${OSTYPE}" in
darwin*) PLATFORM="OSX" ;;
linux*) PLATFORM="LINUX" ;;
bsd*) PLATFORM="BSD" ;;
*) PLATFORM="UNKNOWN" ;;
esac
if [[ "${PLATFORM}" == "OSX" || "${PLATFORM}" == "BSD" ]]; then
find artifacts/ -type f -name "*.yml" -exec sed -i "" "s/$1/$2/g" {} +