const a = { duration: 50 };
a.duration ??= 10;
a.speed ??= 25;
console.log(a.duration); // Expected output: 50
console.log(a.speed); // Expected output: 25
Replacement for:
a.duration = a.duration ?? 10
const a = { duration: 50 };
a.duration ??= 10;
a.speed ??= 25;
console.log(a.duration); // Expected output: 50
console.log(a.speed); // Expected output: 25
Replacement for:
a.duration = a.duration ?? 10
If you have subdirectories or additional applications which have its own package.json file, you can add them via the workspaces setting to your main project.
{
"name": "my-project",
"workspaces": [
"./app/*"
]
}
When running npm install
it will now also install the dependencies of all projects in the app folder.
Use the build in function structuredClone()
to copy all kind of complex JS types.
Official documentation: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm#supported_types
Comparison to other copy techniques like spread or json.stringify: https://www.builder.io/blog/structured-clone
# Install script: https://github.com/nvm-sh/nvm#install--update-script
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
# check current used node version
node -v
# list local available node versions
nvm list
# list all released node versions
nvm ls-remote
# install specific node version and switch to it
nvm install v20.5.1
# switch to a specific node version, which is already installed
nvm use v20.5.1
https://nodejs.org/api/buffer.html
const buffer1 = Buffer.from(ArrayBuffer)
const buffer2 = Buffer.from(Uint8Array)
Using a promise
const streamToBuffer= async () => {
return new Promise(function (resolve, reject) {
const chunks = []
stream.on('data', chunk => chunks.push(chunk))
stream.on('end', () => resolve(Buffer.concat(chunks)))
stream.on("error", err => reject(err))
})
}
const buffer = await streamToBuffer()
A stream is also iterable (see here), so you can also use for await...of
(example)
const chunks = []
for await (const chunk of stream) {
chunks.push(chunk)
}
const buffer = Buffer.concat(chunks)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl
const now = Intl.DateTimeFormat('de', {
dateStyle: 'medium',
timeStyle: 'medium'
}).format( Date.now() )
console.log( now )
When using a blob or base64 data to open a file in PDF.js, the viewer is not able to get the correct filename. So when downloading the pdf using the download button inside the viewer, it will always download as document.pdf
. To manually set a filename, you can use the setTitleUsingUrl
function.
<iframe id="pdf-js-viewer" src="/pdf/web/viewer.html?file=" title="webviewer" frameborder="0" width="100%" height="700" allowfullscreen="" webkitallowfullscreen=""/>
let pdfViewerIFrame = document.getElementById("pdf-js-viewer")
pdfViewerIFrame.contentWindow.PDFViewerApplication.setTitleUsingUrl("myFilename")
More info here: https://github.com/mozilla/pdf.js/issues/10435
Using regex, you can simply reorder the parts of your date string to create a compatible ISO 8601 date string which is accepted by the new Date()
constructor.
const date_string = "20220101"
const oDate = new Date(date_string.replace(/(\d{4})(\d{2})(\d{2})/, '$1-$2-$3'))
console.log(oDate.toUTCString())
As null
belongs to the JS Falsy Values, you can simply do this:
console.log(null) // null
console.log(!!null) // false
Detailed explanation: https://www.samanthaming.com/tidbits/19-2-ways-to-convert-to-boolean/