Skip to content

Instantly share code, notes, and snippets.

View Tusharbalar's full-sized avatar

Tushar Balar Tusharbalar

View GitHub Profile
@Tusharbalar
Tusharbalar / tricks.ts
Created December 14, 2017 11:26
Ionic Tricks
Making a phone call from app
<a href="tel:+9876543210">9876543210</a>
The launching of external pages can be done directly from your view. Take the following example:
- window.open(‘http://example.com’, ‘_system’); Loads in the system browser
- window.open(‘http://example.com’, ‘_blank’, 'location=yes'); Loads in the InAppBrowser
- window.open(‘http://example.com’, ‘_blank’, ‘location=no’); Loads in the InAppBrowser with no location bar
- window.open(‘http://example.com’, ‘_self’); Loads in the Cordova web view
@Tusharbalar
Tusharbalar / shared.ts
Last active December 14, 2017 07:45
Using BehaviorSubject data share between multiple components
@Injectable()
export class SharedService {
lastMsg = new BehaviorSubject<any>('initialData');
telecast = this.lastMsg.asObservable();
update(newVal) {
// Here we get data and emit it
this.lastMsg.next(newVal);
}
@Tusharbalar
Tusharbalar / array.ts
Last active December 4, 2017 13:19
Filter boolean value
// Filter in array
let a = [0, 1, '', true, false, null, "DSDS", "1", "0"];
a.filter(Boolean); // [1, true, "DSDS", "1", "0"]
// Sorting an array
var a = [1,3,2,5,33,22,32];
a.sort((a, b) => a — b); // [1, 2, 3, 5, 22, 32, 33]
@Tusharbalar
Tusharbalar / filter-array.ts
Created December 4, 2017 12:42
Filter boolean value
let a = [0, 1, '', true, false, null, "DSDS", "1", "0"];
a.filter(Boolean); // [1, true, "DSDS", "1", "0"]
@Tusharbalar
Tusharbalar / merge-object.ts
Created December 4, 2017 12:34
Merge an array of objects together
// merge an array of objects
const data = [ {a: 1}, {b: 2}, {c: 3} ]
const merged = data.reduce((res, obj) => ({...res, ...obj}), {})
console.log(merged) // => { a: 1, b: 2, c: 3 }
// merge an array of objects by property
const toMerge = [
{ id: 1, value: 'a', },
{ id: 2, value: 'b', },
{ id: 3, value: 'c' },
@Tusharbalar
Tusharbalar / remove-object.ts
Created December 4, 2017 12:22
Remove a key from an object
const a = {
foo: 'bar',
useful: 1,
useless: 2
}
const {useless, ...clean} = a;
console.log(clean); // {foo: 'bar', useful: 1}
@Tusharbalar
Tusharbalar / object-remove.ts
Created December 4, 2017 12:17
Remove an object from array by property
const initial = [ {id: 1, score: 1}, {id: 2, score: 2}, {id: 3, score: 4}]
const removeId = 3
const without3 = initial.filter(x => x.id !== removeId)
console.log(without3) // => [ { id: 1, score: 1 }, { id: 2, score: 2 } ]
@Tusharbalar
Tusharbalar / object-update.ts
Last active December 4, 2017 12:18
Update an array object by property
const initial = [ {id: 1, score: 1}, {id: 2, score: 2}, {id: 3, score: 4}]
const newValue = {id: 3, score: 3}
const updated = initial.map(x => x.id === newValue.id ? newValue : x)
console.log(updated) // => [ { id: 1, score: 1 }, { id: 2, score: 2 }, { id: 3, score: 3 } ]
@Tusharbalar
Tusharbalar / titleCase.pipe.ts
Created November 24, 2017 13:20
Angular 2 custom pipe to transform string to Title Case
import { Pipe, PipeTransform } from '@angular/core';
/*
* Changes the case of the first letter of a given number of words in a string.
*/
@Pipe({ name: 'titleCase', pure: false })
export class TitleCase implements PipeTransform {
transform(input: string, length: number): string {
return input.length > 0 ? input.replace(/\w\S*/g, (txt => txt[0].toUpperCase() + txt.substr(1).toLowerCase())) : '';
@Tusharbalar
Tusharbalar / fileUpload.ts
Last active October 12, 2020 01:43
Ionic3 - Image upload using Camera, Gallery and File
/*
* component file code
*/
/*
* IMAGE UPLOAD SECTION
*/
public takePicture(sourceType) {