Skip to content

Instantly share code, notes, and snippets.

View DerekSeverson's full-sized avatar

Derek Severson DerekSeverson

  • Greater Milwaukee Area, WI
View GitHub Profile
@sindresorhus
sindresorhus / esm-package.md
Last active May 30, 2025 12:32
Pure ESM package

Pure ESM package

The package that linked you here is now pure ESM. It cannot be require()'d from CommonJS.

This means you have the following choices:

  1. Use ESM yourself. (preferred)
    Use import foo from 'foo' instead of const foo = require('foo') to import the package. You also need to put "type": "module" in your package.json and more. Follow the below guide.
  2. If the package is used in an async context, you could use await import(…) from CommonJS instead of require(…).
  3. Stay on the existing version of the package until you can move to ESM.
@jbroadice
jbroadice / unpkg-npm.html
Last active September 29, 2021 15:38
unpkg.com JavaScript async module import
<!DOCTYPE html>
<html>
<head>
<title>unpkg.com dynamic import module test</title>
</head>
<body>
<script type="module">
window.module = {
set exports(module) {
window.module.modules.push(module);
@bvaughn
bvaughn / updating-external-data-when-props-changes-using-promises.js
Last active June 16, 2024 21:56
Example for loading new external data in response to updated props
// This is an example of how to fetch external data in response to updated props,
// If you are using an async mechanism that does not support cancellation (e.g. a Promise).
class ExampleComponent extends React.Component {
_currentId = null;
state = {
externalData: null
};
@jbroadice
jbroadice / save-data-as-attachment.js
Last active September 29, 2021 15:38
JavaScript util to saveData (download as a browser attachment), taking data and allowing output filename.
const saveDataAsAttachment = (() => {
const a = document.createElement('a');
a.style = 'display: none';
document.body.appendChild(a);
return (data, fileName) => {
const blob = new Blob([data], {type: 'octet/stream'});
const url = window.URL.createObjectURL(blob);
a.href = url;
a.download = fileName;
@augustine-tran
augustine-tran / pagination.js
Created February 15, 2017 16:35
Adding pagination to knex.js
var Knex = require('knex');
var Promise = require('bluebird');
Object.assign(Knex.Client.prototype.QueryBuilder.prototype, {
paginate(per_page, current_page) {
var pagination = {};
var per_page = +per_page || 8;
var page = +current_page || 1;
if ( page < 1 ) page = 1;
var offset = (page - 1) * per_page;
@vlucas
vlucas / encryption.ts
Last active May 8, 2025 12:51
Stronger Encryption and Decryption in Node.js
import { createCipheriv, createDecipheriv, randomBytes } from "crypto";
const ENCRYPTION_KEY: string = process.env.ENCRYPTION_KEY || ""; // Must be 256 bits (32 characters)
const IV_LENGTH: number = 16; // For AES, this is always 16
/**
* Will generate valid encryption keys for use
* Not used in the code below, but generate one and store it in ENV for your own purposes
*/
export function keyGen() {
@staltz
staltz / introrx.md
Last active May 24, 2025 19:53
The introduction to Reactive Programming you've been missing
@branneman
branneman / better-nodejs-require-paths.md
Last active May 15, 2025 11:17
Better local require() paths for Node.js

Better local require() paths for Node.js

Problem

When the directory structure of your Node.js application (not library!) has some depth, you end up with a lot of annoying relative paths in your require calls like:

const Article = require('../../../../app/models/article');

Those suck for maintenance and they're ugly.

Possible solutions

@CMCDragonkai
CMCDragonkai / angularjs_directive_attribute_explanation.md
Last active April 20, 2025 17:02
JS: AngularJS Directive Attribute Binding Explanation

AngularJS Directive Attribute Binding Explanation

When using directives, you often need to pass parameters to the directive. This can be done in several ways. The first 3 can be used whether scope is true or false. This is still a WIP, so validate for yourself.

  1. Raw Attribute Strings

    <div my-directive="some string" another-param="another string"></div>