Skip to content

Instantly share code, notes, and snippets.

View msaxena25's full-sized avatar

Mohit Saxena msaxena25

View GitHub Profile
@msaxena25
msaxena25 / AgmCoreModule - Load Google API KEY Dynamically from Angular service
Last active April 23, 2019 11:01
AgmCoreModule - Load Google API KEY Dynamically from Angular service
#Create a CustomLazyAPIKeyLoader class file and extends MapsAPILoader
import { GoogleMapsScriptProtocol, LAZY_MAPS_API_CONFIG, LazyMapsAPILoaderConfigLiteral, MapsAPILoader } from '@agm/core';
import { DocumentRef, WindowRef } from '@agm/core/utils/browser-globals';
import { Inject, Injectable } from '@angular/core';
import { AppGlobals } from './../../Common/Services/app.global';
@Injectable()
export class CustomLazyAPIKeyLoader extends MapsAPILoader {
@msaxena25
msaxena25 / steps to generate google restrict key:
Last active March 20, 2018 07:25
Generate Google API Restrict Key
Here are steps to generate google restrict key:
1. Go to site: https://console.developers.google.com/projectselector/apis/credentials?supportedpurview=project
2. Log in to the page with your Gmail account
3. Create or Select your project.
a. If you already created a project before then you can select that one.
b. Else click on create button to create a project.
@msaxena25
msaxena25 / Create AppDateAdapter.ts file
Created March 20, 2018 07:33
Angular Material - Override DateAdapter and change format of date picker fields
import { Inject, Injectable, Optional } from '@angular/core';
import { DateAdapter, NativeDateAdapter } from '@angular/material';
export class AppDateAdapter extends NativeDateAdapter {
parse(value: any): any {
if (value) {
const timestamp = typeof value === 'number' ? value : Date.parse(value);
let date_regex = /([0-9]{2}[0-9]{1}\d{1})-([0]{1}[1-9]{1}|[1]{1}[0-2]{1})-([0]{1}[1-9]{1}|[12]{1}\d{1}|[3]{1}[01]{1})/; // Can you own regex or remove it.
if (date_regex.test(value)) {
return isNaN(timestamp) ? null : new Date(timestamp);
@msaxena25
msaxena25 / Selection Sort Algorithm in JavaScript
Last active March 27, 2018 10:33
Selection Sort Algorithm in JavaScript
This algorithm will first find the smallest element in the array and swap it with the element in the first position,
then it will find the second smallest element and swap it with the element in the second position,
and it will keep on doing this until the entire array is sorted.
// Method to find Minimum Value and Minimum Index
function findMinIndex(arr, startIndex) {
var minIndex = startIndex;
var minValue = arr[startIndex];
function printFib(count) {
var n1 = 0; // first char is 0 of fibonacci series
var n2 = 1; // seconds char is also fixed '1'
var output = "";
output = n1 + " " + n2;
// loop start from 2 because we know the first and second chars
for (var i = 2; i < count; i++) {
@msaxena25
msaxena25 / First non repeating char : First way
Created March 28, 2018 06:38
How could you find the first non repeating char in a string?
//First non repeating char : How could you find the first non repeating char in a string?
function firstNonRepeatChar(s) {
for (var i = 0; i < s.length; i++) {
var letter = s.charAt(i);
if (s.substring(s.indexOf(letter) + 1, s.length).indexOf(letter) == -1) {
console.log(letter);
break;
}
}
}
@msaxena25
msaxena25 / gist:a8b34d644167b7e40f57474c25f9483b
Created June 8, 2018 10:11
Array (contains Object type element) get Unique objects items only
1. Compare two arrays contains Object type element
2. Below we are checking based on one property "id"
3. remove duplicate items
(<any>Array.prototype).unique = function () {
var a = this.concat();
for (var i = 0; i < a.length; ++i) {
for (var j = i + 1; j < a.length; ++j) {
if (a[i].id === a[j].id) // checking based on id property
@msaxena25
msaxena25 / one
Created June 12, 2018 09:08
Angular 5 - Gmail Logout
(<any>window).gapi.load('auth2', () => {
(<any>window).gapi.auth2.init().then(() => {
var auth2 = (<any>window).gapi.auth2.getAuthInstance();
auth2.signOut().then((res) => {
if (response.responseCode == 'S0001') {
window.location.href = '/login';
}
})
.catch((err) => { console.log('error is ', err) });
@msaxena25
msaxena25 / offline
Last active June 13, 2018 10:59
Angular How to Check No internet connection And Cookies Disabled
navigator.onLine --> Check for Internet connection
navigator.cookieEnabled --> Check for Cookies Enabled.
if (navigator.onLine == false || navigator.cookieEnabled == false) {
this._router.navigate(['/offline']); // create a new page named like "offline" & redirect to that page
this.util.isOffline = true; // for custom use to activate route
}
@msaxena25
msaxena25 / gist:dd698145f5917cc9211831328928051e
Last active April 28, 2021 23:41
Angular Open File in new window -- Get Byte stream from server and Open in Browser
getFile(id) {
this.newWindow = window.open();
this.downloadDocument(id).subscribe(response => { // API - this.downloadDocument(id)
this.util.removeLoader();
this.downloadFileFromBlob(response["_body"]);
}, error => { })
}