Skip to content

Instantly share code, notes, and snippets.

@panuhorsmalahti
panuhorsmalahti / gist:b91b41d6802f63a2c346
Created November 4, 2014 20:40
JavaScript signed zero example
// JavaScript signed zero example.
// Note that +0 equals -0 in JavaScript.
var getPlusZero = () => +0;
var getNegativeZero = () => -0;
var isPlusZero = x => Math.atan2(0, x) === 0;
var isNegativeZero = x => Math.atan2(0, x) !== 0;
console.log(isPlusZero(getPlusZero()));
console.log(isPlusZero(getNegativeZero()));
@panuhorsmalahti
panuhorsmalahti / squeeze.js
Created May 20, 2015 17:48
Simple way to combine multiple elements in an array.
Array.prototype.squeeze = function (n, fn) {
const from = Object(this).slice();
const to = [];
while (n > 0 && from.length)
to.push(fn.apply(undefined, from.splice(0, n)));
return to;
}
var _ = require("lodash");
var performRoles = require("./performRoles.js");
performRoles({ foo: 1 });
@panuhorsmalahti
panuhorsmalahti / updated_removed.js
Last active August 29, 2015 14:21
updated() and removed()
const updated = (source, property, value) => {
const out = {};
Object.keys(source).forEach(key => {
out[key] = source[key];
});
out[property] = value;
return out;
};
const removed = (source, property) => {
@panuhorsmalahti
panuhorsmalahti / getter-shorthand.js
Created June 11, 2015 17:23
ES6 proxy property getter shorthand
const _ = new Proxy({}, { get: (target, prop) => it => it[prop] });
// Evaluates to [{ foo: true }]
[{ foo: true }, { foo: false }].filter(_.foo);
@panuhorsmalahti
panuhorsmalahti / changed.js
Last active August 29, 2015 14:25
Call a function if the parameters differ
// Calls 'f' if the parameters are different from the last call.
// NOTE: Implement 'notEqual' yourself
const changed = f => {
let lastParams = undefined, flag = false;
return (...args) => {
if (!flag || notEqual(lastParams, args)) {
f(args);
lastParams = args;
flag = true
}
@panuhorsmalahti
panuhorsmalahti / memleak-test.js
Created August 21, 2015 15:47
memleak-test.js
function f(p) {
console.log(Date.now());
var x = [];
while(x.length < 1000000) x.push(x.length);
p = p || {};
setTimeout(function() {
f(p);
}, 25);
};
@panuhorsmalahti
panuhorsmalahti / serialize.js
Last active December 4, 2015 15:58
Serialize references
function randomID() {
return Math.random();
}
function serialize(obj, store) {
const retVal = {};
store = store || {
root: retVal,
objects: {},
@panuhorsmalahti
panuhorsmalahti / constraints.js
Created March 2, 2016 17:33
Clojure-style :pre and :post constraints in ES6 - big thanks to @polytypic for the original idea!
// copied from https://gist.github.com/lauripiispanen/1fd1c3319084f9913f2d
// improved formatting
const constrain = ({
pre = () => {},
post = () => {}
}) => fn => (...args) => {
const throwIf = x => y => {
if (x(y)) {
throw x(y);
} else {
@panuhorsmalahti
panuhorsmalahti / polymer-script-type.html
Created May 13, 2016 16:51
Polymer script type example
<link rel="import" href="../polymer/polymer.html">
<dom-module id="hello-world">
<template>
<h1>Hello</h1>
</template>
</dom-module>
<script type="module">
import { assign } from "./lib/lodash/lodash.js";