← 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.

116

Intl API Examples

2021-07-14

1// JS Tip of the Day
2// Intl API Examples
3
4const number = 789123.456;
5
6// Number format
7Intl.NumberFormat("en-US").format(number); 
8// 789,123.456
9Intl.NumberFormat("it-IT").format(number); 
10// 789.123,456
11
12// Currency format
13new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(number);
14// $789,123.46
15new Intl.NumberFormat('it-IT', { style: 'currency', currency: 'EUR' }).format(number);
16// €789.123,46
17
18// Unit format
19const literConfig = {style: 'unit', unit: 'liter'};
20new Intl.NumberFormat('en-US', literConfig).format(number);
21// 789.123,46 L
22new Intl.NumberFormat('en-US', {...literConfig, unitDisplay: 'long'}).format(number);
23// 789.123,46 liters
24new Intl.NumberFormat('it-IT', literConfig).format(number);
25// 789.123,46 l
26new Intl.NumberFormat('it-IT', {...literConfig, unitDisplay: 'long'}).format(number);
27// 789.123,46 litri
28
29const meterConfig = {style: 'unit', unit: 'meter-per-second'};
30new Intl.NumberFormat('en-US', meterConfig).format(number);
31// 789.123,456 m/s"
32new Intl.NumberFormat('en-US', {...meterConfig, unitDisplay: 'long'}).format(number);
33// 789.123,456 meters per second
34new Intl.NumberFormat('it-IT', meterConfig).format(number);
35// 789.123,456 m/s"
36new Intl.NumberFormat('it-IT', {...meterConfig, unitDisplay: 'long'}).format(number);
37// 789.123,456 metri per secondo
115

Simple Zip Function

2021-07-13

1// JS Tip of the Day
2// Simple Zip Function
3
4const zip = (firstArray, secondArray) => {
5  const zipped = [];
6  for (let i = 0; i < Math.min(firstArray.length, secondArray.length); i++) {
7    zipped.push([firstArray[i], secondArray[i]]);
8  }
9  return zipped;
10};
11
12const zippedHeroes = zip(
13  ['Batman', 'Superman', 'Wonder Woman', 'Flash'],
14  ['Robin', 'Superboy', 'Donna Troy']
15);
16
17console.log(zippedHeroes);
18// [['Batman', 'Robin'], ['Superman', 'Superboy'], ['Wonder Woman', 'Donna Troy']]
114

Live or static reference of ESModule import

2021-07-12

1// JS Tip of the Day
2// Live or static reference of ESModule import
3// https://jakearchibald.com/2021/export-default-thing-vs-thing-as-default/
4
5// These give you a live reference to the exported thing(s):
6import { thing } from './module.js';
7import { thing as otherName } from './module.js';
8import * as module from './module.js';
9const module = await import('./module.js');
10// This assigns the current value of the export to a new identifier:
11let { thing } = await import('./module.js');
12
13// These export a live reference:
14export { thing };
15export { thing as otherName };
16export { thing as default };
17export default function thing() {}
18// These export the current value:
19export default thing;
20export default 'hello!';
113

How to display DOM Element in Console (Chrome)

2021-07-07

1// JS Tip of the Day
2// How to display DOM Element in Console (Chrome)
3
4// <html>...<p id="hero">I'm Batman!</p>...</html>
5
6console.log($0); // $0 is the active inspected element
7// <p id="hero">I'm Batman!</p>
8
9console.dir($0);
10/*
11p#hero { 
12	// ...
13    id: 'hero',
14	// ...
15    innerText: "I'm Batman!",
16    // ...
17 }
18*/
112

Smart use of Object Dinamic Key

2021-07-06

1// JS Tip of the Day
2// Smart use of Object Dinamic Key
3
4const hero = {
5  name: 'Batman',
6  weapon: 'Batarang'
7};
8
9function removeProperty(obj = {}, property = '') {
10  const { [property]: prop, ...newObj } = obj;
11  return newObj;
12}
13
14console.log(removeProperty(hero, 'weapon'));
15// { name: 'Batman' }

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.