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

hey-there

v0.0.2

Published

Separates module definitions from exporting syntax

Downloads

3

Readme

Hey There Travis node npm

A simple exporting wrapper using the best ES6 & Node 6+ has to offer.

Installation

npm install hey-there

Usage

let multiplyBy10 = require('./multiplyBy10');
let util = require('hey-there')(module);

function mainFunc (x) {
  return util.multiplyBy10(x + 1);
}

util.export(mainFunc).expose({
  multiplyBy10,
});

Disclaimer

This solution is meant to be a stop-gap between current module.exports functionality & the upcoming import export functionality. If it is underseriable to compile with Babel but using module.exports feels too limited this tool can allow you to write simpler, easily tested code without having to create unnecessary classes or objects with method properties requiring more precise syntax, nesting, and commas.

When import & export are supported natively in node this package will no longer be of use as I believe we can all agree that using built in syntax is generally the better way to go. But for now this module makes using the current node implementation a little bit better.

Problem

Have you ever written JS code that creates simple functions that call internal functions and want to stub those out for testing?

With Babel and the new module syntax using export and import it's quite trivial! As of Node 7 it's rather unwieldy to do that. For example let's say you've written:

let _ = require('highland');
let buildAssets = require('./buildAssets');
let copyAssets = require('./copyAssets');
let updateBuildEntry = require('./helpers/updateBuildEntry');

function build (stream) {
  return stream
    .through(runBuildMethod)
    .flatMap(updateBuildEntry('complete'));
}

module.exports = build;

//////////////////////////////////////////////////////////////////////////////
// HELPER METHODS
//////////////////////////////////////////////////////////////////////////////

function runBuildMethod (stream) {
  return stream.flatMap((state) => {
    let stateStream = _.of(state);

    switch (state.get('action')) {
      case 'build':
        return stateStream.through(buildAssets);

      case 'copy':
        return stateStream.through(copyAssets);
    }
  });
}

module.exports.runBuildMethod = runBuildMethod;

Now in your test you want to stub out a function like buildAssets in a mocha unit test. Well in that setup: you can't! Even if you spy on the module.exports.runBuildMethod you're just replacing the pointer and the original function will still be called.

Enter hey-there. Now we can structure our app as such:

let _ = require('highland');
let buildAssets = require('./buildAssets');
let copyAssets = require('./copyAssets');
let updateBuildEntry = require('./helpers/updateBuildEntry');
let util = require('hey-there')(module);

function build (stream) {
  return stream
    .through(util.runBuildMethod)
    .flatMap(updateBuildEntry('complete'));
}

//////////////////////////////////////////////////////////////////////////////
// HELPER METHODS
//////////////////////////////////////////////////////////////////////////////

function runBuildMethod (stream) {
  return stream.flatMap((state) => {
    let stateStream = _.of(state);

    switch (state.get('action')) {
      case 'build':
        return stateStream.through(util.buildAssets);

      case 'copy':
        return stateStream.through(util.copyAssets);
    }
  });
}

util.export(build).and.expose({
  buildAssets,
  copyAssets,
  runBuildMethod,
  updateBuildEntry,
});

Now you can spy on any of those internal functions in your unit tests without having to structure your code in an object or turn your function declarations into function expressions on the module.exports object

Spying example:

let expect = require('expect');
let build = require('../lib/build');

describe('build', () => {
  it('Should run the build pipeline', () => {
    let spy = expect.spyOn(build, 'runBuildMethod');

    expect(build()).toExist(); // is true
    expect(spy).toHaveBeenCalled(); // is true
  });
});