← Torna al blog

Dev Tips

Un trucchetto al giorno leva le complicazioni di torno! Piccole "scorciatoie" che possono facilitare la vita degli sviluppatori e far risparmiare tempo...e righe di codice.

70

How to extract a property from an object

2021-04-30

1// JS Tip of the Day
2// How to extract a property from an object
3
4const hero = {
5  name: 'Batman',
6  weapon: 'Batarang',
7  vehicle: 'Batmobile',
8  partner: 'Robin'
9};
10
11const { vehicle, ...anotherHero } = hero;
12console.log(anotherHero); // { name: 'Batman',  weapon: 'Batarang', partner: 'Robin' }
69

Hiding Characters of a String

2021-04-29

1// JS Tip of the Day
2// Hiding Characters of a String
3
4function hideChars(str = '', chars = 0, obfuscator = '*') {
5    const maxChars = chars > str.length ? str.length : chars;  
6    const partialStr = str.slice(0, str.length - maxChars);
7    return partialStr.padEnd(str.length, obfuscator);
8}
9
10
11console.log(hideChars('My name is Batman',6)); // My name is ******
12
68

Tagged Template Literals (Simple Example)

2021-04-28

1// JS Tip of the Day
2// Tagged Template Literals (Simple Example)
3
4const hero = 'Batman';
5const anotherHero = 'Superman';
6
7function uppercaseTagged(strings, ...values) {
8   console.log(strings); // ['Film: ', 'v', ': Dawn of Justice']
9   console.log(values);  // ['Batman', 'Superman']
10   let strWithUppercase = '';
11   strings.forEach((str, i) => {
12       strWithUppercase += str + (values[i] || '').toUpperCase();
13   });
14   return strWithUppercase;
15}
16
17console.log(uppercaseTagged`Film: ${hero} v ${anotherHero}: Dawn of Justice`);
67

How to abort a Fetch request

2021-04-27

1// JS Tip of the Day
2// How to abort a Fetch request
3
4const controller = new AbortController();
5
6// You can call controller.abort() to abort the request;
7setTimeout(() => controller.abort());
8  
9const url = 'https://hero-url.com'
10fetch(url, { signal: controller.signal })
11  	.then((response) => {
12  	  // ...
13 	 })
14 	 .catch((e) => {
15 	  	console.log('abort', e);
16 	 })
66

Super Simple Linear Search

2021-04-26

1// JS Tip of the Day
2// Super Simple Linear Search
3
4const linearSearch = (list, itemToSearch) => {
5  for (const [idx, item] of list.entries()) {
6    if (item === itemToSearch) {
7      return idx;
8    }
9  }
10  return -1;
11}
12
13linearSearch(['Batman', 'Superman', 'Green Arrow'], 'Batman'); // 0
14linearSearch(['Batman', 'Superman', 'Green Arrow'], 'Wonder Woman'); // -1
15

Contattaci.

Hai in mente un progetto e vorresti realizzarlo?
Sei interessato a migliorare le competenze del tuo team in ambito di programmazione e sviluppo?
Oppure vuoi semplicemente prendere prendere un caffè con noi e vedere la nostra collezione di Action Figure, allora scrivici tramite questo form.

Se, invece, vuoi far parte del team, guarda le nostre offerte di lavoro.

Questo sito è protetto da reCAPTCHA e si applicano le Norme sulla privacy e i Termini di servizio di Google.