Skip to content

Instantly share code, notes, and snippets.

View theredhead's full-sized avatar

Kris theredhead

View GitHub Profile
@theredhead
theredhead / makeCompareFunction.ts
Created March 29, 2021 06:19
Typescript generic "make a compare function" function that takes a lambda, expected to return the property to compare (normally).
/* Create a function used with `sort()` to sort arrays of objects
* for example:
* const sortByLastName = makeCompareFunction<Person>(p => p.lastName);
* people.sort(sortByLastName);
*/
export function makeCompareFunction<T>(lambda: (obj: T) => string | number) {
return (a: T, b: T): number => {
const _a = lambda(a);
const _b = lambda(b);
if (_a < _b) {
@theredhead
theredhead / gravatar-url.ts
Created March 16, 2021 11:29
Gravatar url in typescript
// npm install md5
import * as md5 from 'md5';
export const enum GravatarType {
MysteryPerson,
Identicon,
MonsterId,
Wavatar,
Retro,
Robohash,
@theredhead
theredhead / sluggify.ts
Created March 9, 2021 19:33
Sluggify a text (typescript)
const sluggify = (text: string): string => {
const allowedCharacters = 'abcdefghijklmnopqrstuvwxyz_1234567890';
const result = [];
const preTransformed = text.toLowerCase().replace(/\s/, '-');
for (let char of preTransformed) {
if (allowedCharacters.includes(char)) {
result.push(char);
}
}
@theredhead
theredhead / mysql-backup-docker.sh
Created December 1, 2020 09:05
Backup and restore mysql databases running in docker containers (from docker host shell)
# CONTAINER_ID => value of container id column from `docker ps`
# DATABASE_NAME => name of the mysql database to backup/restore
# Backup to backup-file.sql outside container
docker exec CONTAINER_ID /usr/bin/mysqldump --triggers --routines -uMYSQL_USER -pMYSQL_PASSWORD DATABASE_NAME > backup-file.sql
# Restore from backup-file.sql outside container
cat backup-file.sql | docker exec -i CONTAINER_ID /usr/bin/mysql -uMYSQL_USER -pMYSQL_PASSWORD DATABASE_NAME
@theredhead
theredhead / preference-in-localstorage.ts
Created June 22, 2020 08:19
Annotate a field in a typescript class to store its' content in localstorage transparently.
//
// Inspired by https://github.com/zhaosiyang/property-watch-decorator/blob/master/src/index.ts
//
// Annotate a field in a typescript class to store its' content in localstorage transparently.
//
export function Preference<T = any>(preferenceKey: string, defaultValueObj: T) {
return (target: any, key: PropertyKey) => {
Object.defineProperty(target, key, {
set: function(value) {
localStorage.setItem(preferenceKey, JSON.stringify(value));
@theredhead
theredhead / columns-nn-nn.css
Created September 25, 2019 10:33
simple dual columns in simple css
/* You can add global styles to this file, and also import other style files */
.columns-80-20 {
display: grid;
grid-template-areas: 'left right';
grid-template-columns: 80% 20%;
*:nth-child(0) {
grid-area: left;
overflow: scroll;
@theredhead
theredhead / url-tools.ts
Created September 24, 2019 08:02
some utility to help building urls
export function base_href() {
try {
const base = document
.getElementsByTagName('base')
.item(0)
.getAttribute('href');
return base !== '/' ? base : null;
} catch (e) {
return null;
}
@theredhead
theredhead / text-functions.ts
Created September 18, 2019 13:33
useful text functions
export function object_to_searchable_text(obj: any): string {
if (obj === undefined) {
return '';
} else if (typeof obj === 'object') {
const parts = [];
for (const property of Object.keys(obj)) {
parts.push(object_to_searchable_text(obj[property]));
}
return parts.join(' ');
} else if (Array.isArray(obj)) {
dragOver = false;
fileDropped(file: File) {
const reader = new FileReader();
reader.onload = (event: ProgressEvent) => {
const json = reader.result as string;
this.rulesets = JSON.parse(json) as Ruleset[];
};
//reader.onerror...
reader.readAsText(file);
}
export function download(fileName: string, data: any) {
const json = JSON.stringify(data, null, 2);
const anchor = document.createElement('a');
anchor.setAttribute(
'href',
'data:text/plain;charset=utf-8,' + encodeURIComponent(json)
);
anchor.setAttribute('download', fileName);