Comparing version 4.0.0 to 4.1.0
@@ -28,5 +28,2 @@ 'use strict'; | ||
const isFunction = obj => typeof obj === 'function'; | ||
const hasOwn = (obj, name) => { | ||
return Object.hasOwnProperty.call(obj, name); | ||
}; | ||
const instantiate = (context, args) => { | ||
@@ -36,3 +33,3 @@ // eslint-disable-next-line new-parens | ||
}; | ||
const isNode = (() => { | ||
const isNode = () => { | ||
let _process; // eslint-disable-line no-underscore-dangle | ||
@@ -49,3 +46,3 @@ | ||
return typeof _process === 'object' && _process.toString && _process.toString() === '[object process]'; | ||
})(); // eslint-disable-next-line no-new-func | ||
}; // eslint-disable-next-line no-new-func | ||
@@ -57,7 +54,16 @@ const doUnsafeRequire = new Function('require', 'target', 'return require.main.require(target)'); | ||
}; | ||
const getLast = arr => arr[arr.length - 1]; | ||
const ensureArray = arr => { | ||
if (arr == null) { | ||
return []; | ||
} | ||
function factory(jpex, name, dependencies, fn, opts) { | ||
var _opts$precedence, _opts$lifecycle; | ||
if (Array.isArray(arr)) { | ||
return arr; | ||
} | ||
return [arr]; | ||
}; | ||
const hasLength = arr => arr == null || arr.length > 0; | ||
const validateArgs = (name, dependencies, fn) => { | ||
if (!isString(name)) { | ||
@@ -74,4 +80,10 @@ throw new Error("Factories must be given a name, but was called with [" + typeof name + "]"); | ||
} | ||
}; | ||
if (!dependencies.length) { | ||
function factory(jpex, name, dependencies, fn, opts) { | ||
var _opts$precedence, _opts$lifecycle; | ||
validateArgs(name, dependencies, fn); | ||
if (!hasLength(dependencies)) { | ||
dependencies = null; | ||
@@ -81,2 +93,3 @@ } | ||
const precedence = (_opts$precedence = opts == null ? void 0 : opts.precedence) != null ? _opts$precedence : jpex.$$config.precedence; | ||
const lifecycle = (_opts$lifecycle = opts == null ? void 0 : opts.lifecycle) != null ? _opts$lifecycle : jpex.$$config.lifecycle; | ||
@@ -90,3 +103,3 @@ if (precedence === 'passive' && jpex.$$factories[name] != null) { | ||
dependencies, | ||
lifecycle: (_opts$lifecycle = opts == null ? void 0 : opts.lifecycle) != null ? _opts$lifecycle : jpex.$$config.lifecycle | ||
lifecycle | ||
}; | ||
@@ -96,7 +109,11 @@ jpex.$$factories[name] = f; | ||
function service(jpex, name, dependencies, fn, opts) { | ||
const validateArgs$1 = (name, fn) => { | ||
if (!isFunction(fn)) { | ||
throw new Error("Service " + name + " must be a [Function]"); | ||
throw new Error("Factory " + name + " must be a [Function]"); | ||
} | ||
}; | ||
function service(jpex, name, dependencies, fn, opts) { | ||
validateArgs$1(name, fn); | ||
function factory(...args) { | ||
@@ -134,2 +151,3 @@ const context = {}; | ||
const NAMED_PARAMS = '$namedParameters'; | ||
const VOID = 'undefined'; | ||
@@ -140,7 +158,7 @@ const getFromNodeModules = (jpex, target) => { | ||
// inside an eval setup | ||
if (!isNode) { | ||
if (!jpex.$$config.nodeModules) { | ||
return; | ||
} | ||
if (!jpex.$$config.nodeModules) { | ||
if (!isNode()) { | ||
return; | ||
@@ -154,3 +172,5 @@ } | ||
} catch (e) { | ||
if (e && e.message && e.message.includes("Cannot find module '" + target + "'")) { | ||
var _e$message; | ||
if (e == null ? void 0 : (_e$message = e.message) == null ? void 0 : _e$message.includes == null ? void 0 : _e$message.includes("Cannot find module '" + target + "'")) { | ||
// not found in node modules, just continue | ||
@@ -165,3 +185,3 @@ return; | ||
const getGlobalObject = () => { | ||
if (typeof global !== 'undefined') { | ||
if (typeof global !== VOID) { | ||
// eslint-disable-next-line no-undef | ||
@@ -171,3 +191,3 @@ return global; | ||
if (typeof globalThis !== 'undefined') { | ||
if (typeof globalThis !== VOID) { | ||
// eslint-disable-next-line no-undef | ||
@@ -177,3 +197,3 @@ return globalThis; | ||
if (typeof window !== 'undefined') { | ||
if (typeof window !== VOID) { | ||
return window; | ||
@@ -197,3 +217,4 @@ } | ||
// i.e. window = Window / document = Document | ||
const inferredName = name.charAt(12).toLowerCase() + name.substr(13); | ||
const len = GLOBAL_TYPE_PREFIX.length; | ||
const inferredName = name.charAt(len).toLowerCase() + name.substr(len + 1); | ||
return global[inferredName]; | ||
@@ -216,11 +237,22 @@ } | ||
const validateArgs$2 = name => { | ||
if (!isString(name)) { | ||
throw new Error("Name must be a string, but recevied " + typeof name); | ||
} | ||
}; | ||
const getFromResolved = (jpex, name) => { | ||
return jpex.$$resolved[name]; | ||
}; | ||
const getFromRegistry = (jpex, name) => { | ||
return jpex.$$factories[name]; | ||
}; | ||
const getFactory = (jpex, name, opts) => { | ||
var _opts$optional; | ||
if (typeof name !== 'string') { | ||
throw new Error("Name must be a string, but recevied " + typeof name); | ||
} | ||
validateArgs$2(name); | ||
let factory = getFromResolved(jpex, name); | ||
let factory = jpex.$$resolved[name]; | ||
if (factory != null) { | ||
@@ -230,3 +262,3 @@ return factory; | ||
factory = jpex.$$factories[name]; | ||
factory = getFromRegistry(jpex, name); | ||
@@ -280,3 +312,3 @@ if (factory != null) { | ||
const checkStack = (jpex, name, stack) => { | ||
if (!stack.length) { | ||
if (!hasLength(stack)) { | ||
// This is the first loop | ||
@@ -291,3 +323,3 @@ return false; | ||
if (getLast(stack) === name) { | ||
if (stack[stack.length - 1] === name) { | ||
var _jpex$$$parent; | ||
@@ -308,34 +340,16 @@ | ||
const resolveOne = (jpex, name, namedParameters, opts, stack) => { | ||
var _factory$dependencies; | ||
const getNamedParameters = (namedParameters, opts) => { | ||
if (namedParameters) { | ||
return namedParameters; | ||
} | ||
if (!namedParameters) { | ||
namedParameters = { ...(opts == null ? void 0 : opts.with) | ||
if (opts == null ? void 0 : opts.with) { | ||
return { ...opts.with | ||
}; | ||
} // Check named parameters | ||
// if we have a named parameter for this dependency | ||
// we don't need to do any resolution, we can just return the value | ||
if (hasOwn(namedParameters, name)) { | ||
return namedParameters[name]; | ||
} // Special keys | ||
if (name === NAMED_PARAMS || name === "type:jpex/NamedParameters") { | ||
return namedParameters; | ||
} | ||
if (checkStack(jpex, name, stack)) { | ||
// Yes we have tried to resolve this one before, but we could | ||
// actually just be resolving an inherited factory | ||
return resolveOne(jpex.$$parent, name, namedParameters, opts, []); | ||
} // Get the factory | ||
// This will either return the factory, | ||
// return null (meaning it's an optional dependency) | ||
// or throw an error | ||
return {}; | ||
}; | ||
const factory = getFactory(jpex, name, opts); | ||
const resolveFactory = (jpex, name, factory, namedParameters, opts, stack) => { | ||
if (factory == null) { | ||
@@ -353,5 +367,5 @@ return; | ||
if ((_factory$dependencies = factory.dependencies) == null ? void 0 : _factory$dependencies.length) { | ||
if (hasLength(factory.dependencies)) { | ||
// eslint-disable-next-line no-use-before-define | ||
args = resolveMany(jpex, factory, namedParameters, opts, stack.concat(name)); | ||
args = resolveMany(jpex, factory, namedParameters, opts, [...stack, name]); | ||
} // Invoke the factory | ||
@@ -365,6 +379,34 @@ | ||
}; | ||
const resolveOne = (jpex, name, initialParameters, opts, stack) => { | ||
const namedParameters = getNamedParameters(initialParameters, opts); // Check named parameters | ||
// if we have a named parameter for this dependency | ||
// we don't need to do any resolution, we can just return the value | ||
if (Object.hasOwnProperty.call(namedParameters, name)) { | ||
return namedParameters[name]; | ||
} // Special keys | ||
switch (name) { | ||
case NAMED_PARAMS: | ||
case "type:jpex/NamedParameters": | ||
return namedParameters; | ||
} | ||
if (checkStack(jpex, name, stack)) { | ||
// Yes we have tried to resolve this one before, but we could | ||
// actually just be resolving an inherited factory | ||
return resolveOne(jpex.$$parent, name, namedParameters, opts, []); | ||
} // Get the factory | ||
// This will either return the factory, | ||
// return null (meaning it's an optional dependency) | ||
// or throw an error | ||
const factory = getFactory(jpex, name, opts); | ||
return resolveFactory(jpex, name, factory, namedParameters, opts, stack); | ||
}; | ||
const resolveMany = (jpex, definition, namedParameters, opts, stack) => { | ||
var _definition$dependenc; | ||
if (!(definition == null ? void 0 : (_definition$dependenc = definition.dependencies) == null ? void 0 : _definition$dependenc.length)) { | ||
if (!hasLength(definition == null ? void 0 : definition.dependencies)) { | ||
return []; | ||
@@ -377,7 +419,6 @@ } | ||
const dependencies = [].concat(definition.dependencies); | ||
const values = dependencies.reduce((value, dependency) => { | ||
const x = resolveOne(jpex, dependency, namedParameters, opts, stack); | ||
return value.concat([x]); | ||
}, []); | ||
const dependencies = ensureArray(definition.dependencies); | ||
const values = dependencies.map(dependency => { | ||
return resolveOne(jpex, dependency, namedParameters, opts, stack); | ||
}); | ||
return values; | ||
@@ -433,6 +474,6 @@ }; | ||
const clearCache = (jpex, names) => { | ||
names = [].concat(names || []); | ||
names = ensureArray(names); | ||
for (const key in jpex.$$factories) { | ||
if (!names.length || names.indexOf(key) > -1) { | ||
if (!hasLength(names) || names.includes(key)) { | ||
jpex.$$factories[key].resolved = false; | ||
@@ -443,3 +484,3 @@ } | ||
for (const key in jpex.$$resolved) { | ||
if (!names.length || names.indexOf(key) > -1) { | ||
if (!hasLength(names) || names.includes(key)) { | ||
delete jpex.$$resolved[key]; | ||
@@ -446,0 +487,0 @@ } |
export declare const GLOBAL_TYPE_PREFIX = "type:global:"; | ||
export declare const NAMED_PARAMS = "$namedParameters"; | ||
export declare const VOID = "undefined"; |
@@ -1,5 +0,5 @@ | ||
import type { JpexInstance, SetupConfig, NamedParameters, Lifecycle, Precedence, FactoryOpts, ResolveOpts, ServiceOpts } from './types'; | ||
import type { JpexInstance, SetupConfig, NamedParameters, Lifecycle, Precedence, FactoryOpts, ResolveOpts, ServiceOpts, NodeModule, Global } from './types'; | ||
declare const jpex: JpexInstance; | ||
export { jpex, }; | ||
export type { Lifecycle, JpexInstance, SetupConfig, NamedParameters, Precedence, FactoryOpts, ServiceOpts, ResolveOpts, }; | ||
export type { Lifecycle, JpexInstance, JpexInstance as Jpex, SetupConfig, NamedParameters, Precedence, FactoryOpts, ServiceOpts, ResolveOpts, NodeModule, Global, }; | ||
export default jpex; |
@@ -24,5 +24,2 @@ function _defineProperty(obj, key, value) { | ||
const isFunction = obj => typeof obj === 'function'; | ||
const hasOwn = (obj, name) => { | ||
return Object.hasOwnProperty.call(obj, name); | ||
}; | ||
const instantiate = (context, args) => { | ||
@@ -32,3 +29,3 @@ // eslint-disable-next-line new-parens | ||
}; | ||
const isNode = (() => { | ||
const isNode = () => { | ||
let _process; // eslint-disable-line no-underscore-dangle | ||
@@ -45,3 +42,3 @@ | ||
return typeof _process === 'object' && _process.toString && _process.toString() === '[object process]'; | ||
})(); // eslint-disable-next-line no-new-func | ||
}; // eslint-disable-next-line no-new-func | ||
@@ -53,7 +50,16 @@ const doUnsafeRequire = new Function('require', 'target', 'return require.main.require(target)'); | ||
}; | ||
const getLast = arr => arr[arr.length - 1]; | ||
const ensureArray = arr => { | ||
if (arr == null) { | ||
return []; | ||
} | ||
function factory(jpex, name, dependencies, fn, opts) { | ||
var _opts$precedence, _opts$lifecycle; | ||
if (Array.isArray(arr)) { | ||
return arr; | ||
} | ||
return [arr]; | ||
}; | ||
const hasLength = arr => arr == null || arr.length > 0; | ||
const validateArgs = (name, dependencies, fn) => { | ||
if (!isString(name)) { | ||
@@ -70,4 +76,10 @@ throw new Error("Factories must be given a name, but was called with [" + typeof name + "]"); | ||
} | ||
}; | ||
if (!dependencies.length) { | ||
function factory(jpex, name, dependencies, fn, opts) { | ||
var _opts$precedence, _opts$lifecycle; | ||
validateArgs(name, dependencies, fn); | ||
if (!hasLength(dependencies)) { | ||
dependencies = null; | ||
@@ -77,2 +89,3 @@ } | ||
const precedence = (_opts$precedence = opts == null ? void 0 : opts.precedence) != null ? _opts$precedence : jpex.$$config.precedence; | ||
const lifecycle = (_opts$lifecycle = opts == null ? void 0 : opts.lifecycle) != null ? _opts$lifecycle : jpex.$$config.lifecycle; | ||
@@ -86,3 +99,3 @@ if (precedence === 'passive' && jpex.$$factories[name] != null) { | ||
dependencies, | ||
lifecycle: (_opts$lifecycle = opts == null ? void 0 : opts.lifecycle) != null ? _opts$lifecycle : jpex.$$config.lifecycle | ||
lifecycle | ||
}; | ||
@@ -92,7 +105,11 @@ jpex.$$factories[name] = f; | ||
function service(jpex, name, dependencies, fn, opts) { | ||
const validateArgs$1 = (name, fn) => { | ||
if (!isFunction(fn)) { | ||
throw new Error("Service " + name + " must be a [Function]"); | ||
throw new Error("Factory " + name + " must be a [Function]"); | ||
} | ||
}; | ||
function service(jpex, name, dependencies, fn, opts) { | ||
validateArgs$1(name, fn); | ||
function factory(...args) { | ||
@@ -130,2 +147,3 @@ const context = {}; | ||
const NAMED_PARAMS = '$namedParameters'; | ||
const VOID = 'undefined'; | ||
@@ -136,7 +154,7 @@ const getFromNodeModules = (jpex, target) => { | ||
// inside an eval setup | ||
if (!isNode) { | ||
if (!jpex.$$config.nodeModules) { | ||
return; | ||
} | ||
if (!jpex.$$config.nodeModules) { | ||
if (!isNode()) { | ||
return; | ||
@@ -150,3 +168,5 @@ } | ||
} catch (e) { | ||
if (e && e.message && e.message.includes("Cannot find module '" + target + "'")) { | ||
var _e$message; | ||
if (e == null ? void 0 : (_e$message = e.message) == null ? void 0 : _e$message.includes == null ? void 0 : _e$message.includes("Cannot find module '" + target + "'")) { | ||
// not found in node modules, just continue | ||
@@ -161,3 +181,3 @@ return; | ||
const getGlobalObject = () => { | ||
if (typeof global !== 'undefined') { | ||
if (typeof global !== VOID) { | ||
// eslint-disable-next-line no-undef | ||
@@ -167,3 +187,3 @@ return global; | ||
if (typeof globalThis !== 'undefined') { | ||
if (typeof globalThis !== VOID) { | ||
// eslint-disable-next-line no-undef | ||
@@ -173,3 +193,3 @@ return globalThis; | ||
if (typeof window !== 'undefined') { | ||
if (typeof window !== VOID) { | ||
return window; | ||
@@ -193,3 +213,4 @@ } | ||
// i.e. window = Window / document = Document | ||
const inferredName = name.charAt(12).toLowerCase() + name.substr(13); | ||
const len = GLOBAL_TYPE_PREFIX.length; | ||
const inferredName = name.charAt(len).toLowerCase() + name.substr(len + 1); | ||
return global[inferredName]; | ||
@@ -212,11 +233,22 @@ } | ||
const validateArgs$2 = name => { | ||
if (!isString(name)) { | ||
throw new Error("Name must be a string, but recevied " + typeof name); | ||
} | ||
}; | ||
const getFromResolved = (jpex, name) => { | ||
return jpex.$$resolved[name]; | ||
}; | ||
const getFromRegistry = (jpex, name) => { | ||
return jpex.$$factories[name]; | ||
}; | ||
const getFactory = (jpex, name, opts) => { | ||
var _opts$optional; | ||
if (typeof name !== 'string') { | ||
throw new Error("Name must be a string, but recevied " + typeof name); | ||
} | ||
validateArgs$2(name); | ||
let factory = getFromResolved(jpex, name); | ||
let factory = jpex.$$resolved[name]; | ||
if (factory != null) { | ||
@@ -226,3 +258,3 @@ return factory; | ||
factory = jpex.$$factories[name]; | ||
factory = getFromRegistry(jpex, name); | ||
@@ -276,3 +308,3 @@ if (factory != null) { | ||
const checkStack = (jpex, name, stack) => { | ||
if (!stack.length) { | ||
if (!hasLength(stack)) { | ||
// This is the first loop | ||
@@ -287,3 +319,3 @@ return false; | ||
if (getLast(stack) === name) { | ||
if (stack[stack.length - 1] === name) { | ||
var _jpex$$$parent; | ||
@@ -304,34 +336,16 @@ | ||
const resolveOne = (jpex, name, namedParameters, opts, stack) => { | ||
var _factory$dependencies; | ||
const getNamedParameters = (namedParameters, opts) => { | ||
if (namedParameters) { | ||
return namedParameters; | ||
} | ||
if (!namedParameters) { | ||
namedParameters = { ...(opts == null ? void 0 : opts.with) | ||
if (opts == null ? void 0 : opts.with) { | ||
return { ...opts.with | ||
}; | ||
} // Check named parameters | ||
// if we have a named parameter for this dependency | ||
// we don't need to do any resolution, we can just return the value | ||
if (hasOwn(namedParameters, name)) { | ||
return namedParameters[name]; | ||
} // Special keys | ||
if (name === NAMED_PARAMS || name === "type:jpex/NamedParameters") { | ||
return namedParameters; | ||
} | ||
if (checkStack(jpex, name, stack)) { | ||
// Yes we have tried to resolve this one before, but we could | ||
// actually just be resolving an inherited factory | ||
return resolveOne(jpex.$$parent, name, namedParameters, opts, []); | ||
} // Get the factory | ||
// This will either return the factory, | ||
// return null (meaning it's an optional dependency) | ||
// or throw an error | ||
return {}; | ||
}; | ||
const factory = getFactory(jpex, name, opts); | ||
const resolveFactory = (jpex, name, factory, namedParameters, opts, stack) => { | ||
if (factory == null) { | ||
@@ -349,5 +363,5 @@ return; | ||
if ((_factory$dependencies = factory.dependencies) == null ? void 0 : _factory$dependencies.length) { | ||
if (hasLength(factory.dependencies)) { | ||
// eslint-disable-next-line no-use-before-define | ||
args = resolveMany(jpex, factory, namedParameters, opts, stack.concat(name)); | ||
args = resolveMany(jpex, factory, namedParameters, opts, [...stack, name]); | ||
} // Invoke the factory | ||
@@ -361,6 +375,34 @@ | ||
}; | ||
const resolveOne = (jpex, name, initialParameters, opts, stack) => { | ||
const namedParameters = getNamedParameters(initialParameters, opts); // Check named parameters | ||
// if we have a named parameter for this dependency | ||
// we don't need to do any resolution, we can just return the value | ||
if (Object.hasOwnProperty.call(namedParameters, name)) { | ||
return namedParameters[name]; | ||
} // Special keys | ||
switch (name) { | ||
case NAMED_PARAMS: | ||
case "type:jpex/NamedParameters": | ||
return namedParameters; | ||
} | ||
if (checkStack(jpex, name, stack)) { | ||
// Yes we have tried to resolve this one before, but we could | ||
// actually just be resolving an inherited factory | ||
return resolveOne(jpex.$$parent, name, namedParameters, opts, []); | ||
} // Get the factory | ||
// This will either return the factory, | ||
// return null (meaning it's an optional dependency) | ||
// or throw an error | ||
const factory = getFactory(jpex, name, opts); | ||
return resolveFactory(jpex, name, factory, namedParameters, opts, stack); | ||
}; | ||
const resolveMany = (jpex, definition, namedParameters, opts, stack) => { | ||
var _definition$dependenc; | ||
if (!(definition == null ? void 0 : (_definition$dependenc = definition.dependencies) == null ? void 0 : _definition$dependenc.length)) { | ||
if (!hasLength(definition == null ? void 0 : definition.dependencies)) { | ||
return []; | ||
@@ -373,7 +415,6 @@ } | ||
const dependencies = [].concat(definition.dependencies); | ||
const values = dependencies.reduce((value, dependency) => { | ||
const x = resolveOne(jpex, dependency, namedParameters, opts, stack); | ||
return value.concat([x]); | ||
}, []); | ||
const dependencies = ensureArray(definition.dependencies); | ||
const values = dependencies.map(dependency => { | ||
return resolveOne(jpex, dependency, namedParameters, opts, stack); | ||
}); | ||
return values; | ||
@@ -429,6 +470,6 @@ }; | ||
const clearCache = (jpex, names) => { | ||
names = [].concat(names || []); | ||
names = ensureArray(names); | ||
for (const key in jpex.$$factories) { | ||
if (!names.length || names.indexOf(key) > -1) { | ||
if (!hasLength(names) || names.includes(key)) { | ||
jpex.$$factories[key].resolved = false; | ||
@@ -439,3 +480,3 @@ } | ||
for (const key in jpex.$$resolved) { | ||
if (!names.length || names.indexOf(key) > -1) { | ||
if (!hasLength(names) || names.includes(key)) { | ||
delete jpex.$$resolved[key]; | ||
@@ -442,0 +483,0 @@ } |
import { JpexInstance, Dependency, Definition, NamedParameters, ResolveOpts } from '../types'; | ||
export declare const resolveOne: <R extends unknown>(jpex: JpexInstance, name: Dependency, namedParameters: NamedParameters, opts: ResolveOpts, stack: string[]) => R; | ||
export declare const resolveOne: <R extends unknown>(jpex: JpexInstance, name: Dependency, initialParameters: NamedParameters, opts: ResolveOpts, stack: string[]) => R; | ||
export declare const resolveMany: <R extends any[]>(jpex: JpexInstance, definition: Definition, namedParameters: NamedParameters, opts: ResolveOpts, stack: string[]) => R; |
export * from './JpexInstance'; | ||
export * from './BuiltIns'; | ||
export * from './custom'; | ||
export declare type Lifecycle = 'application' | 'class' | 'instance' | 'none'; | ||
@@ -4,0 +5,0 @@ export declare type Precedence = 'active' | 'passive'; |
@@ -1,14 +0,7 @@ | ||
export declare const isObject: (obj: any) => obj is object; | ||
export declare const isSymbol: (obj: any) => boolean; | ||
export declare const isString: (obj: any) => obj is string; | ||
export declare const isFunction: (obj: any) => obj is Function; | ||
export declare const hasOwn: <T>(obj: T, name: string | Symbol) => boolean; | ||
export declare const instantiate: (context: any, args: any[]) => any; | ||
export declare const isNode: boolean; | ||
export declare const isNode: () => boolean; | ||
export declare const unsafeRequire: (target: string) => any; | ||
interface GetLast { | ||
(str: string): string; | ||
<T>(arr: T[]): T; | ||
} | ||
export declare const getLast: GetLast; | ||
export {}; | ||
export declare const ensureArray: <T>(arr: T | T[]) => T[]; | ||
export declare const hasLength: <T>(arr: T[]) => boolean; |
177
dist/es5.js
@@ -67,5 +67,2 @@ 'use strict'; | ||
}; | ||
var hasOwn = function hasOwn(obj, name) { | ||
return Object.hasOwnProperty.call(obj, name); | ||
}; | ||
var instantiate = function instantiate(context, args) { | ||
@@ -75,3 +72,3 @@ // eslint-disable-next-line new-parens | ||
}; | ||
var isNode = function () { | ||
var isNode = function isNode() { | ||
var _process; // eslint-disable-line no-underscore-dangle | ||
@@ -88,3 +85,3 @@ | ||
return typeof _process === 'object' && _process.toString && _process.toString() === '[object process]'; | ||
}(); // eslint-disable-next-line no-new-func | ||
}; // eslint-disable-next-line no-new-func | ||
@@ -96,9 +93,18 @@ var doUnsafeRequire = new Function('require', 'target', 'return require.main.require(target)'); | ||
}; | ||
var getLast = function getLast(arr) { | ||
return arr[arr.length - 1]; | ||
var ensureArray = function ensureArray(arr) { | ||
if (arr == null) { | ||
return []; | ||
} | ||
if (Array.isArray(arr)) { | ||
return arr; | ||
} | ||
return [arr]; | ||
}; | ||
var hasLength = function hasLength(arr) { | ||
return arr == null || arr.length > 0; | ||
}; | ||
function factory(jpex, name, dependencies, fn, opts) { | ||
var _opts$precedence, _opts$lifecycle; | ||
var validateArgs = function validateArgs(name, dependencies, fn) { | ||
if (!isString(name)) { | ||
@@ -115,4 +121,10 @@ throw new Error("Factories must be given a name, but was called with [" + typeof name + "]"); | ||
} | ||
}; | ||
if (!dependencies.length) { | ||
function factory(jpex, name, dependencies, fn, opts) { | ||
var _opts$precedence, _opts$lifecycle; | ||
validateArgs(name, dependencies, fn); | ||
if (!hasLength(dependencies)) { | ||
dependencies = null; | ||
@@ -122,2 +134,3 @@ } | ||
var precedence = (_opts$precedence = opts == null ? void 0 : opts.precedence) != null ? _opts$precedence : jpex.$$config.precedence; | ||
var lifecycle = (_opts$lifecycle = opts == null ? void 0 : opts.lifecycle) != null ? _opts$lifecycle : jpex.$$config.lifecycle; | ||
@@ -131,3 +144,3 @@ if (precedence === 'passive' && jpex.$$factories[name] != null) { | ||
dependencies: dependencies, | ||
lifecycle: (_opts$lifecycle = opts == null ? void 0 : opts.lifecycle) != null ? _opts$lifecycle : jpex.$$config.lifecycle | ||
lifecycle: lifecycle | ||
}; | ||
@@ -137,7 +150,11 @@ jpex.$$factories[name] = f; | ||
function service(jpex, name, dependencies, fn, opts) { | ||
var validateArgs$1 = function validateArgs(name, fn) { | ||
if (!isFunction(fn)) { | ||
throw new Error("Service " + name + " must be a [Function]"); | ||
throw new Error("Factory " + name + " must be a [Function]"); | ||
} | ||
}; | ||
function service(jpex, name, dependencies, fn, opts) { | ||
validateArgs$1(name, fn); | ||
function factory() { | ||
@@ -179,2 +196,3 @@ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
var NAMED_PARAMS = '$namedParameters'; | ||
var VOID = 'undefined'; | ||
@@ -185,7 +203,7 @@ var getFromNodeModules = function getFromNodeModules(jpex, target) { | ||
// inside an eval setup | ||
if (!isNode) { | ||
if (!jpex.$$config.nodeModules) { | ||
return; | ||
} | ||
if (!jpex.$$config.nodeModules) { | ||
if (!isNode()) { | ||
return; | ||
@@ -199,3 +217,5 @@ } | ||
} catch (e) { | ||
if (e && e.message && e.message.includes("Cannot find module '" + target + "'")) { | ||
var _e$message; | ||
if (e == null ? void 0 : (_e$message = e.message) == null ? void 0 : _e$message.includes == null ? void 0 : _e$message.includes("Cannot find module '" + target + "'")) { | ||
// not found in node modules, just continue | ||
@@ -210,3 +230,3 @@ return; | ||
var getGlobalObject = function getGlobalObject() { | ||
if (typeof global !== 'undefined') { | ||
if (typeof global !== VOID) { | ||
// eslint-disable-next-line no-undef | ||
@@ -216,3 +236,3 @@ return global; | ||
if (typeof globalThis !== 'undefined') { | ||
if (typeof globalThis !== VOID) { | ||
// eslint-disable-next-line no-undef | ||
@@ -222,3 +242,3 @@ return globalThis; | ||
if (typeof window !== 'undefined') { | ||
if (typeof window !== VOID) { | ||
return window; | ||
@@ -242,3 +262,4 @@ } | ||
// i.e. window = Window / document = Document | ||
var inferredName = name.charAt(12).toLowerCase() + name.substr(13); | ||
var len = GLOBAL_TYPE_PREFIX.length; | ||
var inferredName = name.charAt(len).toLowerCase() + name.substr(len + 1); | ||
return global[inferredName]; | ||
@@ -261,11 +282,22 @@ } | ||
var validateArgs$2 = function validateArgs(name) { | ||
if (!isString(name)) { | ||
throw new Error("Name must be a string, but recevied " + typeof name); | ||
} | ||
}; | ||
var getFromResolved = function getFromResolved(jpex, name) { | ||
return jpex.$$resolved[name]; | ||
}; | ||
var getFromRegistry = function getFromRegistry(jpex, name) { | ||
return jpex.$$factories[name]; | ||
}; | ||
var getFactory = function getFactory(jpex, name, opts) { | ||
var _opts$optional; | ||
if (typeof name !== 'string') { | ||
throw new Error("Name must be a string, but recevied " + typeof name); | ||
} | ||
validateArgs$2(name); | ||
var factory = getFromResolved(jpex, name); | ||
var factory = jpex.$$resolved[name]; | ||
if (factory != null) { | ||
@@ -275,3 +307,3 @@ return factory; | ||
factory = jpex.$$factories[name]; | ||
factory = getFromRegistry(jpex, name); | ||
@@ -325,3 +357,3 @@ if (factory != null) { | ||
var checkStack = function checkStack(jpex, name, stack) { | ||
if (!stack.length) { | ||
if (!hasLength(stack)) { | ||
// This is the first loop | ||
@@ -336,3 +368,3 @@ return false; | ||
if (getLast(stack) === name) { | ||
if (stack[stack.length - 1] === name) { | ||
var _jpex$$$parent; | ||
@@ -353,33 +385,15 @@ | ||
var resolveOne = function resolveOne(jpex, name, namedParameters, opts, stack) { | ||
var _factory$dependencies; | ||
if (!namedParameters) { | ||
namedParameters = _extends({}, opts == null ? void 0 : opts.with); | ||
} // Check named parameters | ||
// if we have a named parameter for this dependency | ||
// we don't need to do any resolution, we can just return the value | ||
if (hasOwn(namedParameters, name)) { | ||
return namedParameters[name]; | ||
} // Special keys | ||
if (name === NAMED_PARAMS || name === "type:jpex/NamedParameters") { | ||
var getNamedParameters = function getNamedParameters(namedParameters, opts) { | ||
if (namedParameters) { | ||
return namedParameters; | ||
} | ||
if (checkStack(jpex, name, stack)) { | ||
// Yes we have tried to resolve this one before, but we could | ||
// actually just be resolving an inherited factory | ||
return resolveOne(jpex.$$parent, name, namedParameters, opts, []); | ||
} // Get the factory | ||
// This will either return the factory, | ||
// return null (meaning it's an optional dependency) | ||
// or throw an error | ||
if (opts == null ? void 0 : opts.with) { | ||
return _extends({}, opts.with); | ||
} | ||
return {}; | ||
}; | ||
var factory = getFactory(jpex, name, opts); | ||
var resolveFactory = function resolveFactory(jpex, name, factory, namedParameters, opts, stack) { | ||
if (factory == null) { | ||
@@ -397,5 +411,5 @@ return; | ||
if ((_factory$dependencies = factory.dependencies) == null ? void 0 : _factory$dependencies.length) { | ||
if (hasLength(factory.dependencies)) { | ||
// eslint-disable-next-line no-use-before-define | ||
args = resolveMany(jpex, factory, namedParameters, opts, stack.concat(name)); | ||
args = resolveMany(jpex, factory, namedParameters, opts, [].concat(stack, [name])); | ||
} // Invoke the factory | ||
@@ -409,6 +423,34 @@ | ||
}; | ||
var resolveOne = function resolveOne(jpex, name, initialParameters, opts, stack) { | ||
var namedParameters = getNamedParameters(initialParameters, opts); // Check named parameters | ||
// if we have a named parameter for this dependency | ||
// we don't need to do any resolution, we can just return the value | ||
if (Object.hasOwnProperty.call(namedParameters, name)) { | ||
return namedParameters[name]; | ||
} // Special keys | ||
switch (name) { | ||
case NAMED_PARAMS: | ||
case "type:jpex/NamedParameters": | ||
return namedParameters; | ||
} | ||
if (checkStack(jpex, name, stack)) { | ||
// Yes we have tried to resolve this one before, but we could | ||
// actually just be resolving an inherited factory | ||
return resolveOne(jpex.$$parent, name, namedParameters, opts, []); | ||
} // Get the factory | ||
// This will either return the factory, | ||
// return null (meaning it's an optional dependency) | ||
// or throw an error | ||
var factory = getFactory(jpex, name, opts); | ||
return resolveFactory(jpex, name, factory, namedParameters, opts, stack); | ||
}; | ||
var resolveMany = function resolveMany(jpex, definition, namedParameters, opts, stack) { | ||
var _definition$dependenc; | ||
if (!(definition == null ? void 0 : (_definition$dependenc = definition.dependencies) == null ? void 0 : _definition$dependenc.length)) { | ||
if (!hasLength(definition == null ? void 0 : definition.dependencies)) { | ||
return []; | ||
@@ -421,7 +463,6 @@ } | ||
var dependencies = [].concat(definition.dependencies); | ||
var values = dependencies.reduce(function (value, dependency) { | ||
var x = resolveOne(jpex, dependency, namedParameters, opts, stack); | ||
return value.concat([x]); | ||
}, []); | ||
var dependencies = ensureArray(definition.dependencies); | ||
var values = dependencies.map(function (dependency) { | ||
return resolveOne(jpex, dependency, namedParameters, opts, stack); | ||
}); | ||
return values; | ||
@@ -483,6 +524,6 @@ }; | ||
var clearCache = function clearCache(jpex, names) { | ||
names = [].concat(names || []); | ||
names = ensureArray(names); | ||
for (var key in jpex.$$factories) { | ||
if (!names.length || names.indexOf(key) > -1) { | ||
if (!hasLength(names) || names.includes(key)) { | ||
jpex.$$factories[key].resolved = false; | ||
@@ -493,3 +534,3 @@ } | ||
for (var _key in jpex.$$resolved) { | ||
if (!names.length || names.indexOf(_key) > -1) { | ||
if (!hasLength(names) || names.includes(_key)) { | ||
delete jpex.$$resolved[_key]; | ||
@@ -496,0 +537,0 @@ } |
{ | ||
"name": "jpex", | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"description": "Javascript Prototype Extension", | ||
@@ -20,3 +20,4 @@ "main": "dist/cjs/jpex.js", | ||
"prepublishOnly": "yarn build", | ||
"semantic-release": "semantic-release" | ||
"semantic-release": "semantic-release", | ||
"ci": "yarn lint && yarn test && yarn build" | ||
}, | ||
@@ -23,0 +24,0 @@ "husky": { |
@@ -20,2 +20,3 @@ | ||
- [jpex](#jpex) | ||
- [types](#types) | ||
- [caveats](#caveats) | ||
@@ -307,2 +308,42 @@ - [react](#react) | ||
### Types | ||
#### Jpex | ||
This is the type definition for the jpex container | ||
#### NodeModule | ||
This is a special type that lets you automatically inject a node module with type inference. | ||
For example: | ||
```ts | ||
import jpex, { NodeModule } from 'jpex'; | ||
// this will resolve to the fs module without you having to explicitly register it as a dependency | ||
const fs = jpex.resolve<NodeModule<'fs'>>(); | ||
``` | ||
The default return type will be `any` but you can specify one explicitly with the second type parameter: | ||
```ts | ||
import type fstype from 'fs'; | ||
import jpex, { NodeModule } from 'jpex'; | ||
const fs = jpex.resolve<NodeModule<'fs', typeof fstype>>(); | ||
``` | ||
#### Global | ||
This is another special type that lets you automatically inject a global property with type inference. | ||
For built-in types you can do this without any helpers: | ||
```ts | ||
import jpex from 'jpex'; | ||
const navigator = jpex.resolve<Navigator>(); | ||
``` | ||
But for custom globals, or properties that don't have built-in types, you can use the `Global` type: | ||
```ts | ||
import jpex, { Global } from 'jpex'; | ||
const analytics = jpex.resolve<Global<'ga', Function>>(); | ||
``` | ||
## caveats | ||
@@ -340,18 +381,14 @@ There are a few caveats to be aware of: | ||
```tsx | ||
import { Provider, useJpex } from 'jpex'; | ||
import base, { Provider, useJpex } from 'jpex'; | ||
// create a stub for the SaveData dependency | ||
const saveData = stub(); | ||
// create a new container | ||
const jpex = base.extend(); | ||
// register our stub dependency | ||
jpex.constant<SaveData>(saveData); | ||
render( | ||
// the Provider component will create a new jpex instance | ||
<Provider> | ||
{() => { | ||
// grab jpex - it will be isolated to this context only | ||
const jpex = useJpex(); | ||
// register our stub dependency | ||
jpex.constant<SaveData>(saveData); | ||
// when we render MyComponent, it will be given our stubbed dependency | ||
return (<MyComponent/>); | ||
}} | ||
<Provider value={jpex}> | ||
{/* when we render MyComponent, it will be given our stubbed dependency */} | ||
<MyComponent/> | ||
</Provider> | ||
@@ -370,2 +407,4 @@ ); | ||
```ts | ||
const { jpex } = require('jpex'); | ||
jpex.constant('foo', 'foo'); | ||
@@ -372,0 +411,0 @@ jpex.factory('bah', [ 'foo' ], (foo) => foo + 'bah'); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
74899
25
1509
413
0