Skip to content

Instantly share code, notes, and snippets.

View nivrith's full-sized avatar
🎸
Recreational Mathemusician

Nivrith nivrith

🎸
Recreational Mathemusician
View GitHub Profile
@nivrith
nivrith / Trouble with default assignment with logical or.js
Last active November 27, 2020 09:46
Trouble with default assignment with logical or
console.log(length); // 25 and not 0
console.log(message); // "John" and not ""
let length = 0;
console.log(length || 99); // 99
console.log(length ?? 99); // 0
@nivrith
nivrith / Use Set to filter Uni values in array.js
Created November 27, 2020 09:47
Use Set to filter Uni values in array
const array = [1, 2, 2, 3,3, 4, 5, 5, 3, 6, 9]
const uniqueArray = [...new Set(array)];
console.log(uniqueArray); //[1, 4, 6, 9]
@nivrith
nivrith / functions with multiple parameters are unreadable.js
Last active November 27, 2020 09:49
functions with multiple parameters are unreadable
function getImageURLById(imageId, asAttachment = false, asThumbnail = false, withCompression = false) {
...
}
// This is unreadable without checking function definition
getImageURLById(25, true, false, true);
@nivrith
nivrith / Destructured object parameters are better than multiple function parameters.js
Last active November 27, 2020 09:51
Destructured object parameters are better than multiple function parameters
function getImageURLById({imageId, asAttachment = false, asThumbnail = false, withCompression = false}) {
...
}
// This is more readable and doesn't require checking the function definition
getImageURLById({imageId: 25, asAttachment: true, withCompression: true});
@nivrith
nivrith / Filter Falsey values out of an array.js
Created November 27, 2020 09:52
Filter Falsey values out of an array
const array = [1, 0 , {foo: 'bar'}, undefined, null];
const truthyArray = array.filter(Boolean);
//or we can use the double bang !! to do the same thing
const truthyArray = array.filter(value => !!value);
@nivrith
nivrith / merge latest from final.ts
Last active December 6, 2020 05:51
Final merge latest from operator
export type ObservableSourceType<T = any> = Observable<T> | ((value: any) => Observable<T>);
function* sourcesGen<T>(sources: ObservableSourceType[], a: T, i: number): Generator<Observable<any>, Observable<any>, undefined> {
const length = sources.length;
for (const [index, source] of sources.entries()) {
const observableSource: Observable<any> = typeof source === 'function' ? source.call(null, [a, i]) : source;
if (index <= length + 1) {
yield observableSource;
} else {
return observableSource;
@nivrith
nivrith / merge latest from.ts
Last active December 6, 2020 05:53
Merge Latest From Operator to get latest emission of other source observables
export type ObservableSourceType<T = any> = Observable<T> | ((value: any) => Observable<T>);
function* sourcesGen<T>(sources: ObservableSourceType[], a: T, i: number): Generator<Observable<any>, Observable<any>, undefined> {
const length = sources.length;
for (const [index, source] of sources.entries()) {
const observableSource: Observable<any> = typeof source === 'function' ? source.call(null, [a, i]) : source;
if (index <= length + 1) {
yield observableSource;
} else {
return observableSource;
@nivrith
nivrith / preserve-query-params-path-location-strategy.ts
Last active February 1, 2021 22:34
How to preserve query params globally with angular router
import { PathLocationStrategy, APP_BASE_HREF, PlatformLocation } from '@angular/common';
import { Optional, Inject, Injectable } from '@angular/core';
import { UrlSerializer } from '@angular/router';
@Injectable()
export class PreserveQueryParamsPathLocationStrategy extends PathLocationStrategy {
private get search(): string {
return this.platformLocation?.search ?? '';
}
constructor(
@nivrith
nivrith / click-double.directive.ts
Created November 16, 2021 07:49
Bind Single and Double Click events in Angular without clashing or bubbling issues
import { Directive, EventEmitter, HostListener, Input, OnDestroy, OnInit, Output } from '@angular/core';
import { Subject, Subscription } from 'rxjs';
import { debounceTime } from 'rxjs/operators';
@Directive({
selector: '[click.single],[click.double]',
})
export class ClickDoubleDirective implements OnInit, OnDestroy {
@Input() debounceTime = 300;
@Output('click.double') doubleClick = new EventEmitter();
@nivrith
nivrith / click-double-usage-example.html
Created November 16, 2021 07:54
Bind Single and Double Click events in Angular without bubbling issues
// In some angular template bind to events `click.single` and `click.double`
<div (click.single)="onSingleClick($event)" (click.double)="onDoubleClick(event)">
I got 99 problems but the click ain't one
</div>