Parent Modules
Get all parent modules
Useful when you want to get all parent modules including the module which your script lives in.
Install
$ npm install parent-modules --save
Know more
Assume that we have three nested modules into /home/hastijs
directory. That means we require module three into module two and module two into module one.
/home/hastijs/
|_ module-one.js
|_ module-two.js
|_ module-three.js
Now we run module-one
while parent-modules
called into module-three
.
// module-three.jsconsole;
The result is:
//=> [ '/home/hastijs/module-three.js', '/home/hastijs/module-two.js', '/home/hastijs/module-one.js' ]
Usage
In all examples bellow, module-one executed.
Get parents IDs
// module-two.js (path: /home/hastijs/module-two.js)const parentModules = ;console;
// module-one.js (path: /home/hastijs/module-one.js);//=> [ '/home/hastijs/module-two.js', '/home/hastijs/module-one.js' ]
Get parents names
// module-two.js (path: /home/hastijs/module-two.js)const parentModules = ;const options = type: 'names';console;
// module-one.js (path: /home/hastijs/module-one.js);//=> [ 'module-tow', 'module-one' ]
Get parents names by index
// module-three.js (path: /home/hastijs/module-three.js)const parentModules = ;const options = type: 'names' indexes: 0 2;console;
// module-two.js (path: /home/hastijs/module-tow.js);
// module-one.js (path: /home/hastijs/module-one.js);//=> [ 'module-three', 'module-one' ]
Search and get parents name->id (complex) into an index range
// module-three.js (path: /home/hastijs/module-three.js)const parentModules = ;const options = type: 'complex' find: /module-t/ indexRange: 1 2;console;
// module-two.js (path: /home/hastijs/module-tow.js);
// module-one.js (path: /home/hastijs/module-one.js);//=> [ 'module-two': '/home/hastijs/module-tow.js' ]
API
parentModules(options)
options
Type: object
Options to filter result (parent modules).
type
Type: string
Default: ids
You can use ids
, names
and complex
.
find
Type: string
| object
Search the result using a string or RegExp.
indexes
Type: array
List of parent module indexes which we want to get.
indexRange
Type: array
Range of parent module indexes which we want to get.
Contributing
Everyone is very welcome to contribute to Parent Modules project. Parent Modules is a HastiJS project so please see HastiJS contributing guidelines before contributing.
License
MIT © HastiJS