Skip to content

Instantly share code, notes, and snippets.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>ACPI</key>
<dict>
<key>Add</key>
<array>
<dict>
<key>Comment</key>
@drewwiens
drewwiens / random-typescript.ts
Created November 16, 2020 16:34
Random useful TypeScript code snippets
/*
Ever need just the keys of an interface who's values match a certain type? e.g.
interface A {
id: string;
other: boolean:
}
we want a union of the keys of the properties with a value of type string (useful for strongly typing a mapping arrays of objects, to hashmap with a specific key value to be used as key in hashmap)
*/
type Unionize<T extends object> = {
@drewwiens
drewwiens / ng-dynamic-i18n.md
Last active April 14, 2022 20:14
(i18n) A simple way to dynamically load Angular translations

Problem

Calling loadTranslations() in either main.ts or in a resolved Promise does not work, at least in Angular 9 rc12. The function has to be called synchronously in polyfills.ts, so the translations also have to be available as a JS object, i.e. you can't fetch or XHR the JSON.

Solution

You could require() the JSON file in polyfills.ts, but then the JSON for all the languages will be in the bundle. (Try it and search for each JSON in polyfills.js.)

Alternatively, save each translation as a .js file and add a <script> tag to load only the desired translations.

@drewwiens
drewwiens / rxjs-operators.ts
Last active September 8, 2024 06:11
Handy custom RxJS operators for Angular etc
import { AbstractControl, FormArray, FormGroup } from '@angular/forms';
import { map, toPairs, fromPairs, differenceWith, isEqual, isNull, isUndefined } from 'lodash';
import { Observable, OperatorFunction, defer, empty, of, merge, pipe } from 'rxjs';
import { distinctUntilChanged, filter, map, shareReplay, pairwise } from 'rxjs/operators';
/**
* Convenience RxJS operator that filters out undefined & null and modifies the downstream type
* appropriately.
*/
export function exists<T>(): OperatorFunction<T | undefined | null, T> {