Skip to content

Instantly share code, notes, and snippets.

View maxired's full-sized avatar

Maxence Dalmais maxired

View GitHub Profile
@maxired
maxired / index.mjs
Created February 10, 2025 14:44
reselect deps check
import { createSelector, createSelectorCreator, weakMapMemoize } from 'reselect'
const firstArgsMemoize = (func) => {
const internal = weakMapMemoize(func)
return (state) => {
return internal(state)
}
}
@maxired
maxired / cases.ts
Last active January 16, 2025 09:28
type recordsFunction<Input, Value> = (a: Input) => Value;
const cases = <Key extends string | number, Value>(
input: Key,
cases: Record<Key, Value | recordsFunction<Key, Value>> & { default: Value | recordsFunction<Key, Value> }
) => {
if (input in cases) {
return typeof cases[input] === 'function' ? (cases[input] as recordsFunction<Key, Value>)(input) : cases[input];
}
@maxired
maxired / blob.svg
Last active June 11, 2021 07:04
blbok
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@maxired
maxired / config-environments-development-middleware.json
Last active March 3, 2020 21:38
Strapi 3 custom middleware for partial content
{
"partial": {
"enabled": true
}
}
@maxired
maxired / index.js
Created January 22, 2019 15:00
Set if needed
export const setPath = ([key, ...next], value, obj) => {
if (next.length === 0) {
return {...obj, [key]: value };
}
return {...obj, [key]: setPath(next, value, obj[key]) };
};
export const getPath = ([key, ...next], obj) => {
if (next.length === 0) {
return obj[key];
04094e7973e1f2a7a4a27e89eaca885cf05163ae129f6e5d8246e98e405110fcb47a47a4584ff05f25434344e126c2a4c95755f50206545366e9ccb4bdbef2dab5;yddmat
@maxired
maxired / minimal-set.js
Created August 9, 2017 20:53
Minimal implementation of an immutable set
const setPath = ([key, ...next], value, obj) => {
if (next.length === 0) {
return {...obj, [key]: value };
}
return {...obj, [key]: setPath(next, value, obj[key]) };
};
const set = (path, value, obj) => setPath(path.split('.'), value, obj);
const store = createStore((state = {repoIds, reposById}, action) => {
switch (action.type) {
case UPDATE_TAG:
return set('reposById.1.tags[0]', {id: 213, text: 'Node.js'}, state);
default:
return state;
}
});
@maxired
maxired / index.es6
Last active August 5, 2017 11:21
immutable reducer
switch (action.type) {
case ADD_NEW_AVAILABLE_COLOR_TO_CAR:{
const { color, model, manufacturer } = action.payload
return {...state, manufacturer: {
...state.manufacturer, [manufacturer]:
{...state.manufacturers[manufacturers], models:
{...state.manufacturers[manufacturers].models, [model]:
{...state.manufacturers[manufacturers].models[model], options:
{...state.manufacturers[manufacturers].models[model].options, colors:
{...state.manufacturers[manufacturers].models[model].options.colors, [color]: true}