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
@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]), []);
@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 / 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 / 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 / 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 / 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();
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 / use-constant.ts
Created January 13, 2020 08:54
react use-constant hook
import * as React from 'react'
type ResultBox<T> = { v: T }
export default function useConstant<T>(fn: () => T): T {
const ref = React.useRef<ResultBox<T>>()
if (!ref.current) {
ref.current = { v: fn() }
}
@ngrnv
ngrnv / curry.js
Last active February 1, 2020 19:49
js curry implementation
function curry(fn) {
return function curried() {
return arguments.length >= fn.length
? fn.apply(null, arguments) // if there are all args - execute fn with them
: curried.bind(null, ...arguments) // return new version of curried() with prepended arg(s)
}
}
curry((x, y, z) => x + y + z)(5)(4)(1); // 10
@ngrnv
ngrnv / ts-enum-mapping.ts
Created February 22, 2020 08:30
Map enum values to custom values in TypeScript
enum LogLevel {
ERROR, WARN, INFO, DEBUG
}
/**
* This is equivalent to:
* type LogLevelStrings = 'ERROR' | 'WARN' | 'INFO' | 'DEBUG';
*/
type LogLevelStrings = keyof typeof LogLevel;