Skip to content

Instantly share code, notes, and snippets.

Avatar

cahnory

  • Marseille, France
View GitHub Profile
View findProp.js
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)) {
View index.js
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 Mar 5, 2018
WebpackRelayCompilerPlugin
View WebpackRelayCompilerPlugin.js
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 Feb 4, 2018
Create relay node field
View UserType.js
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 Nov 29, 2017
Subscription manager to quickly remove all subs (on componentWillUnmount for eg.)
View subscriptionManager.js
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 Jan 27, 2017
Idée vite jetée à propos des bundlers
View README.md

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 ?

View gist:6d47f34f1ce37380f45816426f7517ea
View DataSelect.jsx
import React from 'react'
export function DataSelect({
children,
onData,
onChange,
value,
defaultValue,
...props,
}) {
@cahnory
cahnory / Walker_Nav_Menu_Object.php
Created Feb 18, 2016
Wordpress nav menu object
View Walker_Nav_Menu_Object.php
<?php
class Walker_Nav_Menu_Object extends Walker_Nav_Menu
{
protected $scopes = array();
public function __construct() {
$this->scopes[] = (object)array(
'children' => array(),
'parent' => NULL
@cahnory
cahnory / middleware.js
Created Dec 21, 2015
koa-webpack-middleware
View middleware.js
import connect from 'koa-connect';
import compose from 'koa-compose';
import webpack from 'webpack';
import webpackDev from 'webpack-dev-middleware';
import webpackHot from 'webpack-hot-middleware';
export default function (config = {}) {
let compiler = webpack(config);
return compose([
You can’t perform that action at this time.