Git Product home page Git Product logo

narr's Introduction

narr Build Status Coverage Status

Creates an array of size N, filled with undefined. Useful for applying higher order functions (e.g. map, reduce) to produce dense arrays of size N.

For some reason when I made originally this, I thought the method I used wouldn't iterate N times in order to fill the array. Turns out that wasn't the case, and it actually used the call stack to produce the array. That had some inherent limitations. So, I'm now using a loop to fill the array. It's still fast, but it's basically the same as a million other packages that do the same thing. Since I wrote some tests and docs, I'll just leave it up.

Install

$ npm install narr --save

Usage Examples

Create an array of random integers

import narr from "narr"

const randomInt = (min, max) => Math.floor(Math.random() * (max - min)) + min

const randArry = narr(10).map(() => randomInt(1,100))
// [ 37, 30, 57, 44, 50, 42, 99, 78, 29, 8 ]

Create a string of random characters

import narr from "narr"

const randomInt = (min, max) => Math.floor(Math.random() * (max - min)) + min
const characters = "abcdefghijklmonpqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

const id = narr(20).reduce((carry) => carry + characters.substr(randomInt(0,characters.length),1), "")
// tTz4PNUrvFWSjUDY0qn8

Why?

So, why not just do something like this:

new Array(20).map(() => Math.random())

The array created using new Array(20) has a length of 20, but it doesn't have any elements. So, you can't map or reduce over it. See test.js for some counter-examples.

License

MIT © Cully Larson

narr's People

Watchers

Cully Larson 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.