Comparing version 8.0.2 to 9.0.0
@@ -1,2 +0,2 @@ | ||
var CLASS_PATTERN = /^class[ {]/; | ||
const CLASS_PATTERN = /^class[ {]/; | ||
@@ -19,3 +19,3 @@ | ||
function isArray(obj) { | ||
return Object.prototype.toString.call(obj) === '[object Array]'; | ||
return Array.isArray(obj); | ||
} | ||
@@ -44,4 +44,3 @@ | ||
*/ | ||
function annotate() { | ||
var args = Array.prototype.slice.call(arguments); | ||
function annotate(...args) { | ||
@@ -52,4 +51,6 @@ if (args.length === 1 && isArray(args[0])) { | ||
var fn = args.pop(); | ||
args = [ ...args ]; | ||
const fn = args.pop(); | ||
fn.$inject = args; | ||
@@ -74,5 +75,5 @@ | ||
var CONSTRUCTOR_ARGS = /constructor\s*[^(]*\(\s*([^)]*)\)/m; | ||
var FN_ARGS = /^(?:async\s+)?(?:function\s*[^(]*)?(?:\(\s*([^)]*)\)|(\w+))/m; | ||
var FN_ARG = /\/\*([^*]*)\*\//m; | ||
const CONSTRUCTOR_ARGS = /constructor\s*[^(]*\(\s*([^)]*)\)/m; | ||
const FN_ARGS = /^(?:async\s+)?(?:function\s*[^(]*)?(?:\(\s*([^)]*)\)|(\w+))/m; | ||
const FN_ARG = /\/\*([^*]*)\*\//m; | ||
@@ -87,6 +88,6 @@ /** | ||
if (typeof fn !== 'function') { | ||
throw new Error('Cannot annotate "' + fn + '". Expected a function!'); | ||
throw new Error(`Cannot annotate "${fn}". Expected a function!`); | ||
} | ||
var match = fn.toString().match(isClass(fn) ? CONSTRUCTOR_ARGS : FN_ARGS); | ||
const match = fn.toString().match(isClass(fn) ? CONSTRUCTOR_ARGS : FN_ARGS); | ||
@@ -98,6 +99,6 @@ // may parse class without constructor | ||
var args = match[1] || match[2]; | ||
const args = match[1] || match[2]; | ||
return args && args.split(',').map(function(arg) { | ||
var argMatch = arg.match(FN_ARG); | ||
return args && args.split(',').map(arg => { | ||
const argMatch = arg.match(FN_ARG); | ||
return (argMatch && argMatch[1] || arg).trim(); | ||
@@ -127,3 +128,3 @@ }) || []; | ||
} else { | ||
throw error('No provider for "' + name + '"!'); | ||
throw error(`No provider for "${ name }"!`); | ||
} | ||
@@ -133,12 +134,12 @@ } | ||
var currentlyResolving = []; | ||
var providers = this._providers = Object.create(parent._providers || null); | ||
var instances = this._instances = Object.create(null); | ||
const currentlyResolving = []; | ||
const providers = this._providers = Object.create(parent._providers || null); | ||
const instances = this._instances = Object.create(null); | ||
var self = instances.injector = this; | ||
const self = instances.injector = this; | ||
var error = function(msg) { | ||
var stack = currentlyResolving.join(' -> '); | ||
const error = function(msg) { | ||
const stack = currentlyResolving.join(' -> '); | ||
currentlyResolving.length = 0; | ||
return new Error(stack ? msg + ' (Resolving: ' + stack + ')' : msg); | ||
return new Error(stack ? `${ msg } (Resolving: ${ stack })` : msg); | ||
}; | ||
@@ -156,4 +157,4 @@ | ||
if (!providers[name] && name.indexOf('.') !== -1) { | ||
var parts = name.split('.'); | ||
var pivot = get(parts.shift()); | ||
const parts = name.split('.'); | ||
let pivot = get(parts.shift()); | ||
@@ -197,8 +198,8 @@ while (parts.length) { | ||
} else { | ||
throw new Error('Cannot invoke "' + fn + '". Expected a function!'); | ||
throw error(`Cannot invoke "${ fn }". Expected a function!`); | ||
} | ||
} | ||
var inject = fn.$inject || parseAnnotations(fn); | ||
var dependencies = inject.map(function(dep) { | ||
const inject = fn.$inject || parseAnnotations(fn); | ||
const dependencies = inject.map(dep => { | ||
if (hasOwnProp(locals, dep)) { | ||
@@ -218,9 +219,9 @@ return locals[dep]; | ||
function instantiate(Type) { | ||
var def = fnDef(Type); | ||
const { | ||
fn, | ||
dependencies | ||
} = fnDef(Type); | ||
var fn = def.fn, | ||
dependencies = def.dependencies; | ||
// instantiate var args constructor | ||
var Constructor = Function.prototype.bind.apply(fn, [ null ].concat(dependencies)); | ||
const Constructor = Function.prototype.bind.apply(fn, [ null ].concat(dependencies)); | ||
@@ -231,7 +232,7 @@ return new Constructor(); | ||
function invoke(func, context, locals) { | ||
var def = fnDef(func, locals); | ||
const { | ||
fn, | ||
dependencies | ||
} = fnDef(func, locals); | ||
var fn = def.fn, | ||
dependencies = def.dependencies; | ||
return fn.apply(context, dependencies); | ||
@@ -246,5 +247,3 @@ } | ||
function createPrivateInjectorFactory(childInjector) { | ||
return annotate(function(key) { | ||
return childInjector.get(key); | ||
}); | ||
return annotate(key => childInjector.get(key)); | ||
} | ||
@@ -260,14 +259,15 @@ | ||
if (forceNewInstances && forceNewInstances.length) { | ||
var fromParentModule = Object.create(null); | ||
var matchedScopes = Object.create(null); | ||
const fromParentModule = Object.create(null); | ||
const matchedScopes = Object.create(null); | ||
var privateInjectorsCache = []; | ||
var privateChildInjectors = []; | ||
var privateChildFactories = []; | ||
const privateInjectorsCache = []; | ||
const privateChildInjectors = []; | ||
const privateChildFactories = []; | ||
var provider; | ||
var cacheIdx; | ||
var privateChildInjector; | ||
var privateChildInjectorFactory; | ||
for (var name in providers) { | ||
let provider; | ||
let cacheIdx; | ||
let privateChildInjector; | ||
let privateChildInjectorFactory; | ||
for (let name in providers) { | ||
provider = providers[name]; | ||
@@ -296,3 +296,3 @@ | ||
/* jshint -W083 */ | ||
forceNewInstances.forEach(function(scope) { | ||
forceNewInstances.forEach(scope => { | ||
if (provider[1].$scope.indexOf(scope) !== -1) { | ||
@@ -306,3 +306,3 @@ fromParentModule[name] = [ provider[2], provider[1] ]; | ||
forceNewInstances.forEach(function(scope) { | ||
forceNewInstances.forEach(scope => { | ||
if (!matchedScopes[scope]) { | ||
@@ -319,3 +319,3 @@ throw new Error('No provider for "' + scope + '". Cannot use provider from the parent!'); | ||
var factoryMap = { | ||
const factoryMap = { | ||
factory: invoke, | ||
@@ -334,6 +334,6 @@ type: instantiate, | ||
var initializers = moduleDefinition.__init__ || []; | ||
const initializers = moduleDefinition.__init__ || []; | ||
return function() { | ||
initializers.forEach(function(initializer) { | ||
initializers.forEach(initializer => { | ||
@@ -355,9 +355,9 @@ // eagerly resolve component (fn or string) | ||
var moduleExports = moduleDefinition.__exports__; | ||
const moduleExports = moduleDefinition.__exports__; | ||
// private module | ||
if (moduleExports) { | ||
var nestedModules = moduleDefinition.__modules__; | ||
const nestedModules = moduleDefinition.__modules__; | ||
var clonedModule = Object.keys(moduleDefinition).reduce(function(clonedModule, key) { | ||
const clonedModule = Object.keys(moduleDefinition).reduce((clonedModule, key) => { | ||
@@ -371,6 +371,6 @@ if (key !== '__exports__' && key !== '__modules__' && key !== '__init__' && key !== '__depends__') { | ||
var childModules = (nestedModules || []).concat(clonedModule); | ||
const childModules = (nestedModules || []).concat(clonedModule); | ||
var privateInjector = createChild(childModules); | ||
var getFromPrivateInjector = annotate(function(key) { | ||
const privateInjector = createChild(childModules); | ||
const getFromPrivateInjector = annotate(function(key) { | ||
return privateInjector.get(key); | ||
@@ -384,3 +384,3 @@ }); | ||
// ensure child injector initializes | ||
var initializers = (moduleDefinition.__init__ || []).slice(); | ||
const initializers = (moduleDefinition.__init__ || []).slice(); | ||
@@ -410,4 +410,4 @@ initializers.unshift(function() { | ||
var type = moduleDefinition[key][0]; | ||
var value = moduleDefinition[key][1]; | ||
const type = moduleDefinition[key][0]; | ||
const value = moduleDefinition[key][1]; | ||
@@ -448,7 +448,7 @@ providers[key] = [ factoryMap[type], arrayUnwrap(type, value), type ]; | ||
var initializers = moduleDefinitions | ||
const initializers = moduleDefinitions | ||
.reduce(resolveDependencies, []) | ||
.map(loadModule); | ||
var initialized = false; | ||
let initialized = false; | ||
@@ -463,5 +463,3 @@ return function() { | ||
initializers.forEach(function(initializer) { | ||
return initializer(); | ||
}); | ||
initializers.forEach(initializer => initializer()); | ||
}; | ||
@@ -468,0 +466,0 @@ } |
@@ -5,3 +5,3 @@ 'use strict'; | ||
var CLASS_PATTERN = /^class[ {]/; | ||
const CLASS_PATTERN = /^class[ {]/; | ||
@@ -24,3 +24,3 @@ | ||
function isArray(obj) { | ||
return Object.prototype.toString.call(obj) === '[object Array]'; | ||
return Array.isArray(obj); | ||
} | ||
@@ -49,4 +49,3 @@ | ||
*/ | ||
function annotate() { | ||
var args = Array.prototype.slice.call(arguments); | ||
function annotate(...args) { | ||
@@ -57,4 +56,6 @@ if (args.length === 1 && isArray(args[0])) { | ||
var fn = args.pop(); | ||
args = [ ...args ]; | ||
const fn = args.pop(); | ||
fn.$inject = args; | ||
@@ -79,5 +80,5 @@ | ||
var CONSTRUCTOR_ARGS = /constructor\s*[^(]*\(\s*([^)]*)\)/m; | ||
var FN_ARGS = /^(?:async\s+)?(?:function\s*[^(]*)?(?:\(\s*([^)]*)\)|(\w+))/m; | ||
var FN_ARG = /\/\*([^*]*)\*\//m; | ||
const CONSTRUCTOR_ARGS = /constructor\s*[^(]*\(\s*([^)]*)\)/m; | ||
const FN_ARGS = /^(?:async\s+)?(?:function\s*[^(]*)?(?:\(\s*([^)]*)\)|(\w+))/m; | ||
const FN_ARG = /\/\*([^*]*)\*\//m; | ||
@@ -92,6 +93,6 @@ /** | ||
if (typeof fn !== 'function') { | ||
throw new Error('Cannot annotate "' + fn + '". Expected a function!'); | ||
throw new Error(`Cannot annotate "${fn}". Expected a function!`); | ||
} | ||
var match = fn.toString().match(isClass(fn) ? CONSTRUCTOR_ARGS : FN_ARGS); | ||
const match = fn.toString().match(isClass(fn) ? CONSTRUCTOR_ARGS : FN_ARGS); | ||
@@ -103,6 +104,6 @@ // may parse class without constructor | ||
var args = match[1] || match[2]; | ||
const args = match[1] || match[2]; | ||
return args && args.split(',').map(function(arg) { | ||
var argMatch = arg.match(FN_ARG); | ||
return args && args.split(',').map(arg => { | ||
const argMatch = arg.match(FN_ARG); | ||
return (argMatch && argMatch[1] || arg).trim(); | ||
@@ -132,3 +133,3 @@ }) || []; | ||
} else { | ||
throw error('No provider for "' + name + '"!'); | ||
throw error(`No provider for "${ name }"!`); | ||
} | ||
@@ -138,12 +139,12 @@ } | ||
var currentlyResolving = []; | ||
var providers = this._providers = Object.create(parent._providers || null); | ||
var instances = this._instances = Object.create(null); | ||
const currentlyResolving = []; | ||
const providers = this._providers = Object.create(parent._providers || null); | ||
const instances = this._instances = Object.create(null); | ||
var self = instances.injector = this; | ||
const self = instances.injector = this; | ||
var error = function(msg) { | ||
var stack = currentlyResolving.join(' -> '); | ||
const error = function(msg) { | ||
const stack = currentlyResolving.join(' -> '); | ||
currentlyResolving.length = 0; | ||
return new Error(stack ? msg + ' (Resolving: ' + stack + ')' : msg); | ||
return new Error(stack ? `${ msg } (Resolving: ${ stack })` : msg); | ||
}; | ||
@@ -161,4 +162,4 @@ | ||
if (!providers[name] && name.indexOf('.') !== -1) { | ||
var parts = name.split('.'); | ||
var pivot = get(parts.shift()); | ||
const parts = name.split('.'); | ||
let pivot = get(parts.shift()); | ||
@@ -202,8 +203,8 @@ while (parts.length) { | ||
} else { | ||
throw new Error('Cannot invoke "' + fn + '". Expected a function!'); | ||
throw error(`Cannot invoke "${ fn }". Expected a function!`); | ||
} | ||
} | ||
var inject = fn.$inject || parseAnnotations(fn); | ||
var dependencies = inject.map(function(dep) { | ||
const inject = fn.$inject || parseAnnotations(fn); | ||
const dependencies = inject.map(dep => { | ||
if (hasOwnProp(locals, dep)) { | ||
@@ -223,9 +224,9 @@ return locals[dep]; | ||
function instantiate(Type) { | ||
var def = fnDef(Type); | ||
const { | ||
fn, | ||
dependencies | ||
} = fnDef(Type); | ||
var fn = def.fn, | ||
dependencies = def.dependencies; | ||
// instantiate var args constructor | ||
var Constructor = Function.prototype.bind.apply(fn, [ null ].concat(dependencies)); | ||
const Constructor = Function.prototype.bind.apply(fn, [ null ].concat(dependencies)); | ||
@@ -236,7 +237,7 @@ return new Constructor(); | ||
function invoke(func, context, locals) { | ||
var def = fnDef(func, locals); | ||
const { | ||
fn, | ||
dependencies | ||
} = fnDef(func, locals); | ||
var fn = def.fn, | ||
dependencies = def.dependencies; | ||
return fn.apply(context, dependencies); | ||
@@ -251,5 +252,3 @@ } | ||
function createPrivateInjectorFactory(childInjector) { | ||
return annotate(function(key) { | ||
return childInjector.get(key); | ||
}); | ||
return annotate(key => childInjector.get(key)); | ||
} | ||
@@ -265,14 +264,15 @@ | ||
if (forceNewInstances && forceNewInstances.length) { | ||
var fromParentModule = Object.create(null); | ||
var matchedScopes = Object.create(null); | ||
const fromParentModule = Object.create(null); | ||
const matchedScopes = Object.create(null); | ||
var privateInjectorsCache = []; | ||
var privateChildInjectors = []; | ||
var privateChildFactories = []; | ||
const privateInjectorsCache = []; | ||
const privateChildInjectors = []; | ||
const privateChildFactories = []; | ||
var provider; | ||
var cacheIdx; | ||
var privateChildInjector; | ||
var privateChildInjectorFactory; | ||
for (var name in providers) { | ||
let provider; | ||
let cacheIdx; | ||
let privateChildInjector; | ||
let privateChildInjectorFactory; | ||
for (let name in providers) { | ||
provider = providers[name]; | ||
@@ -301,3 +301,3 @@ | ||
/* jshint -W083 */ | ||
forceNewInstances.forEach(function(scope) { | ||
forceNewInstances.forEach(scope => { | ||
if (provider[1].$scope.indexOf(scope) !== -1) { | ||
@@ -311,3 +311,3 @@ fromParentModule[name] = [ provider[2], provider[1] ]; | ||
forceNewInstances.forEach(function(scope) { | ||
forceNewInstances.forEach(scope => { | ||
if (!matchedScopes[scope]) { | ||
@@ -324,3 +324,3 @@ throw new Error('No provider for "' + scope + '". Cannot use provider from the parent!'); | ||
var factoryMap = { | ||
const factoryMap = { | ||
factory: invoke, | ||
@@ -339,6 +339,6 @@ type: instantiate, | ||
var initializers = moduleDefinition.__init__ || []; | ||
const initializers = moduleDefinition.__init__ || []; | ||
return function() { | ||
initializers.forEach(function(initializer) { | ||
initializers.forEach(initializer => { | ||
@@ -360,9 +360,9 @@ // eagerly resolve component (fn or string) | ||
var moduleExports = moduleDefinition.__exports__; | ||
const moduleExports = moduleDefinition.__exports__; | ||
// private module | ||
if (moduleExports) { | ||
var nestedModules = moduleDefinition.__modules__; | ||
const nestedModules = moduleDefinition.__modules__; | ||
var clonedModule = Object.keys(moduleDefinition).reduce(function(clonedModule, key) { | ||
const clonedModule = Object.keys(moduleDefinition).reduce((clonedModule, key) => { | ||
@@ -376,6 +376,6 @@ if (key !== '__exports__' && key !== '__modules__' && key !== '__init__' && key !== '__depends__') { | ||
var childModules = (nestedModules || []).concat(clonedModule); | ||
const childModules = (nestedModules || []).concat(clonedModule); | ||
var privateInjector = createChild(childModules); | ||
var getFromPrivateInjector = annotate(function(key) { | ||
const privateInjector = createChild(childModules); | ||
const getFromPrivateInjector = annotate(function(key) { | ||
return privateInjector.get(key); | ||
@@ -389,3 +389,3 @@ }); | ||
// ensure child injector initializes | ||
var initializers = (moduleDefinition.__init__ || []).slice(); | ||
const initializers = (moduleDefinition.__init__ || []).slice(); | ||
@@ -415,4 +415,4 @@ initializers.unshift(function() { | ||
var type = moduleDefinition[key][0]; | ||
var value = moduleDefinition[key][1]; | ||
const type = moduleDefinition[key][0]; | ||
const value = moduleDefinition[key][1]; | ||
@@ -453,7 +453,7 @@ providers[key] = [ factoryMap[type], arrayUnwrap(type, value), type ]; | ||
var initializers = moduleDefinitions | ||
const initializers = moduleDefinitions | ||
.reduce(resolveDependencies, []) | ||
.map(loadModule); | ||
var initialized = false; | ||
let initialized = false; | ||
@@ -468,5 +468,3 @@ return function() { | ||
initializers.forEach(function(initializer) { | ||
return initializer(); | ||
}); | ||
initializers.forEach(initializer => initializer()); | ||
}; | ||
@@ -473,0 +471,0 @@ } |
{ | ||
"name": "didi", | ||
"version": "8.0.2", | ||
"version": "9.0.0", | ||
"description": "Dependency Injection for JavaScript", | ||
@@ -8,3 +8,2 @@ "main": "dist/index.js", | ||
"types": "lib/index.d.ts", | ||
"umd:main": "dist/didi.umd.js", | ||
"source": "lib/index.js", | ||
@@ -38,8 +37,8 @@ "scripts": { | ||
"@types/mocha": "^8.2.3", | ||
"@typescript-eslint/eslint-plugin": "^5.29.0", | ||
"@typescript-eslint/parser": "^5.29.0", | ||
"@typescript-eslint/eslint-plugin": "^5.36.2", | ||
"@typescript-eslint/parser": "^5.36.2", | ||
"chai": "^4.3.6", | ||
"cross-env": "^7.0.3", | ||
"eslint": "^8.18.0", | ||
"eslint-plugin-bpmn-io": "^0.14.0", | ||
"eslint": "^8.23.1", | ||
"eslint-plugin-bpmn-io": "^0.14.1", | ||
"esm": "^3.2.25", | ||
@@ -50,3 +49,2 @@ "mocha": "^8.4.0", | ||
"rollup": "^2.75.7", | ||
"rollup-plugin-terser": "^7.0.2", | ||
"ts-node": "^10.8.1", | ||
@@ -53,0 +51,0 @@ "typescript": "^4.7.4" |
15
34498
6
819