Skip to content

Instantly share code, notes, and snippets.

View ngrnv's full-sized avatar
🏠
Working from home

Alexey Nagornov ngrnv

🏠
Working from home
  • Yerevan, Armenia
View GitHub Profile
module.exports = Deferred
Deferred.defer = defer
function Deferred(Promise) {
if (Promise == null) Promise = global.Promise
if (this instanceof Deferred) return defer(Promise, this)
else return defer(Promise, Object.create(Deferred.prototype))
}
function defer(Promise, deferred) {
@ngrnv
ngrnv / component.ts
Created October 15, 2019 10:54
ng-bootstrap modal and ExpressionChangedAfterItHasBeenCheckedError
/**
* Triggered on (change) event on checkbox inside template form
* See https://github.com/ng-bootstrap/ng-bootstrap/issues/1252
*/
onChange(event: Event) {
/* The Angular form directives have bindings that update synchronously based on focus change
The modal brings focus to the window (required by accessibility) */
(event.target as HTMLInputElement).blur();
@ngrnv
ngrnv / jwtRS256.sh
Last active May 12, 2019 21:55 — forked from ygotthilf/jwtRS256.sh
How to generate JWT RS256 key
ssh-keygen -t rsa -b 4096 -m PEM -f fg-platform-auth.key
# Don't add passphrase
openssl rsa -in fg-platform-auth.key -pubout -outform PEM -out fg-platform-auth.key.pub
cat fg-platform-auth.key
cat fg-platform-auth.key.pub
@ngrnv
ngrnv / log.ts
Created May 2, 2019 08:45
RxJS log operator
export const log = <T>(tag: string = '') => tap<T>(
next => console.log(`%c[${tag}: Next]`, 'color: #4CAF50;', next),
error => console.log(`%c${tag}: Error]`, 'color: #F44336;', error),
() => console.log(`%c[${tag}: Complete]`, 'color: #2196F3;')
);
/*
usage:
...
.pipe(
@ngrnv
ngrnv / copy-pub.sh
Last active April 7, 2019 10:52
Copy PUB key to remote server
cat ~/.ssh/ngrnv_rsa.pub | ssh user@server "mkdir -p ~/.ssh && touch ~/.ssh/authorized_keys && chmod -R go= ~/.ssh && cat >> ~/.ssh/authorized_keys”
@ngrnv
ngrnv / to.ts
Created December 5, 2018 13:55
Function that returns array of result and error for async function (to avoid catch)
export type ResultAndError<T, E extends Error> = [T | null, E | null]; // tuple
export const to = <T>(promise: Promise<T>): Promise<ResultAndError<T, Error>> => {
return promise.then<ResultAndError<T, Error>>(data => {
return [data, null];
})
.catch<ResultAndError<T, Error>>(err => [null, err]);
};
const [result, error] = await to(this.anything.promising());
@ngrnv
ngrnv / concat.js
Created September 27, 2018 19:05
Concat arrays of array elements that are
/**
* let arr = [ { prop: [1,2,3] }, { prop: [3,4,5] } ];
* let result = concatBy(arr, 'prop');
*/
const concatBy = <T>(arr: T[], propName: string): T[] =>
arr.reduce((acc: T[], val) => acc.concat(val[propName]), []);