Skip to content

Instantly share code, notes, and snippets.

@starovoitovs
starovoitovs / app.component.ts
Created January 19, 2018 16:11
Directive that generates random id for input and sets for attributes for the label
import {Component, ViewEncapsulation, Directive, Input, ContentChild, Renderer2, ElementRef} from "@angular/core";
@Directive({
selector: "input,select,textarea"
})
export class FormInputDirective {
}
@Directive({
@starovoitovs
starovoitovs / localstorage.ts
Created December 1, 2017 23:15
Reactive localStorage
import {BehaviorSubject, Observable} from 'rxjs';
@Injectable()
export class LocalStorage implements ILocalStorage {
protected subjects: {[key: string]: BehaviorSubject<any>} = {};
select(key: string, defaultValue: any = null): Observable<any> {
if (this.subjects.hasOwnProperty(key)) {
@starovoitovs
starovoitovs / pascal.ts
Created August 6, 2017 20:01
Pascal triangle
// [1, 3, 3, 1] => [[1, 3], [3, 3], [3, 1]]
const pairs = ([head, ...tail]) => tail.map((item, i) => i === 0 ? [head, item] : [tail[i - 1], item]);
// [1, 3, 3, 1] => [1, 4, 6, 4, 1]
const next = numbers => [1, ...pairs(numbers).reduce((carry, pair) => [...carry, pair[0] + pair[1]], []), 1];
// 5 => <5 rows of pascal triangle>
const pascal = (n, carry = [[1]]) => n > 1 ? pascal(n - 1, [next(carry[0]), ...carry]) : carry.reverse();
@starovoitovs
starovoitovs / client.index.html
Last active June 12, 2017 09:01
Third-party authentication
<html>
<head>
<title>Client</title>
<script>
function openWindow() {
window.open('http://service/auth.html', '_blank', "toolbar=0,location=0,menubar=0,height=600,width=400");
window.addEventListener('message', function(message) {
document.write('Authorized with token: ' + message.data.token);
});
}
export const compose: (...funcs) => (value: any) => any =
(...funcs) => value => funcs.reduce((value, func) => value !== undefined ? func(value) : undefined, value);
[
{ "keys": ["tab"], "command": "unbound", "context":
[
{ "key": "has_next_field", "operator": "equal", "operand": true }
]
},
{ "keys": ["tab"], "command": "insert_best_completion", "args": {"default": "\t", "exact": true} },
{ "keys": ["tab"], "command": "insert_best_completion", "args": {"default": "\t", "exact": false},
"context":
[