This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// npm install md5 | |
import * as md5 from 'md5'; | |
export const enum GravatarType { | |
MysteryPerson, | |
Identicon, | |
MonsterId, | |
Wavatar, | |
Retro, | |
Robohash, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function base_href() { | |
try { | |
const base = document | |
.getElementsByTagName('base') | |
.item(0) | |
.getAttribute('href'); | |
return base !== '/' ? base : null; | |
} catch (e) { | |
return null; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
NewerOlder