Skip to content

Instantly share code, notes, and snippets.

View StJohn3D's full-sized avatar
🏠
Working from home

St John Peaster StJohn3D

🏠
Working from home
View GitHub Profile
@Integralist
Integralist / regex.js
Created March 11, 2013 15:15
The difference between JavaScript's `exec` and `match` methods is subtle but important, and I always forget...
var str = "The quick brown fox jumped over the box like an ox with a sox in its mouth";
str.match(/\w(ox)/g); // ["fox", "box", "sox"]
// match (when used with a 'g' flag) returns an Array with all matches found
// if you don't use the 'g' flag then it acts the same as the 'exec' method.
str.match(/\w(ox)/); // ["fox", "ox"]
/\w(ox)/.exec(str); // ["fox", "ox"]
@tjfontaine
tjfontaine / deferrals-in-nodejs.md
Last active February 16, 2016 06:49
Description of Deferrals in Node.js

There are four kinds of deferral mechanisms in Node.js:

  • setTimeout
  • setInterval
  • setImmediate
  • process.nextTick

setTimeout and setInterval are quite familiar to those used to JavaScript in the browser and their semantics are fairly well understood. They return opaque values that can be passed to their clear counterparts, and have been around forever. setImmediate is a newer construct and its adoption in the browser is not very wide, and nextTick is a creation solely unto Node.js. The latter two are mechanisms to queue a callback in the short future, such that currently executing JavaScript may continue. If you're used to trying to do this pattern in the browser you may be used to using something like setTimeout(fn, 0).

If Node.js actually exposed the idea of a turn of the event loop, you would be expecting the scheduled callback to run at the end of the current loop, or the start of the next -- though from the perspective of your application there isn't really a difference. In

@itsgreggreg
itsgreggreg / node-tcp-chat.md
Last active February 23, 2021 14:15
Simple TCP 1 to 1 chat in Node

Simple TCP 1 to 1 chat on Node

Can be used to establish a 1 to 1 chat across any TCP network with static IP addresses, such as a LAN or WAN.

Usage

Open a terminal

  • Start the server
  • node --harmony tcp-chat-server.js
  • Give it your name
  • Wait for a connection
  • Start chatting
@rachelhyman
rachelhyman / gist:b1f109155c9dafffe618
Last active February 21, 2024 18:20
Github README anchor links

To create anchor links that jump down to different sections of a README (as in an interactive table of contents), first create a heading:
#Real Cool Heading

The anchor link for that heading is the lowercase heading name with dashes where there are spaces. You can always get the anchor name by visiting the README on Github.com and clicking on the anchor that appears when you hover to the left of the heading. Copy everything starting at the #:
#real-cool-heading

Wherever you want to link to your Real Cool Heading section, put your desired text in brackets, followed by the anchor link in parentheses:
[Go to Real Cool Heading section](#real-cool-heading)

@twolfson
twolfson / .gitignore
Last active March 27, 2020 20:29
Proof of concept to explore media keys for https://github.com/twolfson/google-music-electron/issues/31
node_modules/
@drodsou
drodsou / writeFileSyncRecursive.js
Last active June 5, 2024 15:57
Like writeFileSync but creating all folder paths if not exist
// -- updated in 2020/04/19 covering the issues in the comments to this point
// -- remember you also have things like `ensureDirSync` from https://github.com/jprichardson/node-fs-extra/blob/master/docs/ensureDir-sync.md
const fs = require('fs')
function writeFileSyncRecursive(filename, content, charset) {
// -- normalize path separator to '/' instead of path.sep,
// -- as / works in node for Windows as well, and mixed \\ and / can appear in the path
let filepath = filename.replace(/\\/g,'/');
// -- preparation to allow absolute paths as well
@lukechilds
lukechilds / get_latest_release.sh
Created August 9, 2016 19:43
Shell - Get latest release from GitHub
get_latest_release() {
curl --silent "https://api.github.com/repos/$1/releases/latest" | # Get latest release from GitHub api
grep '"tag_name":' | # Get tag line
sed -E 's/.*"([^"]+)".*/\1/' # Pluck JSON value
}
# Usage
# $ get_latest_release "creationix/nvm"
# v0.31.4
@willdages
willdages / slack-interactive-messages-api-gateway-mapping-template.vtl
Last active March 6, 2024 07:37
Slack's Interactive Messages endpoint reports a Content-Type of `application/x-www-form-urlencoded` but responds with something like looks like `payload=%7B%22actions%22%3A%5B%7B%22...`, which is encoded JSON inside the payload parameter, which takes the API Gateway VTL templates for a loop.
## The `substring(8)` returns the input with everything after "payload=" which is the only parameter passed in,
## everything else is encoded JSON inside that parameter.
#set ($encodedJSON = $input.body.substring(8))
$util.urlDecode(${encodedJSON})
@pierrejoubert73
pierrejoubert73 / markdown-details-collapsible.md
Last active June 26, 2024 06:53
How to add a collapsible section in markdown.

How to add a collapsible section in markdown

1. Example

Click me

Heading

  1. Foo
  2. Bar
    • Baz
  • Qux
@ryanburnette
ryanburnette / universal-definition-pattern.js
Last active June 1, 2021 22:02
A universal definition pattern for JavaScript libraries. Supports require(), AMD, and browser.
;(function() {
function myModule() {
}
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = myModule;
}
else {
if (typeof define === 'function' && define.amd) {
define([], function() {