Git Product home page Git Product logo

selektor's Introduction

Selektor

Create memoized selectors, an alternative to reselect.

Usage

import { createSelector, pipe, combine } from '@nilscox/selektor';

const state = {
  todos: [
    { id: 1, completed: true },
    { id: 2, completed: false },
  ],
  currentTodoId: 2,
};

type State = typeof state;

// create a memoized selector
const selectTodos = createSelector((state: State) => state.todos);

// create a new selector from another selector's output
const selectFirstTodo = pipe(selectTodos, (todos) => todos[0]);

selectFirstTodo(state); // { id: 1, completed: true }

// introduce extra parameters
const selectFilteredTodos = pipe(selectTodos, (todos, completed: boolean) => {
  return todos.filter((todo) => todo.completed === completed);
});

selectFilteredTodos(state, false); // [{ id: 2, completed: false }]

const selectCurrentTodoId = createSelector((state) => state.currentTodoId);

// combine outputs from multiple selectors
const selectCurrentTodo = combine(selectTodos, selectCurrentTodoId, (todos, currentTodoId) => {
  return todos.find((todo) => todo.id === currentTodoId);
});

selectCurrentTodo(state); // { id: 2, completed: false }

Installation

<your package manager's install command> @nilscox/selektor

API

createSelector(fn)

Creates a memoized selector. When the selector is called, the function fn is called and its returned value is cached. If the selector is called again with the same parameters, fn is not called and the cached value is returned.

const state = { foo: 42 };
const selectFoo = createSelector((state) => state.foo);

selectFoo(state); // 42

pipe(input, output)

Create a selector from another selector's output, optionally adding parameters.

const state = { foo: { bar: 42 } };
const selectFoo = createSelector((state) => state.foo);
const selectBar = pipe(selectFoo, (foo) => foo.bar);

selectBar(state); // 42

const selectBarPlusNum = pipe(selectFoo, (foo, num) => foo.bar + num);

selectBarPlusNum(state, 9); // 51

combine(...inputs, output)

Combine multiple selector's outputs into a new selector. The results of the input selectors will be given as parameters to the output selector.

const state = { foo: 42, bar: 51 };
const selectFoo = createSelector((state) => state.foo);
const selectBar = createSelector((state) => state.bar);
const selectForPlusBar = combine(selectFoo, selectBar, (foo, bar) => foo + bar);

selectFooPlusBar(state); // 93

Custom memoization function

The default memoization only remembers the last call of the memoized function, similar to memoize-one. Use createPipe and createCombine to create custom pipe and combine functions with a custom memoization function.

function memoize(fn) {
  return (...params) => {
    // custom memoization logic
  };
}

const pipe = createPipe(memoize);
const combine = createCombine(memoize);

selektor's People

Contributors

nilscox avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.