Skip to content

Instantly share code, notes, and snippets.

@xialvjun
xialvjun / yarn.lock
Created November 23, 2018 10:17
graphql-js-issues-1536
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
"@babel/code-frame@7.0.0", "@babel/code-frame@^7.0.0", "@babel/code-frame@^7.0.0-beta.35":
version "7.0.0"
resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.0.0.tgz#06e2ab19bdb535385559aabb5ba59729482800f8"
integrity sha512-OfC2uemaknXr87bdLUkWog7nYuliM9Ij5HUcajsVcMCpQrcLmtxRbVFTIqmcSkSeYRBFBRxs2FiUqFJDLdiebA==
dependencies:
"@babel/highlight" "^7.0.0"
@xialvjun
xialvjun / react-inline-style.tsx
Created June 29, 2018 09:10
String inline style with prefix in React. DEPRECATED FOR `import {createInlineStyle} from '@xialvjun/create-react-style'`
import * as React from "react";
import { isValidElement } from "react";
import { createStyle } from "@xialvjun/create-react-style";
const StyleContext = createStyle();
function format_style(style) {
if (!style) {
return "";
}
@xialvjun
xialvjun / easy_graphql_query_client.jsx
Created April 24, 2018 07:07
an naive version of react-apollo Query
import { createContext } from '@xialvjun/create-react-context';
import { create_client } from '@xialvjun/tiny-graphql-client';
import React, { Component } from 'react';
const tiny_client = create_client(async (body, extra_headers) => {
const res = await fetch('http://127.0.0.1:3000/graphql', {
method: 'post',
body: JSON.stringify(body),
headers: {
import React, { Component } from 'react';
import { render } from 'react-dom';
var data = [
['1','1.1','1.1.1'],
['1','1.1','1.1.2'],
['1','1.1','1.1.3'],
['1','1.2','1.2.1'],
['1','1.2','1.2.2'],
['2','2.1','2.1.1', '2.1.1.1'],
@xialvjun
xialvjun / setIn.js
Last active February 23, 2021 18:23
immutable version of lodash/set
// https://github.com/lodash/lodash/issues/1696
import {clone, setWith, curry} from 'lodash/fp';
// export const setIn = curry((path, value, obj) =>
// setWith(clone, path, value, clone(obj)),
// );
export const setIn = curry((obj, path, value) =>
setWith(clone, path, value, clone(obj)),
);
@xialvjun
xialvjun / StackRoutes.jsx
Last active January 3, 2018 16:10
StackRoutes, make the router stacks like the modal, just agree with the history.
import React from 'react';
import { render } from 'react-dom';
import { BrowserRouter, Switch, Route, Link, withRouter, matchPath } from 'react-router-dom';
const db = {
authors: [
{ id: '8', name: 'xialvjun', gender: 'M', },
{ id: '1', name: 'lilei', gender: 'M' },
{ id: '7', name: 'hanmeimei', gender: 'F' },
// 返回一个可以任意执行,但内部并发只有 count 的函数
function limit(fn, count) {
let n = 0;
let ps = [];
function work(...args) {
n++;
let rp = fn(...args);
rp.then(_ => {
n--;
let next = ps.shift();// ps[0];
@xialvjun
xialvjun / safe_add.js
Last active January 4, 2018 14:45
javascript safe add two float without worrying about float precision
// 其实这并不 safe。。。因为 js 中所有的数字其实都是浮点数,哪怕整数也是浮点数。。。
// 数值比较小的整数可能看不出来浮点数的特性,但是当数值比较大时,就能看出来了
// 还是直接用库来的方便实在:https://github.com/defunctzombie/num
function safe_add(a, b) {
const decimal_length = Math.max(...[a, b].map(n => (n + '').split('.')).map(n => (n[1] || '').length));
const power = Math.pow(10, decimal_length);
// 不要把 a*power+b*power 变为 (a+b)*power
return Math.round(a * power + b * power) / power;
}
@xialvjun
xialvjun / koa-dataloader.js
Last active December 5, 2017 05:46
just a sample to show how to implement a js dataloader like facebook/dataloader
const DataLoader = require('dataloader');
const { knex } = require('./singleton.js');
function middleware(ctx, next) {
const target = {};
const handler = {
get: function (receiver, table_name$key_column_name$) {
if (receiver[table_name$key_column_name$]) {
return receiver[table_name$key_column_name$];
@xialvjun
xialvjun / Promise.some.js
Created October 25, 2016 15:01
Like Array.some and on the contrast of Promise.race, there is a Promise.some
function some_sync(...ps) {
return (async () => {
let errors = []
while (true) {
try {
return await ps[errors.length]
} catch (error) {
errors.push(error)
if (errors.length === ps.length) {
throw errors