Skip to content

Instantly share code, notes, and snippets.

@hax
hax / readonly.js
Created December 4, 2018 10:32
readonly collections
void function () {
'use strict'
const targetSymbol = Symbol()
function createSafeMethod(method) {
return new Proxy(method, {
apply(target, thisArg, argArray) {
return Reflect.apply(target, thisArg[targetSymbol], argArray)
}
// See https://github.com/nodejs/node/issues/6456
import {platform} from 'os'
import {satisfies} from 'semver'
if (platform() === 'darwin' && satisfies(process.version, '>=6.0.0 <6.2.1')) {
for (const s of [process.stdout, process.stderr]) {
if (s && s.isTTY && s._handle && typeof s._handle.setBlocking === 'function') {
s._handle.setBlocking(true)
}
@hax
hax / index.js
Created December 7, 2016 19:32
log with calling location info in Node.js
const {main} = require('./main')
main()
@hax
hax / test-block-scope-performance.js
Created April 30, 2016 08:46
block scope optimization test
'use strict'
const Benchmark = require('benchmark')
function MASK() {
return 100
}
function MAX() {
@hax
hax / hi-bot.js
Created January 5, 2016 09:29
detect robot
void function () {
if (!window.tracker) {
return;
}
var events = ['scroll', 'click', 'mousemove', 'keydown', 'mousedown'];
for (var i = 0; i < events.length; i++) {
addHandler(document, events[i], detect);
}
function detect(event) {
for (var i = 0; i < events.length; i++) {
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
// See https://github.com/promises-aplus/promises-spec/issues/179
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
// See https://github.com/promises-aplus/promises-spec/issues/179
@hax
hax / dabblet.css
Last active August 29, 2015 14:13
Untitled
header {
color: white;
background: navy;
height: 60px;
position: fixed;
left: 0;
right: 0;
top: 0;
}
@hax
hax / safe_json_encode.php
Last active May 13, 2021 08:30
PHP safe json encode
<?php
function safe_json_encode($data) {
// We might have been tolerant to some common cases such as convert
// INF/NAN as 0 by using JSON_PARTIAL_OUTPUT_ON_ERROR option, but
// sadly `json_last_error()` only get the last error means it may
// override worse errors such as malfored utf-8 which we can't ignore!
// Poor H P !!
$result = @json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
@hax
hax / koa-confusing-accessors.js
Last active August 29, 2015 14:00
confusing accessors in koa
// See https://github.com/koajs/koa/issues/215
// start: nvm run 0.11 --harmony koa-confusing-accessors
// test: curl -d '{"test":"ok"}' -H 'Content-Type: application/json' http://localhost:3000/
var koa = require('koa');
var app = module.exports = koa();
app.use(function *(next){
if (this.method === 'POST') {
this.body = '<?xml version="1.0">'