Comparing version 1.0.1 to 1.0.2
208
factories.js
var internal = require('./internal'), | ||
grequire = require('./grequire'); | ||
grequire = require('./grequire'), | ||
master; | ||
// Dependency Injection registration | ||
module.exports = function(thisObj){ | ||
return { | ||
Constant : Constant.bind(thisObj), | ||
Service : Service.bind(thisObj), | ||
Factory : Factory.bind(thisObj), | ||
Enum : Enum.bind(thisObj), | ||
File : File.bind(thisObj), | ||
Folder : Folder.bind(thisObj), | ||
NodeModule : NodeModule.bind(thisObj) | ||
var register = function(){ | ||
return register.Factory.apply(thisObj, arguments); | ||
}; | ||
register.Factory = Factory.bind(thisObj); | ||
register.Constant = Constant.bind(thisObj); | ||
register.Service = Service.bind(thisObj); | ||
register.Enum = Enum.bind(thisObj); | ||
register.File = File.bind(thisObj); | ||
register.Folder = Folder.bind(thisObj); | ||
register.NodeModule = NodeModule.bind(thisObj); | ||
return register; | ||
}; | ||
// ----------------------------------------- | ||
// Return an object | ||
@@ -22,3 +27,3 @@ function Constant(name, obj){ | ||
} | ||
// ----------------------------------------- | ||
// Return a new instance | ||
@@ -40,2 +45,6 @@ function Service(name, dependencies, fn, singleton){ | ||
if(fn.extend && fn.Register && fn.Register.Factory){ | ||
return jaas.call(this, name, dependencies, fn, singleton); | ||
} | ||
if (dependencies){ | ||
@@ -55,3 +64,51 @@ dependencies = [].concat(dependencies); | ||
} | ||
// ----------------------------------------- | ||
function jaas(name, dependencies, fn, singleton){ | ||
// Assuming the parameters have been validated and sorted already | ||
dependencies = dependencies ? [].concat(dependencies) : []; | ||
dependencies.unshift('$namedParameters'); | ||
function instantiator(){ | ||
var args = Array.from(arguments); | ||
var params = {}; | ||
// Get factory dependencies | ||
var i = 1; | ||
dependencies.forEach(function(key, index){ | ||
if (index === 0){return;} | ||
if (typeof key === 'object'){ | ||
Object.keys(key).forEach(function(key2){ | ||
var val = args[i++]; | ||
if (val !== undefined){ | ||
params[key2] = val; | ||
} | ||
}); | ||
}else{ | ||
var val = args[i++]; | ||
if (val !== undefined){ | ||
params[key] = val; | ||
} | ||
} | ||
}); | ||
// Get named dependencies | ||
if (args[0] && args[0]){ | ||
Object.keys(args[0]).forEach(function(key){ | ||
var val = args[0][key]; | ||
if (val !== undefined){ | ||
params[key] = args[0][key]; | ||
} | ||
}); | ||
} | ||
// Instantiate the class | ||
return new fn(params); | ||
} | ||
return this.Register.Factory(name, dependencies, instantiator, singleton); | ||
} | ||
// ----------------------------------------- | ||
// Return a factory function | ||
@@ -99,3 +156,3 @@ function Factory(name, dependencies, fn, singleton){ | ||
} | ||
// ----------------------------------------- | ||
function File(name, path){ | ||
@@ -105,4 +162,8 @@ var file; | ||
} | ||
function Folder(path){ | ||
// ----------------------------------------- | ||
function Folder(path, opt){ | ||
if (typeof opt === 'object'){ | ||
return smartfolder.call(this, path, opt); | ||
} | ||
if (this._folders.indexOf(path) === -1 ){ | ||
@@ -114,3 +175,118 @@ this._folders.push(path); | ||
} | ||
// ----------------------------------------- | ||
function smartfolder(path, opt){ | ||
var self = this; | ||
var glob = require('glob'); | ||
var Path = require('path'); | ||
opt = { | ||
type : opt.type || 'auto', | ||
singleton : opt.singleton, | ||
prefix : opt.prefix, | ||
suffix : opt.suffix, | ||
prefixFolder : opt.prefixFolder !== undefined ? opt.prefixFolder : true, | ||
pattern : opt.pattern || '**/*.js', | ||
transform : opt.transform, | ||
register : opt.register | ||
}; | ||
if (!opt.transform){ | ||
opt.transform = function(file, folders){ | ||
// Build up the name of the dependency | ||
var result = []; | ||
if (opt.prefix){ | ||
result.push(toPascal(opt.prefix)); | ||
} | ||
if (opt.prefixFolder){ | ||
result.push(toPascal(folders)); | ||
} | ||
if (file !== 'index'){ | ||
result.push(toPascal(file)); | ||
} | ||
if (opt.suffix){ | ||
result.push(toPascal(opt.suffix)); | ||
} | ||
result = result.join(''); | ||
return result.length ? | ||
result[0].toLowerCase() + result.substring(1) : ''; | ||
}; | ||
} | ||
if (!opt.register){ | ||
opt.register = function(name, obj){ | ||
// Register the dependency | ||
switch (opt.type.toLowerCase()){ | ||
case 'factory': | ||
if (typeof obj !== 'function'){ | ||
throw new Error('Factory type expected but got ' + typeof obj); | ||
} | ||
self.Register.Factory(name, null, obj, opt.singleton); | ||
break; | ||
case 'service': | ||
if (typeof obj !== 'function'){ | ||
throw new Error('Service type expected but got ' + typeof obj); | ||
} | ||
self.Register.Service(name, null, obj, opt.singleton); | ||
break; | ||
case 'constant': | ||
self.Register.Constant(name, obj); | ||
break; | ||
case 'enum': | ||
self.Register.Enum(name, obj); | ||
break; | ||
case 'auto': | ||
if (typeof obj === 'function'){ | ||
self.Register.Factory(name, null, obj, opt.singleton); | ||
}else{ | ||
self.Register.Constant(name, obj); | ||
} | ||
break; | ||
} | ||
}; | ||
} | ||
// Get the absolute path of the search folder | ||
var root = Path.resolve(path); | ||
// Retrieve all files that match the pattern | ||
glob.sync(opt.pattern, {cwd : root}) | ||
.map(function(file){ | ||
// Get the name of the dependency | ||
var info = Path.parse(file); | ||
var folders = info.dir ? info.dir.split('/') : []; | ||
var ext = info.ext; | ||
var base = info.name; | ||
var name = opt.transform.call(self, base, folders, ext); | ||
return { | ||
path : [path, file].join('/'), | ||
file : base, | ||
name : name | ||
}; | ||
}) | ||
.filter(function(file){ | ||
// Remove blank/uncalculated dependencies | ||
return !!file.name; | ||
}) | ||
.forEach(function(file){ | ||
// Grap the content of the file and register it | ||
var content = grequire(file.path); | ||
opt.register.call(self, file.name, content); | ||
}); | ||
return self; | ||
} | ||
function toPascal(arr){ | ||
return [] | ||
.concat(arr) | ||
.map(function(folder){ | ||
return folder[0].toUpperCase() + folder.substring(1).toLowerCase(); | ||
}) | ||
.join(''); | ||
} | ||
// ----------------------------------------- | ||
function NodeModule(name){ | ||
@@ -120,3 +296,3 @@ var file; | ||
} | ||
// ----------------------------------------- | ||
function Enum(name, value){ | ||
@@ -123,0 +299,0 @@ var self = this; |
10
index.js
@@ -16,3 +16,4 @@ var internal = require('./internal'), | ||
static : null, | ||
dependencies : [] | ||
dependencies : [], | ||
bindToInstance : false | ||
}; | ||
@@ -47,2 +48,9 @@ | ||
if (opt.bindToInstance){ | ||
var bindParameters = newClass.NamedParameters(args, namedParameters); | ||
Object.keys(bindParameters).forEach(key => { | ||
this[key] = bindParameters[key]; | ||
}); | ||
} | ||
//Run constructor | ||
@@ -49,0 +57,0 @@ if (typeof(opt.constructor) === 'function'){ |
@@ -83,2 +83,5 @@ var statics = require('./statics'), | ||
} | ||
if (name === '$namedParameters'){ | ||
return namedParameters; | ||
} | ||
@@ -85,0 +88,0 @@ // Named parameters |
{ | ||
"name": "jpex", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Javascript Prototype Extension", | ||
@@ -15,3 +15,6 @@ "main": "index.js", | ||
"author": "Jack Ellis", | ||
"license": "ISC" | ||
"license": "ISC", | ||
"dependencies": { | ||
"glob": "^7.0.5" | ||
} | ||
} |
@@ -153,3 +153,4 @@ JPEX - Javascipt Protoype Extension | ||
*(Name, Deps, Fn, Singleton)* | ||
Fn is an instantiatable function. Services are similar to angular services except that they are not singletons by default. | ||
Fn is an instantiatable function. Services are similar to angular services except that they are not singletons by default. | ||
It is also possible to register another jpex class as a service. | ||
```javascript | ||
@@ -229,4 +230,45 @@ var MyClass = jpex.extend(function(myService){ | ||
#####Folders (advanced) | ||
*(Path, Options)* | ||
It is also possible to have the folder factory automatically register all files in a folder. This means you can organise your application in a logical manner and then have all dependencies automatically loaded and injected into your jpex classes. | ||
The Options parameter takes the following options: | ||
######type | ||
The type of dependency you want to register them as. If not specified it will register all functions as factories and anything else as a constant. Valid values are *'Factory', 'Service', 'Constant', 'Enum'* | ||
######singleton | ||
Whether or not to create a singleton if the files are services or factories. | ||
######prefix | ||
Aprefix to add to the start of each dependency | ||
######suffix | ||
Asuffix to add to the end of each dependency | ||
######prefixFolder | ||
When adding sub folders, this will prepend the folder name to the dependency, so *main* folder with a *sub.js* file will be registered as *mainSub*, for example. | ||
######pattern | ||
A glob pattern to match by. This defaults to `'**/*.js'` | ||
######transform | ||
*(file, folders, ext)* | ||
A function that will transform the filename into the name of the dependency. This is entirely optional and will be handled automatically using the above option values if omitted. | ||
The function takes 3 parameters: the file name (without the folders or file extension), an array of folders leading to the file, and the file extension (.js). | ||
The value returned by this function will be used as the dependency name. | ||
######register | ||
Another optional function that takes the name of the dependency and the contents of the related file and registers the dependency against the class. | ||
```javascript | ||
var MyClass = jpex.extend(function(myserviceModel, subfolderServiceModel){ | ||
}); | ||
MyClass.Register.Folder('models', { | ||
type : 'Service', | ||
suffix : 'model' | ||
}); | ||
//where there is a folder structure: | ||
// models | ||
// myservice.js | ||
// subfolder | ||
// service.js | ||
``` | ||
#####Node_module | ||
*(name)* | ||
*(name)* | ||
If all of the above fail, it will attempt to load the dependency from node_modules. This includes anything in the node_modules folder and global modiles like fs and path. | ||
@@ -233,0 +275,0 @@ To avoid the overhead of checking all factories and folders before resorting to this method, you can manually register a node_module. |
415
34320
1
9
588
+ Addedglob@^7.0.5
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addedfs.realpath@1.0.0(transitive)
+ Addedglob@7.2.3(transitive)
+ Addedinflight@1.0.6(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedminimatch@3.1.2(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedpath-is-absolute@1.0.1(transitive)
+ Addedwrappy@1.0.2(transitive)