Skip to content

Instantly share code, notes, and snippets.

View redspirit's full-sized avatar
👌

Alexey redspirit

👌
  • Saint-Petersburg
  • 17:31 (UTC +03:00)
View GitHub Profile
@TheBrenny
TheBrenny / matchAll polyfill.js
Last active February 24, 2023 19:23
A polyfill for the String.prototype.matchAll method. Only available in Node12+ and most browsers.
if(!String.prototype.matchAll) {
String.prototype.matchAll = function (rx) {
if (typeof rx === "string") rx = new RegExp(rx, "g"); // coerce a string to be a global regex
rx = new RegExp(rx); // Clone the regex so we don't update the last index on the regex they pass us
let cap = []; // the single capture
let all = []; // all the captures (return this)
while ((cap = rx.exec(this)) !== null) all.push(cap); // execute and add
return all; // profit!
};
}
@mattdesl
mattdesl / point-to-line-2d.js
Created March 22, 2018 00:36
2D Point to Line Segment distance function
// Taken From:
// https://stackoverflow.com/questions/849211/shortest-distance-between-a-point-and-a-line-segment
function sqr (x) {
return x * x;
}
function dist2 (v, w) {
return sqr(v[0] - w[0]) + sqr(v[1] - w[1]);
}