npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fib-it

v1.1.0

Published

6 different implementation of the fibonacci sequence in JavaScript

Downloads

2

Readme

Fib-it

Fibonacci sequence implemented in 6 different ways with JavaScript. A nice excuse to explore functions, iterators, generators, iterable values, event emitters and streams!

npm version CircleCI JavaScript Style Guide

Install

As usual, npm-it-away:

npm i --save fib-it

Usage

The library exposes 6 different implementations of the fibonacci sequence.

The sequence will be infinite (as much as it can be considering JavaScript integer space) unless limited to max number.

Function-based implementation

const { genFib } = require('fib-it')

const f = genFib(6) // limit the sequence to numbers below 6
f() // 1
f() // 1
f() // 2
f() // 3
f() // 5
f() // null
f() // null

itarator-based implementation

const { genFibIterator } = require('fib-it')

it = genFibIterator(6) // { next: [Function: next] }
it.next() // { value: 1, done: false }
it.next() // { value: 1, done: false }
it.next() // { value: 2, done: false }
it.next() // { value: 3, done: false }
it.next() // { value: 5, done: false }
it.next() // { value: null, done: true }

// or

it = genFibIterator(6)
let result = it.next()
while (!result.done) {
  console.log(result.value)
  result = it.next()
}
// 1
// 1
// 2
// 3
// 5

generator-based implementation

gen = fibGenerator(6) // {}
gen.next() // { value: 1, done: false }
gen.next() // { value: 1, done: false }
gen.next() // { value: 2, done: false }
gen.next() // { value: 3, done: false }
gen.next() // { value: 5, done: false }
gen.next() // { value: null, done: true }
gen.next() // { value: undefined, done: true }

// or

gen = fibGenerator(6)
[...gen] // [ 1, 1, 2, 3, 5 ]

iterable-based implementation

const { genFibIterable } = require('fib-it')

it = genFibIterable(6) // { [Symbol(Symbol.iterator)]: [GeneratorFunction: [Symbol.iterator]] }
[...it] // [ 1, 1, 2, 3, 5 ]

// or

for (n of f) { console.log(n) }
// 1
// 1
// 2
// 3
// 5

event-emitter-based implementation

const { FibEmitter } = require('fib-it')

const fe = new FibEmitter(6, 10)
fe.on('data', n => console.log(n))
fe.on('end', () => console.log('done!'))
fe.start()
// 1
// 1
// 2
// 3
// 5
// done!

stream-based implementation

const s = new FibStream(7) // FibStream { ... }
s.on('data', chunk => console.log(chunk.readUInt32LE(0).toString()))
s.on('end', () => console.log('done!'))
// 1
// 1
// 2
// 3
// 5
// done!

When do I really use this?

There are some actual use cases for the fibonacci sequence. So maybe you need something like this in your code!

In reality this is just an experiment to compare different methodology to generate data sequences in JavaScript, so I expect it to be used mostly for education purposes rather than for production code.

Also with this goal in mind, the code is kept relatively simple and it's not meant to be feature-complete or error-proof.

Contribute

Feel more than welcome to report bugs or propose changes.

License

Licensed under MIT.