Skip to content

Instantly share code, notes, and snippets.

View debonx's full-sized avatar
🍕
Food processing

Emanuele De Boni debonx

🍕
Food processing
View GitHub Profile
@debonx
debonx / node-process-object.js
Created January 19, 2020 19:04
Node: Accessing the process object.
let initialMemory = process.memoryUsage().heapUsed;
let word = process.argv[2];
console.log(`Your word is ${word}`)
// Create a new array
let wordArray = [];
// Loop 1000 times, pushing into the array each time
for (let i = 0; i < 1000; i++){
@debonx
debonx / myNodeWebsite.html
Last active January 19, 2020 19:00
Node: Create an HTTP server rendering a simple HTML app.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>My Node Server</title>
</head>
@debonx
debonx / node-writable-stream.js
Created January 19, 2020 18:39
Node: Writable stream, to get data from file and write on another.
const readline = require('readline');
const fs = require('fs');
// Create interface with a reading stream
const myInterface = readline.createInterface({
input: fs.createReadStream('shoppingList.txt')
});
// Create a write stream to custom file
const fileStream = fs.createWriteStream('shoppingResults.txt.');
@debonx
debonx / node-readable-stream.js
Last active January 19, 2020 18:26
Node: Readable stream, to execute code sequentially using readline and fs core modules.
const readline = require('readline');
const fs = require('fs');
const myInterface = readline.createInterface({
input: fs.createReadStream('shoppingList.txt')
});
const printData = (data) => {
console.log(`Item: ${data}`);
}
@debonx
debonx / app.js
Created January 11, 2020 12:00
JavaScript: number guesser game with Node.js.
let {testNumber} = require('./game.js');
process.stdout.write("I'm thinking of a number from 1 through 10. What do you think it is? \n(Write \"quit\" to give up.)\n\nIs the number ... ");
let playGame = (userInput) => {
let input = userInput.toString().trim();
testNumber(input);
};
process.stdin.on('data', playGame);
@debonx
debonx / cat.js
Created January 11, 2020 11:00
Javascript: random Dog - Cat fight.
module.exports = class Cat {
constructor(name, clawStrength) {
this.name = name;
this.clawStrength = clawStrength;
}
};
@debonx
debonx / react-componentdidupdate.js
Created January 4, 2020 11:51
React: Updating and Unmounting lifecycle methods.
import React from 'react';
export class Example extends React.component {
componentDidUpdate(prevProps, prevState) {
alert('Component is done rendering!');
}
render() {
return <h1>Hello world</h1>;
}
@debonx
debonx / react-container-component.js
Created January 4, 2020 11:17
React: Container components from presentational components.
/* Separating container components from presentational components is a popular React programming pattern.
Here’s the basic idea behind it: if a component has to have state, make calculations based on props, or manage any other complex logic, then that component shouldn’t also have to render HTML-like JSX.
Instead of rendering HTML-like JSX, the component should render another component. It should be that component’s job to render HTML-like JSX.
Following this pattern separates your business logic from your presentational logic. */
import React from 'react';
import ReactDOM from 'react-dom';
import { GuineaPigs } from '../components/GuineaPigs';
const GUINEAPATHS = [
@debonx
debonx / react-stateless-functional-component.js
Created January 2, 2020 19:00
React: Stateless functional component example.
// Normal way to display a prop:
export class MyComponentClass extends React.Component {
render() {
return <h1>{this.props.title}</h1>;
}
}
// Stateless functional component way to display a prop:
export const MyComponentClass = (props) => {
return <h1>{props.title}</h1>;
@debonx
debonx / react-proptypes.js
Last active January 2, 2020 17:21
React: How to set propTypes stateless component.
import React from 'react';
export class BestSeller extends React.Component {
render() {
return (
<li>
Title: <span>
{this.props.title}
</span><br />