Skip to content

Instantly share code, notes, and snippets.

@cahnory
cahnory / machine.js
Last active November 5, 2020 21:33
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
@cahnory
cahnory / machine.js
Created November 5, 2020 20:25
Generated by XState Viz: https://xstate.js.org/viz
Machine({
id: 'JSON File',
initial: 'unknown',
context: {
path: null,
content: null,
},
states: {
unknown: {
on: {
@cahnory
cahnory / findProp.js
Created March 31, 2020 10:51
Debug utils
export default (value, cb, path = '', res = {}, scannedValues=[]) => {
if (!value || typeof value !== 'object' || scannedValues.includes(value)) {
return res;
}
scannedValues.push(value);
if (Array.isArray(value)) {
value.forEach((next, index) => {
if (cb(next, index)) {
export const closestToZero = input => {
if (!(Array.isArray(input) && input.length)) {
return 0;
}
return input.reduce((prev, value) => {
const ratio = Math.abs(prev / value);
if (ratio > 1 || (ratio === 1 && value > 0)) {
return value;
@cahnory
cahnory / WebpackRelayCompilerPlugin.js
Last active March 5, 2018 09:58
WebpackRelayCompilerPlugin
const childProcess = require('child_process');
function RelayCompilerPlugin(options) {
this.schema = options.schema;
this.src = options.src;
}
RelayCompilerPlugin.prototype.apply = function(compiler) {
const cmd = `relay-compiler --src ${this.src} --schema ${this.schema}`;
@cahnory
cahnory / UserType.js
Last active February 4, 2018 10:40
Create relay node field
import { GraphQLObjectType } from 'graphql';
import { globalIdField } from 'graphql-relay';
import { nodeInterface, defineNodeType } from './node';
import { findById } from '../models/User';
export const UserType = new GraphQLObjectType({
name: 'User',
fields: {
id: globalIdField('User')
@cahnory
cahnory / subscriptionManager.js
Last active November 29, 2017 11:48
Subscription manager to quickly remove all subs (on componentWillUnmount for eg.)
export default createSubscriptionManager = () => {
const subs = []
return {
subscriptions: subs,
interval: attachSubscriptionMethod(subs, interval),
listen: attachSubscriptionMethod(subs, listen),
timeout: attachSubscriptionMethod(subs, timeout),
promise: attachSubscriptionMethod(subs, promise),
@cahnory
cahnory / README.md
Created January 27, 2017 09:08
Idée vite jetée à propos des bundlers

Utilisation d'une fonction d'import propre au bundler

Aujourd'hui, webpack nous permet d'inclure un asset comme s'il s'agissait d'un fichier javascript :

import style from './style.css'

Mais les avantages de cette approches ne sont ils pas inférieurs aux inconvéniants ?

@cahnory
cahnory / DataSelect.jsx
Last active June 21, 2016 08:08
DataSelect
import React from 'react'
export function DataSelect({
children,
onData,
onChange,
value,
defaultValue,
...props,
}) {