Git Product home page Git Product logo

repure's Introduction

repure

中文文档

A replacement for reselect and providing a more simple, more natural way of writing. Same parameters will get same result, so the function which wrapped by repure will cache the result. If the same parameter is transmitted next time, the cached results will be used.

Get Start

install :npm install repure

import repure from 'repure'

function f(a, b) {
  console.log('invoke f, will takes 5 seconds')
  return a + b
}

const rf = repure(f)

rf(1, 1) // takes 5 seconds
rf(1, 1) // takes 0 seconds
rf(1, 1) // takes 0 seconds
rf(1, 1) // takes 0 seconds
rf(2, 2) // takes 5 seconds

API

repure provide 3 API: repureCreator(cacheSize, equalityCheck), repure, repureOneCacheCreator(equalityCheck)

repureCreator(cacheSize, equalityCheck)

the result of repureCreator is a repure and the wrapped functions could cache cacheSize results. we will use equalityCheck to compare whether or not the parameters are equal.

Default equalityCheck:

function defaultEqualityCheck(a, b) {
    return a === b
}

Another common equalityCheck is shallowEqual: import { shallowEqual } from 'repure'

repure

In most cases, you should use this method. cacheSize = 1 and equalityCheck is ===. the same with createSelector provided by reselct

repureOneCacheCreator(equalityCheck)

repureOneCacheCreator is optimized version of repureCreator when cacheSize is 1.

Also provide a method of batch operation: batchRepure(obj, repure)

import * as fobj from './xx' // file export func

const fobj2 = batchRepure(obj, repure)

Composing

function f(a, b) {
    ...
}
const rf = repure(f)

function g(a, b, c){
    const r = rf(a, b)
    return r * c
}
const rg = repure(g)

rg(1, 1, 2)
rg(1, 1, 2)
rg(1, 1, 3)

repure's People

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  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.