Skip to content

Instantly share code, notes, and snippets.

HE Shi-Jun hax

Block or report user

Report or block hax

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View node6-stdout-patch.js
// 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 / safe_json_encode.php
Last active Sep 4, 2018
PHP safe json encode
View safe_json_encode.php
<?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 / readonly.js
Created Dec 4, 2018
readonly collections
View readonly.js
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)
}
@hax
hax / hi-bot.js
Created Jan 5, 2016
detect robot
View hi-bot.js
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++) {
@hax
hax / json-slashs-hint.md
Last active Feb 3, 2020
JSON `\//` Hint
View json-slashs-hint.md

JSON \// Hint

Problems

JSON.stringify({x: 1234567890n}) // throw!

You can custom it

View tree-module.js
// 根据 https://www.zhihu.com/question/391388615/answer/1193135808 中的代码重构,
// 以对应所 fork 的工业聚版本
// 和工业聚版本的主要区别:
// - OO 范式而不是 FP 范式(其实像知乎原版那样不用 class 写成 makeTree 函数也是可以的,这里
// 主要是响应工业聚的号召,更「class」一些。但因为是 JS 而不是 TS 写的,就没有再加额外的
// interface 或 abstract class 了。)
// - Tree 没有弄成 immutable 的。要弄成 immutable 也不是不行,但 OO 范式下处理这类数据结构,
// 通常不会特意弄成 immutable 的,而是在必要的时候提供 clone 方法。
// - 简易起见,没有对 root 节点做额外处理(实际上似乎也不应该做原本格式的数据必然只有一个
@hax
hax / channel.js
Created May 13, 2020
A experimental Channel in JS
View channel.js
// kotlin channel: https://kotlinlang.org/docs/reference/coroutines/channels.html
// golang channel
class Channel {
constructor() {
this._s = null
this._r = null
}
send(v) {
if (this._r) {
@hax
hax / promise-trans.md
Last active May 19, 2020
Promise 相关译名考虑
View promise-trans.md

译名方法:

  • 黑话型(生造新词)
  • 会意型(合成新词、古词新用)
  • 假借型(复用引申已有专用名词)
  • 白话型(直接用日常用语)

术语

  • Promise
  • Status/State: pending/fulfilled/rejected, settled
  • Fate: resolved/unresolved
@hax
hax / 答工业聚.md
Created May 19, 2020
Explanation of my arrow switch (pattern match) idea
View 答工业聚.md

Rough exmaple:

switch (x) {
  case Number ?n -> console.log(n)
  case String ?s -> console.log(s)
  case Point {?x, ?y} -> console.log(x, y)
  case OtherPattern ?binding -> console.log(binding)
}
You can’t perform that action at this time.