Nosferatu's Pace Maker

    provide-id-gen

    1.0.0 • Public • Published

    provide-id-gen

    build status npm version npm downloads

    Tiny provider factory for generating IDs.

    Table of contents

    1. Installation
    2. Usage
    3. Example
    4. Real world example
    5. Protip

    Installation

    npm install provide-id-gen --save
    

    Usage

    Pass any set of keys (an array of strings) to the provideIdGen function and it will return a provider with genId action(s) and newId reducer(s) based on each key. Each key is usually some provider key for which you would like to generate IDs.

    Example

    // src/providers/idGen.js
     
    import provideIdGen from 'provide-id-gen';
     
    const idGen = provideIdGen([
      'foo',
      'bar'
    ]);
     
    export default idGen;

    You'll then have a provider with the following actions:

    • genFooId - Increments and returns the current fooId.
    • genBarId - Increments and returns the current barId.

    And reducers:

    • newFooId - The current new fooId.
    • newBarId - The current new barId.

    Real world example

    See Lumbur's user login component.

    Protip

    On the server, you should probably set isGlobal to true. This will ensure the same provider instance is used across multiple requests and prevent any race conditions with replication if/when some ID of the same key is generated for more than one request at almost the exact same time.

    Install

    npm i provide-id-gen

    DownloadsWeekly Downloads

    15

    Version

    1.0.0

    License

    MIT

    Last publish

    Collaborators

    • timbur