Skip to content

Instantly share code, notes, and snippets.

const environment = {
type: 'prod',
variables: {
api: 'some-url',
port: '8080'
}
}
const {
type: envType, // we extract "type" and call it "envType" here
const client = {name: 'John', login: 'john45', secretKey: 'xyz123'};
const {secretKey, ...clientData} = client;
clientData // {name: 'John', login: 'john45'};
function expandObjWithStatus(obj, condition) {
return {
...obj,
...(condition && {
status: 'modified'
})
}
}
@Component({})
export class AppComponent {
readonly noneMessage = "nothing";
readonly selectSeat$ = new Subject<number>();
readonly selectedMessage$ = this.selectSeat$.pipe(
scan(registerSeats, new Set<number>()),
startWith(new Set<number>()),
map(set => (set.size ? Array.from(set).join(", ") : this.noneMessage)),
@MarsiBarsi
MarsiBarsi / token.ts
Created March 23, 2021 15:14
the whole page visibility$ token
import {DOCUMENT} from '@angular/common';
import {inject, InjectionToken} from '@angular/core';
import {fromEvent, Observable} from 'rxjs';
import {distinctUntilChanged, map, share, startWith} from 'rxjs/operators';
export const PAGE_VISIBILITY = new InjectionToken<Observable<boolean>>(
'Shared Observable based on `document visibility changed`',
{
factory: () => {
const documentRef = inject(DOCUMENT);
@MarsiBarsi
MarsiBarsi / rxjs-challenge.ts
Last active March 23, 2021 14:50
rxjs pageVisibility$
const pageVisibility$ = fromEvent(documentRef, 'visibilitychange').pipe(
startWith(0),
map(() => documentRef.visibilityState !== 'hidden'),
distinctUntilChanged(),
);
@MarsiBarsi
MarsiBarsi / package.json
Created August 12, 2020 13:13
How to avoid heap out of memory in nodejs
{
"scripts": {
"build": "node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng build --prod --configuration=production"
}
}
@MarsiBarsi
MarsiBarsi / is-mobile.token.ts
Created July 30, 2020 19:53
is-mobile.token.ts
import { inject, InjectionToken } from '@angular/core';
// from https://github.com/ng-web-apis/common
import { USER_AGENT } from '@ng-web-apis/common';
// https://stackoverflow.com/a/11381730/2706426 http://detectmobilebrowsers.com/
const firstRegex = /(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino/;
const secondRegex = /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go
@MarsiBarsi
MarsiBarsi / get-native-focused.ts
Created July 14, 2020 13:30
Returns current active element, including shadow dom
/**
* Returns current active element, including shadow dom
*
* @return element or null
*/
export function getNativeFocused(documentRef: Document): Element | null {
if (!documentRef.activeElement || !documentRef.activeElement.shadowRoot) {
return documentRef.activeElement;
}
@MarsiBarsi
MarsiBarsi / static-request.service.ts
Last active November 30, 2020 03:44
static-request.service.ts
@Injectable({ providedIn: 'root' })
export class StaticRequestService {
private readonly cache = new Map<string, Observable<string>>();
request(url: string): Observable<string> {
const cache = this.cache.get(url);
if (cache) {
return cache;
}