Skip to content

Instantly share code, notes, and snippets.

@mvictorl
mvictorl / insertScript.js
Created July 11, 2022 06:40
Insert <script> block into template.html BEFORE </body> tag (Node.js)
const fsp = require('fs').promises
const readFilePath = './template.html'
const writeFilePath = './index.html'
const block = `
<script>
const eventSource = new EventSource('http://localhost:3000/subscribe')
eventSource.onerror = () => { console.log('Error with live-server') }
eventSource.onmessage = () => { window.location.reload() }
@mvictorl
mvictorl / sleep.js
Created October 1, 2020 07:54
JS function sleep() vice setTimeout() [return Promise]
const sleep = ms => {
return new Promise(resolve => {
setTimeout(() => resolve(), ms)
})
}
sleep(3000).then(() => console.log('After 3 seconds...'))
@mvictorl
mvictorl / ObjToArr.js
Last active August 3, 2020 06:22
Array from Object of objects (handling fetrch answer)
const respose = axios.get(`http://url.com/api/users`) // async/await required
const payload = Object.keys( respose.data || {} ).map( key => {
return {
...respose.data[key],
id: key,
}
})
/*
@mvictorl
mvictorl / index.html
Created May 10, 2020 15:03
Animated Input Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Animated Input Example</h1>
<div class="form">