Skip to content

Instantly share code, notes, and snippets.

View Mustafa-Omran's full-sized avatar
💭
I may be slow to respond.

Mustafa Omran Mustafa-Omran

💭
I may be slow to respond.
  • Egypt
View GitHub Profile
@Mustafa-Omran
Mustafa-Omran / language.service.ts
Created February 28, 2022 07:56
Angular - Language Service
import { Injectable, Inject } from '@angular/core';
import { BehaviorSubject, Observable } from 'rxjs';
import { TranslateService } from '@ngx-translate/core';
import { DOCUMENT } from '@angular/common';
@Injectable({
providedIn: 'root'
})
export class LanguageService {
private readonly DEFAULT_LANGUAGE = 'en';
@Mustafa-Omran
Mustafa-Omran / no-white-space.ts
Created March 14, 2022 09:16
Angular - No white spaces
import { FormControl } from "@angular/forms";
export function noWhitespaceValidator(control: FormControl) {
const isWhitespace = (control.value || '').trim().length === 0;
const isValid = !isWhitespace;
return isValid ? null : { 'whitespace': true };
}
@Mustafa-Omran
Mustafa-Omran / update.service.ts
Created March 14, 2022 11:08
Angular - Service Worker (Dealing With Updates)
import { Injectable } from '@angular/core';
import { MatSnackBar } from '@angular/material/snack-bar';
import { SwUpdate } from '@angular/service-worker';
@Injectable()
export class UpdateService {
constructor(private swUpdate: SwUpdate,
private snackbar: MatSnackBar) {
this.swUpdate.available.subscribe(event => {
@Mustafa-Omran
Mustafa-Omran / localstorage-changes-angular.ts
Created March 14, 2022 17:39
Angular - Listen to local storage changes angular
import { Component, HostListener, OnInit } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
@Mustafa-Omran
Mustafa-Omran / build-date.js
Created April 13, 2022 10:57
Angular - Save Build Time At Package.json
const { writeFileSync } = require('fs')
const { join } = require('path')
const BUILD_DATE_TIME_STAMP_PATH = join(__dirname, 'build-date.json');
const createBuildDate = {
buildDate: new Date()
}
writeFileSync(TIME_STAMP_PATH, JSON.stringify(createBuildDate, null, 2));
@Mustafa-Omran
Mustafa-Omran / itersection.ts
Last active August 18, 2022 16:25
Intersection of two Strings JS
private itersection(textOne: string, textTwo: string): string {
let textOneIndex: number = 0;
let textTwoIndex: number = 0;
let matchedText: string = '';
textOne = textOne.toLowerCase();
textTwo = textTwo.toLowerCase();
while (textOneIndex < textOne.length && textTwoIndex < textTwo.length) {
if (textOne[textOneIndex] < textTwo[textTwoIndex]) {
@Mustafa-Omran
Mustafa-Omran / angular-log-service.ts
Created January 22, 2023 12:58
Angular Log Service
import { Injectable } from "@angular/core";
export enum LogLevel {
DEBUG,
INFO,
ERROR,
}
@Injectable()
export class LogService {
minimumLevel: LogLevel = LogLevel.INFO;
@Mustafa-Omran
Mustafa-Omran / cache-interceptor.ts
Created February 13, 2023 11:15
Caching - Passing Context to HTTP Interceptors
const CACHE_IT = new HttpContextToken<boolean>(() => false);
export function cacheIt() {
return new HttpContext().set(CACHE_IT, true);
}
@Injectable()
export class CacheInterceptor implements HttpInterceptor {
intercept(request: HttpRequest, next: HttpHandler): Observable<HttpEvent> {
@Mustafa-Omran
Mustafa-Omran / auth.interceptor.ts
Last active September 20, 2024 08:58
Angular +17 Interceptor (HttpInterceptorFn)
import { User } from '@amaleyat/shared/models/user';
import { isPlatformBrowser } from '@angular/common';
import { HttpInterceptorFn } from '@angular/common/http';
import { inject, PLATFORM_ID } from '@angular/core';
export const authInterceptor: HttpInterceptorFn = (req, next) => {
const platformId: Object = inject(PLATFORM_ID);
if (isPlatformBrowser(platformId)) {
@Mustafa-Omran
Mustafa-Omran / http-client.service.ts
Last active September 20, 2024 09:06
New HttpClient Service
import { environment } from '@amaleyat/environments/dev.environment';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { Inject, Injectable, PLATFORM_ID } from '@angular/core';
import { Observable } from 'rxjs/internal/Observable';
import { throwError } from 'rxjs/internal/observable/throwError';
import { catchError } from 'rxjs/internal/operators/catchError';
import { isPlatformBrowser } from '@angular/common';
import { LanguageService } from './language.service';
@Injectable({