Skip to content

Instantly share code, notes, and snippets.

@ljharb
ljharb / array_iteration_thoughts.md
Last active April 29, 2024 17:13
Array iteration methods summarized

Array Iteration

https://gist.github.com/ljharb/58faf1cfcb4e6808f74aae4ef7944cff

While attempting to explain JavaScript's reduce method on arrays, conceptually, I came up with the following - hopefully it's helpful; happy to tweak it if anyone has suggestions.

Intro

JavaScript Arrays have lots of built in methods on their prototype. Some of them mutate - ie, they change the underlying array in-place. Luckily, most of them do not - they instead return an entirely distinct array. Since arrays are conceptually a contiguous list of items, it helps code clarity and maintainability a lot to be able to operate on them in a "functional" way. (I'll also insist on referring to an array as a "list" - although in some languages, List is a native data type, in JS and this post, I'm referring to the concept. Everywhere I use the word "list" you can assume I'm talking about a JS Array) This means, to perform a single operation on the list as a whole ("atomically"), and to return a new list - thus making it mu

@Snugug
Snugug / detect.js
Last active January 24, 2017 21:54
Feature Detection!
(function () {
'use strict';
var detect = function detect(property, value) {
if (window.CSS && window.CSS.supports) {
return window.CSS.supports(property, value);
}
else {
return false;
}
@jatubio
jatubio / .php_cs
Created June 13, 2015 11:51
Config file to pass PHP-CS-Fixer with Laravel 5.1 custom and PSR-2 styles coding
<?php
$finder = Symfony\Component\Finder\Finder::create()
->notPath('bootstrap/cache')
->notPath('storage')
->notPath('vendor')
->in(__DIR__)
->name('*.php')
->ignoreDotFiles(true)
->ignoreVCS(true);
@paulirish
paulirish / bling.js
Last active May 1, 2024 19:56
bling dot js
/* bling.js */
window.$ = document.querySelectorAll.bind(document);
Node.prototype.on = window.on = function (name, fn) {
this.addEventListener(name, fn);
}
NodeList.prototype.__proto__ = Array.prototype;
@nolanlawson
nolanlawson / promises_answer_sheet.md
Last active July 26, 2022 08:02
Promises puzzle cheat sheet
@sebmarkbage
sebmarkbage / Enhance.js
Last active January 31, 2024 18:33
Higher-order Components
import { Component } from "React";
export var Enhance = ComposedComponent => class extends Component {
constructor() {
this.state = { data: null };
}
componentDidMount() {
this.setState({ data: 'Hello' });
}
render() {
@getify
getify / gist:661f707f05e2743d00d2
Created February 10, 2015 23:27
param spread helper
// thanks to @raganwald
function spread(fn) {
return Function.apply.bind( fn, null );
}
function foo(x,y,z) {
console.log(x,y,z);
}
function bar(fn) {
@ericelliott
ericelliott / essential-javascript-links.md
Last active May 7, 2024 01:25
Essential JavaScript Links
@rauschma
rauschma / PreventUnknownGet.js
Last active August 12, 2021 16:07
Prevent getting of unknown properties via ES6 proxies
// The following code is valid ECMAScript 6, but doesn’t work in Firefox, yet
function PreventUnknownGet() {
}
PreventUnknownGet.prototype = new Proxy(Object.prototype, {
get(target, propertyKey, receiver) {
if (!(propertyKey in target)) {
throw new TypeError('Unknown property: '+propertyKey);
}
// Make sure we don’t block access to Object.prototype
@jcgregorio
jcgregorio / How_to_use.html
Last active July 17, 2023 14:44
HTML Templating using the HTML <template> element and exactly 100 lines of JS. A cleaned up version of this code is now available at https://github.com/jcgregorio/stamp/.
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
<script src="templating.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<template id=t>