Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

helpers

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

helpers - npm Package Compare versions

Comparing version 0.0.1 to 0.0.3

t2.js

142

node-helpers.js

@@ -1,47 +0,121 @@

/// <reference path="../nodelib/node.js" />
var path = require('path');
var path = require('path');
var defaultLibDir = 'lib';
module.exports = function (filepath, _exports) {
/// <summary>Helper functions for requiring Node.js modules.</summary>
/// <param name="filepath" type="String">The filepath of the current module.</param>
/// <param name="_exports" type="Object">The _exports property of the current module.</param>
/// <returns type="Object">Helper-methods object.</returns>
/**
* Helper functions for requiring Node.js modules.
* @param {String} dirname directory of the calling script.
* @param Object _exports exports object of the calling script.
* @param {String} [libDir] relative path to a library subdir.
* @returns {Object} Helper-methods object.
*/
module.exports = function(dirname, _exports, libDir) {
var dirname = filepath || process.cwd();
libDir = libDir || defaultLibDir;
function submodule(name) {
/// <summary>Require a local Node.js module file.</summary>
/// <param name="name" type="String">The filename of the module.</param>
/// <returns type="Object">The Node.js module.</returns>
return require(path.resolve(dirname + path.sep + name));
function getRelativePath() {
var args = Array.prototype.slice.call(arguments);
args.unshift(dirname);
return path.join.apply(path, args);
}
/**
* Require a local Node.js module file relative to dirname
* @returns {Object} The module.
*/
function requireSubModule() {
var subpath = getRelativePath.apply(null, arguments);
return require(subpath);
}
function exp(name) {
/// <summary>Export a local module file as a property.</summary>
/// <param name="name" type="String">The filename of the module.</param>
_exports[name] = submodule(name);
/*
* Require a relative path module and export it as a property.
*/
function exportSubModule() {
var args = Array.prototype.slice.call(arguments);
var name = camelCase(args.join('-').replace(/\.| /));
_exports[name] = requireSubModule.apply(null, args);
}
function mixin(name, excludeNs) {
/// <summary>Mixin a local module file's methods with root namespace.</summary>
/// <param name="name" type="String">The filename of the module.</param>
/// <param name="excludeNs" type="Boolean" optional="true">Whether to export module as a namespace also (default is true).</param>
var sub = submodule(name);
for (var key in sub) {
if (sub.hasOwnProperty(key)) {
_exports[key] = sub[key];
}
}
if (!excludeNs) {
// include methods in namespaced module
exp(name);
}
/**
* adds a relative module's properties to a module's exports
*/
function imports() {
var args = Array.prototype.slice.call(arguments);
var subModule = requireSubModule.apply(null, args);
_exports = extend(_exports, subModule);
return _exports;
}
/*
* Export a local module file from 'lib' subdir as a property.
*/
function exportLibSubModule() {
var args = Array.prototype.slice.call(arguments);
var name = args.length ? camelCase(args.join('-')
.replace(/\.| /)) : 'lib';
args.unshift(dirname, libDir);
var subpath = path.join.apply(path, args);
_exports[name] = require(subpath);
}
/**
* adds a relative module's properties to a module's exports
*/
function importsFromLib() {
var args = Array.prototype.slice.call(arguments);
args.unshift(libDir);
var subModule = requireSubModule.apply(null, args);
_exports = extend(_exports, subModule);
return _exports;
}
return {
sub: submodule,
exp: exp,
mixin: mixin
relpath: getRelativePath,
libpath: getRelativePath.bind(null, libDir),
require: requireSubModule,
exports: exportSubModule,
imports: imports,
lib: {
require: requireSubModule.bind(null, libDir),
exports: exportLibSubModule,
// exports: exportSubModule.bind({ fromLib: true }),
imports: importsFromLib
}
// ,
// importsLib: mixin.bind(null, 'lib')
};
};
/**
* Convert string with dashes to camelCase string
* @param str {String} A string that may contain dashes
* @returns {String} A camelCase string
*/
function camelCase(str) {
var result = str.replace(/-([a-z])/ig, function(word, letter) {
return letter.toUpperCase();
});
return result;
}
/**
* Extends target object with source object
* @param target {Object} target object to extend
* @param target {Object} source object
* @returns {Object} extended target object
*/
function extend(target, source) {
for (var key in source) {
if (source.hasOwnProperty(key)) {
target[key] = source[key];
}
}
return target;
}
{
"name": "helpers",
"description": "helper functions for Node.js",
"version": "0.0.1",
"author": "Mashdraggin",
"version": "0.0.3",
"author": "Nathan Cartwright",
"license": "MIT",

@@ -16,4 +16,4 @@ "main": "node-helpers",

"devDependencies": {
"should": "*",
"mocha": "*"
"should": "*",
"mocha": "*"
},

@@ -20,0 +20,0 @@ "readme": "### helpers\nhelper functions for Node.js\n####installation\nnpm install helpers",

### node-helpers
some simple functions for Node.js to help in normalizing requiring modules across platforms and when run from external scripts such as testing frameworks or npm.
<<<<<<< HEAD
Node.js methods to help in getting paths relative to current script and in requiring relative modules
#### why
Running "npm test" does not resolve '.' correctly on my windows x64 pc and I got tired of copying and pasting the boilerplate in this little module just to be able to require my submodules easily. This module is only (possibly) relevant if you care whether your tests run properly on Windows or not and want to save some minor repetitive effort.
When using relative paths that use '.' the paths are sometimes not resolved correctly if the current working directory is not the same as the script that requires the relative path. The solution is to use __dirname. This is not really an issue when requiring modules, as require is very robust in Node.js, however functionality for requiring relative paths and from a 'lib' subdir are included to help reduce boilerplate code and generally speed up development time for my projects. This is mostly seen when working with many and/or deeply nested paths in a module. For just a few require statements this module isn't really helpful as it will not save that much in terms of repetitive coding. However, to keep the examples succint, they are of course contrived and as such may not be very illustrative of if and when the usefulness of this module becomes more apparent.
=======
Some simple methods to require and export Node.js modules from a 'lib' subdirectory.
#### installation
#### why
npm install helpers
Using '.' when requiring relative paths does not always work correctly when running a node.js script from a cwd other than the one the original script is in, e.g. if running
npm test
in the project root, require statements with '.' in the paths will fail in Windows. This being the case, I wrote my code to require modules something like
>>>>>>> b3b6d9bcebd0e7d6755b00ed998270531e6fa2ba
#### usage
var path = require('path');
var lib1 = require(path.join(__dirname, 'lib', 'lib1');
var h = require('helpers')(__dirname, exports);
<<<<<<< HEAD
npm install helpers
=======
As I began to use a standard structure for Node.js projects that had the main entrypoint consisting only of lines of code to require and exporting library modules, where the real code resides, e.g. suppose you have a directory structure like
>>>>>>> b3b6d9bcebd0e7d6755b00ed998270531e6fa2ba
#### methods
index.js
/lib
- lib1.js
- lib2.js
- lib3.js
<<<<<<< HEAD
suppose you have a directory structure like
<pre>/lib
<pre>
/lib
- index.js
- myLib1.js
- myLib2.js
- foo.js
- bar.js
</pre>
then the following examples could be taken from index.js:
and assuming this module is required like so
##### sub
require a module by name relative to the current module's directory
<pre>
var myLib1 = h.sub('myLib1');
// myLib1 = whatever myLib1.js is exporting
</pre>
var h = require('helpers')(__dirname, exports);
##### exp
then the following method examples would apply
##### require
require relative modules
var lib = h.require('lib'),
foo = h.require('lib/foo'),
bar = h.require('lib/bar');
##### exports
export a module by name relative to the current module's directory
<pre>
h.exp('myLib1');
// exports.myLib1 = whatever myLib1.js is exporting
</pre>
normally the code for this might be something like
##### mixin
export the properties of a module within the current module
<pre>
h.mixin('myLib1');
// basically the same as if you cut and paste myLib1.js into the current module
</pre>
exports.lib = require(__dirname + '/lib');
exports.libFoo = require(__dirname + '/lib/foo');
exports.libBar = require(__dirname + '/lib/bar');
// etc ad nauseum...
If the above is not clear take a look at the test.js file. It is short and easy to understand.
using the helpers exports method reduces this to
h.exports('lib')
h.exports('lib/lib1')
h.exports('lib/lib2')
The above examples are equivalent. Dash, slash, space, and '.' separated file-names will be converted to camelCase, e.g.
h.exports('foo/bar/buz')
will result in the buz module being exported as a property with identifier 'fooBarBuz' from the current module
##### imports
extends the properties of a module's exports object with the exported properties of a module at a relative path to the current script
h.imports('lib')
if the **lib** module had two exported properties 'foo' & 'bar', the current module will now be exporting those properties in addition to any existing and later exports, if any.
####tests
To run the tests you will need to have mocha installed, then just cd into the dir node-helpers is installed and type:
<pre>mocha</pre>
More detailed examples can be seen in the test/test.js file. If you wish to actually run the tests, cd into the dir node-helpers is installed in. If you do not already have mocha, chai, and should.js installed globally, you can install them by typing
npm install
although for mocha, at least, it is recommended that it be installed globally, so you can simply type
mocha
to run the tests.
=======
my index.js file began to always look something like
var path = require('path');
exports.lib1 = require(path.join(__dirname, 'lib', 'lib1');
exports.lib2 = require(path.join(__dirname, 'lib', 'lib2');
exports.lib3 = require(path.join(__dirname, 'lib', 'lib3');
// etc. ad nauseum
In my continuing effort to speed up coding I wrote this little module so that my index.js files now look like
var h = require('helpers')(__dirname, exports);
h.exp('lib1');
h.exp('lib2');
h.exp('lib3');
Its faster for me to type, quicker for me to scan, and works (even on Windows). There are two other usage methods as well - **sub** and **mixin**, which can be used as follows
// to simply require a module in relative 'lib' subdir
var lib1 = h.sub('lib1');
// to add every exported member of a submodule in lib to the current script's export object
h.mixin('lib1');
The above, or various combinations thereof, gives me enough flexibility to very quickly write my index.js entry-point for a Node.js code library in every scenario I've run into so far.
**tl;dr** - it saves typing and just works
#### installation
npm install helpers
>>>>>>> b3b6d9bcebd0e7d6755b00ed998270531e6fa2ba
#### license: MIT
see LICENSE.txt for more info
see LICENSE.txt for more info

@@ -10,31 +10,114 @@ var path = require('path');

describe('node-helpers', function () {
describe('node-helpers', function() {
var h = nodeHelpers(__dirname, exports);
var lib = h.lib;
it('has a function to require submodules relative to the current module by name', function () {
h.sub('fixtures').test.should.equal('test-value');
});
describe('methods for resolving relative filepaths', function() {
it('has a function to export a submodule namespace by name', function () {
h.exp('fixtures');
exports.fixtures.test.should.equal('test-value');
delete exports.fixtures;
it('can resolve a filepath relative to the calling scripts path', function() {
h.relpath('lib').should.equal(path.join(__dirname, 'lib'));
h.relpath('lib', 'sub').should.equal(path.join(__dirname, 'lib', 'sub'));
h.relpath('lib/sub').should.equal(path.join(__dirname, 'lib/sub'));
});
it('can resolve a filepath relative to a library subdir', function() {
h.libpath().should.equal(path.join(__dirname, 'lib'));
h.libpath('sub').should.equal(path.join(__dirname, 'lib', 'sub'));
h.libpath('sub/lib2.js').should.equal(path.join(__dirname, 'lib/sub/lib2.js'));
});
it('can resolve a filepath relative to a specified library subdir', function() {
// default is 'dir', give it a relative path of ..
var rel2 = nodeHelpers(__dirname, exports, '..');
rel2.libpath().should.equal(path.join(__dirname, '..'));
rel2.libpath('sub').should.equal(path.join(__dirname, '..', 'sub'));
rel2.libpath('sub/lib2.js').should.equal(path.join(__dirname, '../sub/lib2.js'));
});
});
it('has a function to export submodule methods with current module methods and as a namespace', function () {
h.mixin('fixtures');
exports.test.should.equal('test-value');
exports.fixtures.test.should.equal('test-value');
exports.test.should.equal(exports.fixtures.test);
delete exports.fixtures;
delete exports.test;
describe('methods for relative modules', function() {
it('has a method to require submodules relative to the current module', function() {
h.require('lib').sub.should.equal('index.js');
h.require('lib', 'lib1').sub1.should.equal('lib1.js');
h.require('/lib/lib1').sub1.should.equal('lib1.js');
h.require('lib', 'sub', 'lib2').sub2.should.equal('lib2.js');
h.require('/lib/sub/lib2').sub2.should.equal('lib2.js');
h.require('..').should.equal(nodeHelpers);
});
it('has a method to export a submodule relative to the current module', function() {
h.exports('lib');
exports.lib.sub.should.equal('index.js');
delete exports.lib;
h.exports('lib', 'lib1');
exports.libLib1.sub1.should.equal('lib1.js');
delete exports.libLib1;
h.exports('lib', 'sub', 'lib2');
exports.libSubLib2.sub2.should.equal('lib2.js');
delete exports.libSubLib2;
});
it('has a method to extend exports with relative submodule methods', function() {
h.imports('lib');
h.imports('lib', 'lib1');
h.imports('lib', 'sub', 'lib2');
exports.sub.should.equal('index.js');
exports.sub1.should.equal('lib1.js');
exports.sub2.should.equal('lib2.js');
delete exports.sub;
delete exports.sub1;
delete exports.sub2;
});
});
it('has a function to export submodule methods with current module methods but not as a namespace', function () {
h.mixin('fixtures', true);
exports.test.should.equal('test-value');
expect(exports.fixtures).to.be.undefined;
describe('has methods for modules in a lib subdirectory', function() {
it('has a method to require a module from a lib subdirectory', function() {
lib.require().sub.should.equal('index.js');
lib.require('lib1').sub1.should.equal('lib1.js');
lib.require('sub', 'lib2').sub2.should.equal('lib2.js');
lib.require('sub/lib2').sub2.should.equal('lib2.js');
});
it('has a method to export a module from a lib subdirectory', function() {
lib.exports();
lib.exports('lib1');
lib.exports('sub', 'lib2');
exports.lib.sub.should.equal('index.js');
exports.lib1.sub1.should.equal('lib1.js');
exports.subLib2.sub2.should.equal('lib2.js');
delete exports.lib;
delete exports.lib1;
delete exports.subLib2;
});
it('has a method to extend exports with module in lib subdirectory', function() {
lib.imports();
lib.imports('lib1');
lib.imports('sub', 'lib2');
exports.sub.should.equal('index.js');
exports.sub1.should.equal('lib1.js');
exports.sub2.should.equal('lib2.js');
delete exports.lib;
delete exports.sub1;
delete exports.sub2;
});
});
})
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc