Skip to content

Instantly share code, notes, and snippets.

View ephys's full-sized avatar
🌸

Alyx ephys

🌸
View GitHub Profile
@ephys
ephys / class_merge.js
Last active March 2, 2016 18:47
ES6 class mixin
(function () {
'use strict';
function mergeClass(receivingClass, givingPrototype) {
if (givingPrototype === Object.prototype) {
return;
}
// fetch inherited properties
const givingPrototypeProto = Object.getPrototypeOf(givingPrototype);
@ephys
ephys / findtext.js
Created May 6, 2016 00:43
Creates a Range around a piece of text in a DOM tree.
(function () {
'use strict';
/*
Result:
========= NEW TEST =========
VM1096:14 Before wrap: <p><strong>Hello</strong> <em>World</em></p>
VM1096:19 Wrapping: Hello World
VM1096:25 After wrap: <p><section><strong>Hello</strong> <em>World</em></section></p>
VM1096:13 ========= NEW TEST =========

List of things I want in EcmaScript

Frozen Object literal

Flow syntax {| <data> |} could be used in EcmaScript too to mean Object.freeze({ <data> })

Reflect.isOfBuiltinType(obj, type)

Or maybe called crossRealmnInstanceof. The objective is to have a way to determine if an object is an instance of a built-in even cross-realmns.

// new RegExp('/test/i')
// is interpreted as /\/test\/i/
// this function interprets it as
// /test/i
function parseRegExp(regExpStr) {
if (typeof regExpStr !== 'string') {
throw new TypeError('Invalid argument: not a string.');
}
const start = regExpStr.indexOf('/');
@ephys
ephys / ES-Proposal-Default-Operator.md
Last active September 24, 2016 14:37
EcmaScript - Undefined Coalesce Operator / Default Operator Proposal

EcmaScript Default Operator / Undefined Coalesce Operator

Overview and Motivation

EcmaScript 2015 introduced a new mechanism that enabled developers to define the default values of their function parameters.
Though it is powerful, that mechanism often cannot be used when setting the default values of the properties of received object arguments without destructuring them.

One such case would be a method having to pass the argument to another method :*

class A {
// Remove comments from JSON with this JavaScript RegExp
// Only strips out /* */ style comments
const stripJsonRegExp = /(?=([^"\\]*(\\.|"([^"\\]*\\.)*[^"\\]*"))*[^"]*$)\/\*(.|\n)*?\*\/(?=([^"\\]*(\\.|"([^"\\]*\\.)*[^"\\]*"))*[^"]*$)/gm;
// Usage:
const commentedJson = String.raw`
/* test 1 */
{ /* test2 */

Loading a styleSheet via HTMLLinkElement

The creation process of CSSStyleSheets is different in almost all browsers:

  • Chrome sets HTMLLinkElement.sheet once the sheet is loaded, it is null before that.
  • Firefox sets HTMLLinkElement.sheet immediately but accessing its properties throws an InvalidAccessError.
  • Edge sets HTMLLinkElement.sheet immediately, and accessign its properties return default values
    • CSSStyleSheet.cssRules is empty.
    • CSSStyleSheet.cssText is an empty string.
const [first, second, ...middle, beforeLast, last] = anIterable;

At any step, if the iterator reaches the end of its available values, stop the algorithm.

  1. set the value of middle to a new empty array.
  2. for each variable from the first to middle excluded,
    1. iterate anIterable.
    2. set the value of the variable to the result of the iteration.

My ideal HTTP server

Using ESNext to make building http server less tedious:

  • Add Promise and async/await support.
  • Use decorators to set the routes metadata rather than writing the same boilerplate in every route handler.
  • Use Flow or TypeScript for type inference (maybe).

Promises and Async/Await

Keybase proof

I hereby claim:

  • I am ephys on github.
  • I am ephys (https://keybase.io/ephys) on keybase.
  • I have a public key whose fingerprint is 30A2 38AC 14C8 6ACA 4323 1ECA A6BA 3549 E92A 7A6A

To claim this, I am signing this object: