Skip to content

Instantly share code, notes, and snippets.

View guillefd's full-sized avatar
🤖
Building

Guillermo guillefd

🤖
Building
View GitHub Profile
@guillefd
guillefd / import.ts
Created May 17, 2019 21:25
Import JSON in Angular
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import jsonCfg from '../../../data/config.json';
@Injectable()
export class AppCfg {
constructor(private http: HttpClient) {}
load(): Promise<void> {
@guillefd
guillefd / async2.js
Last active January 30, 2019 13:52 — forked from bmorelli25/async2.js
Async/Await Example
/// EXAMPLE 1
function resolveAfter2Seconds() {
return new Promise(resolve => {
setTimeout(() => {
resolve('resolved');
}, 2000);
});
}
@guillefd
guillefd / call-observables-after-observable.js
Last active January 29, 2019 13:53
Get a list, perform a fetch on each item and return one array of objects as result
/**
Returns a list of objects
1. get the list of files to fetch
2. fetch each file to get the object
3. return an array of objects
*/
getAll(): Observable<Tutorial[]> {
const list = this.dataSrv.getDataList()
.pipe(
// get list from dataList
<html>
<style>
.item-header {
grid-area: header;
padding: 0px;
}
.item-btn-a {
grid-area: btnA;
padding: 0px;
@guillefd
guillefd / async-await.js
Created April 3, 2018 18:18 — forked from wesbos/async-await.js
Simple Async/Await Example
// 🔥 Node 7.6 has async/await! Here is a quick run down on how async/await works
const axios = require('axios'); // promised based requests - like fetch()
function getCoffee() {
return new Promise(resolve => {
setTimeout(() => resolve('☕'), 2000); // it takes 2 seconds to make coffee
});
}
@guillefd
guillefd / javascript-promise-timeout.js
Created February 18, 2018 14:59 — forked from john-doherty/javascript-promise-timeout.js
Adds a timeout to a JavaScript promise, rejects if not resolved within timeout period
/**
* wraps a promise in a timeout, allowing the promise to reject if not resolve with a specific period of time
* @param {integer} ms - milliseconds to wait before rejecting promise if not resolved
* @param {Promise} promise to monitor
* @Example
* promiseTimeout(1000, fetch('https://courseof.life/johndoherty.json'))
* .then(function(cvData){
* alert(cvData);
* })
* .catch(function(){
@guillefd
guillefd / sanitize.service.ts
Created February 7, 2018 13:52
Angular, sanitize string: remove tags and urls
import { Injectable } from '@angular/core';
@Injectable()
export class SecurityService {
constructor() { }
sanitizeAllTags(text:string) {
return text.replace(/<[^>]*>/g, '*');
}
@guillefd
guillefd / form.component.ts
Last active February 6, 2018 20:29
Angular, form value change observer
import { Component } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
@Component({
selector: 'form',
templateUrl: `
<form [formGroup]="form">
<input type="text" formControlName="name">
</form>
`,
@guillefd
guillefd / custom-validations.js
Created February 3, 2018 18:46
JS Regex Validations
function isInputValid(value, type) {
let valid;
let errorTxt;
switch(type) {
case 'email':
valid = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(value);
errorTxt = 'Enter a valida email';
break;
case 'phone':
valid = /^([-0-9.()+ ]{5,50})*$/.test(value);
@guillefd
guillefd / array.js
Created February 1, 2018 02:11
Javascript Array: find item index and remove from Array
function findAndRemove(array, item) {
const index = array.indexOf(item);
if(index>-1) {
array.splice(index, 1);
}
return array;
}