Skip to content

Instantly share code, notes, and snippets.

View vsemozhetbyt's full-sized avatar

Vse Mozhe Buty vsemozhetbyt

View GitHub Profile
@azu
azu / Medium: remove location hash.user.js
Created December 2, 2015 15:05
Medium: remove location hash
// ==UserScript==
// @name Medium: remove location hash
// @namespace http://efcl.info/
// @description Remove location hash from medium
// @include https://medium.com/*#*
// @version 1
// @grant none
// ==/UserScript==
function removeLocationHash(){
@ericelliott
ericelliott / class-constructor-factory-examples.js
Last active August 28, 2023 18:18
Class, Constructor, Factory
// class
class ClassCar {
drive () {
console.log('Vroom!');
}
}
const car1 = new ClassCar();
car1.drive();
@chicoxyzzy
chicoxyzzy / results
Created January 19, 2017 01:21
Benchmarking for loops
Now using node v4.7.2 (npm v2.15.11)
// v8 4.5.103.43 (Node.js 4.7.2)
forVar_______: 2ms
forLet_______: 13ms
forOfVar_____: 66ms
forOfLetConst: 64ms
forEachVar___: 15ms
forEachLet___: 21ms

Motivation

  • expression-oriented programming one of the great advances of FP
  • expressions plug together like legos, making more malleable programming experience in-the-small

Examples

Write in an expression-oriented style, scoping variables as locally as possible:

@myshov
myshov / function_invocation.js
Last active January 21, 2024 15:14
11 Ways to Invoke a Function
console.log(1);
(_ => console.log(2))();
eval('console.log(3);');
console.log.call(null, 4);
console.log.apply(null, [5]);
new Function('console.log(6)')();
Reflect.apply(console.log, null, [7])
Reflect.construct(function(){console.log(8)}, []);
Function.prototype.apply.call(console.log, null, [9]);
Function.prototype.call.call(console.log, null, 10);
// Add flag 'g' if it isn’t there, yet
// Solution 1
let cloneFlags = regExp.flags;
if (!cloneFlags.includes('g')) {
cloneFlags += 'g';
}
// Solution 2
const cloneFlags = regExp.flags.includes('g')
// From callbacks to Promises to async functions
function callbackFunc(x, callback) {
f1(x, (err1, result1) => {
if (err1) {
console.error(err1);
callback(err1);
return;
}
f2(result1, (err2, result2) => {
const assert = require('assert');
//========== Helper functions
/**
* Resolves after `ms` milliseconds
*/
function delay(ms) {
return new Promise((resolve, _reject) => {
setTimeout(resolve, ms);
@brunnerh
brunnerh / xpath.js
Created May 3, 2019 18:11
Iterable document.evaluate wrapper.
/**
* Executes an XPath expression returning an iterable node list.
* @param {string} expression XPath expression.
* @param {Node} node Optional context node for query. Default is document element.
* @param {XPathNSResolver | ((prefix: string) => string | null) | null} nsResolver Optional namespace resolver function.
* @returns {Iterator<Node>} Result nodes iterator.
*/
function* xpath(expression, node = document.documentElement, nsResolver = null)
{
const xPathResult = document.evaluate(

Summary: .global (/g) and .sticky (/y)

/g /y /yg
.exec() at .lI or later at .lI same as /y
.test() at .lI or later at .lI same as /y
.replace() ignores & resets .lI at .lI /g w/o gaps
.replaceAll() ignores .lI TypeError /g w/o gaps
.search() no effect no effect no effect
.match() Array of group 0 captures like .exec() /g w/o gaps