JavaScript
31.9K subscribers
1.01K photos
9 videos
33 files
693 links
A resourceful newsletter featuring the latest and most important news, articles, books and updates in the world of #javascript πŸš€ Don't miss our Quizzes!

Let's chat: @nairihar
Download Telegram
CHALLENGE

const weakSet = new WeakSet();

const obj1 = { name: 'First' };
const obj2 = { name: 'Second' };
const obj3 = obj1;

weakSet.add(obj1);
weakSet.add(obj2);

let result = '';
result += weakSet.has(obj1) + ', ';
result += weakSet.has(obj3) + ', ';

obj2.name = 'Modified';
result += weakSet.has(obj2) + ', ';

weakSet.delete(obj1);
result += weakSet.has(obj3);

console.log(result);
❀7πŸ”₯1
πŸ‘€ npq: Safely Install Packages by Auditing Them Pre-Install

npq performs several extra steps compared to npm. It consults Snyk’s database of vulnerabilities, looks at the package’s age, download count, and docs, and tries to paint a better picture of what you’re really installing.

Liran Tal
Please open Telegram to view this post
VIEW IN TELEGRAM
πŸ”₯5πŸ‘4❀2
CHALLENGE

class LightMachine {
constructor() {
this.states = {
green: { next: 'yellow' },
yellow: { next: 'red' },
red: { next: 'green' }
};
this.currentState = 'green';
}

transition() {
this.currentState = this.states[this.currentState].next;
return this.currentState;
}
}

const lightMachine = new LightMachine();
let result = '';
for (let i = 0; i < 5; i++) {
result += lightMachine.transition() + ' ';
}
console.log(result.trim());
❀4πŸ‘1
πŸ˜‚
Please open Telegram to view this post
VIEW IN TELEGRAM
🀣61πŸ”₯4❀2πŸ‘2
CHALLENGE

async function processValues() {
try {
console.log('Start');
const a = await Promise.resolve('First');
console.log(a);
const b = await Promise.reject('Error');
console.log(b);
return 'Done';
} catch (err) {
console.log(err);
return 'Recovered';
} finally {
console.log('Finally');
}
}

processValues().then(result => console.log(result));
❀1
CHALLENGE

function greet(name) {
return `Hello, ${name}!`;
}

function highlight(strings, ...values) {
return strings.reduce((result, str, i) => {
return result + str + (values[i] ? `<em>${values[i]}</em>` : '');
}, '');
}

const user = 'Sarah';
const status = 'online';

console.log(highlight`User ${user} is currently ${status}.`);
πŸ”₯2❀1
✌️ The Many, Many, Many JavaScript Runtimes of the Last Decade

A meaty article (which took a year to put together) covering the myriad of JavaScript runtimes and engines both past and present, from obvious picks like Node.js to cloud platforms and lesser known β€˜honorable mentions’. This is a great summary to round out your JS ecosystem knowledge.

Whatever, Jamie
Please open Telegram to view this post
VIEW IN TELEGRAM
πŸ‘6❀5
CHALLENGE

class ChatServer {
constructor() {
this.clients = new Set();
this.messageLog = [];
}

connect(client) {
this.clients.add(client);
return () => this.clients.delete(client);
}

broadcast(message, sender) {
this.messageLog.push(message);
this.clients.forEach(client => {
if (client !== sender) {
client.receive(message);
}
});
}
}

const server = new ChatServer();
const david = { name: 'David', receive: msg => console.log(`David got: ${msg}`) };
const sarah = { name: 'Sarah', receive: msg => console.log(`Sarah got: ${msg}`) };
const emma = { name: 'Emma', receive: msg => console.log(`Emma got: ${msg}`) };

const disconnectDavid = server.connect(david);
server.connect(sarah);
server.connect(emma);
server.broadcast('Hello everyone!', david);
disconnectDavid();
server.broadcast('Is David still here?', sarah);

console.log(server.clients.size);
❀2πŸ‘2
CHALLENGE

class VideoCall {
constructor() {
this.pc = { iceConnectionState: 'new' };
this.streams = [];
}

async connect() {
this.pc.iceConnectionState = 'checking';
await Promise.resolve();
this.pc.iceConnectionState = 'connected';
this.streams.push('remote-video');
return this.pc.iceConnectionState;
}

handleConnectionChange() {
const states = ['new', 'checking', 'connected'];
return states.map(state => {
return this.pc.iceConnectionState === state ? `Status: ${state}` : null;
}).filter(Boolean);
}
}

const call = new VideoCall();
call.connect().then(() => {
console.log(call.handleConnectionChange());
});
❀6
🀟 AudioTee.js: macOS System Audio Capture for Node.js

Wrapping around an (included) Swift-powered binary, this captures Mac system audio output and emits it as PCM encoded chunks at regular intervals. GitHub repo.

Nick Payne
Please open Telegram to view this post
VIEW IN TELEGRAM
❀4πŸ‘2πŸ”₯1
CHALLENGE

const compose = (...fns) => x => fns.reduceRight((acc, fn) => fn(acc), x);

const addPrefix = str => `prefix_${str}`;
const capitalize = str => str.charAt(0).toUpperCase() + str.slice(1);
const addSuffix = str => `${str}_suffix`;

const process = compose(addSuffix, capitalize, addPrefix);

console.log(process('data'));
❀3
❀5πŸ‘2πŸ”₯2
Please open Telegram to view this post
VIEW IN TELEGRAM
1❀1πŸ”₯1
CHALLENGE

const Flyable = {
fly() { return `${this.name} is flying`; }
};

const Swimmable = {
swim() { return `${this.name} is swimming`; }
};

function createDuck(name) {
return Object.assign({ name }, Flyable, Swimmable);
}

const duck = createDuck('Quackers');
console.log(duck.fly());
console.log(duck.swim());
console.log(Object.getOwnPropertyNames(duck));
❀3