New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

jpex

Package Overview
Dependencies
Maintainers
1
Versions
42
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jpex - npm Package Compare versions

Comparing version 4.1.0 to 4.2.0

dist/ts/clearCache.d.ts

2

CHANGELOG.md
Change Log
==========
Further change logs can be found on the [releases](https://github.com/jpex-js/jpex/releases) page
### 4.0.0

@@ -4,0 +6,0 @@ - global dependencies such as `Window` and `Document` are now automatically resolved (unless you register your own dependency of the same name)

285

dist/cjs/jpex.js

@@ -5,19 +5,4 @@ 'use strict';

function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function constant(jpex, name, obj) {
return jpex.factory(name, [], () => obj, {
function constant(name, obj) {
return this.factory(name, [], () => obj, {
lifecycle: 'application'

@@ -67,17 +52,14 @@ });

if (!isString(name)) {
throw new Error("Factories must be given a name, but was called with [" + typeof name + "]");
throw new Error(`Factories must be given a name, but was called with [${typeof name}]`);
}
if (!Array.isArray(dependencies)) {
throw new Error("Expected an array of dependencies, but was called with [" + typeof dependencies + "]");
throw new Error(`Expected an array of dependencies, but was called with [${typeof dependencies}]`);
}
if (!isFunction(fn)) {
throw new Error("Factory " + name + " must be a [Function]");
throw new Error(`Factory ${name} must be a [Function]`);
}
};
function factory(jpex, name, dependencies, fn, opts) {
var _opts$precedence, _opts$lifecycle;
function factory(name, dependencies, fn, opts = {}) {
validateArgs(name, dependencies, fn);

@@ -87,8 +69,6 @@

dependencies = null;
}
} // eslint-disable-next-line max-len
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;
if (precedence === 'passive' && jpex.$$factories[name] != null) {
if ((opts.precedence || this.$$config.precedence) === 'passive' && this.$$factories[name] != null) {
return;

@@ -100,15 +80,13 @@ }

dependencies,
lifecycle
lifecycle: opts.lifecycle || this.$$config.lifecycle
};
jpex.$$factories[name] = f;
}
this.$$factories[name] = f;
const validateArgs$1 = (name, fn) => {
if (!isFunction(fn)) {
throw new Error("Factory " + name + " must be a [Function]");
if (opts.alias) {
ensureArray(opts.alias).forEach(alias => this.alias(alias, name));
}
};
}
function service(jpex, name, dependencies, fn, opts) {
validateArgs$1(name, fn);
function service(name, dependencies, fn, opts = {}) {
validateArgs(name, dependencies, fn);

@@ -118,3 +96,3 @@ function factory(...args) {

if (opts == null ? void 0 : opts.bindToInstance) {
if (opts.bindToInstance) {
dependencies.forEach((key, i) => {

@@ -125,21 +103,20 @@ context[key] = args[i];

args.unshift(context);
return instantiate(fn, args);
return instantiate(fn, [context, ...args]);
}
return jpex.factory(name, dependencies, factory, opts);
return this.factory(name, dependencies, factory, opts);
}
function alias(jpex, alias, name) {
if (jpex.$$factories[name] != null) {
jpex.$$factories[alias] = jpex.$$factories[name];
function alias(alias, name) {
if (this.$$factories[name] != null) {
this.$$factories[alias] = this.$$factories[name];
return;
}
if (jpex.$$factories[alias] != null) {
jpex.$$factories[name] = jpex.$$factories[alias];
if (this.$$factories[alias] != null) {
this.$$factories[name] = this.$$factories[alias];
return;
}
throw new Error("Cannot create an alias for [" + name + "|" + alias + "] as it does not exist");
throw new Error(`Cannot create an alias for [${name}|${alias}] as it does not exist`);
}

@@ -155,10 +132,6 @@

// inside an eval setup
if (!jpex.$$config.nodeModules) {
if (!jpex.$$config.nodeModules || !isNode()) {
return;
}
if (!isNode()) {
return;
}
try {

@@ -171,3 +144,3 @@ const value = unsafeRequire(target);

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 + "'")) {
if ((_e$message = e.message) != null && _e$message.includes != null && _e$message.includes(`Cannot find module '${target}'`)) {
// not found in node modules, just continue

@@ -230,5 +203,5 @@ return;

const validateArgs$2 = name => {
const validateArgs$1 = name => {
if (!isString(name)) {
throw new Error("Name must be a string, but recevied " + typeof name);
throw new Error(`Name must be a string, but recevied ${typeof name}`);
}

@@ -245,35 +218,21 @@ };

const getFactory = (jpex, name, opts) => {
const getFactory = (jpex, name, opts = {}) => {
var _opts$optional;
validateArgs$2(name);
let factory = getFromResolved(jpex, name);
validateArgs$1(name);
const fns = [getFromResolved, getFromRegistry, getFromGlobal, getFromNodeModules];
if (factory != null) {
return factory;
}
while (fns.length) {
const factory = fns.shift()(jpex, name);
factory = getFromRegistry(jpex, name);
if (factory != null) {
return factory;
if (factory != null) {
return factory;
}
}
factory = getFromGlobal(jpex, name);
if (factory != null) {
return factory;
}
factory = getFromNodeModules(jpex, name);
if (factory != null) {
return factory;
}
if ((_opts$optional = opts == null ? void 0 : opts.optional) != null ? _opts$optional : jpex.$$config.optional) {
if ((_opts$optional = opts.optional) != null ? _opts$optional : jpex.$$config.optional) {
return;
}
throw new Error("Unable to find required dependency [" + name + "]");
throw new Error(`Unable to find required dependency [${name}]`);
};

@@ -299,2 +258,3 @@ const cacheResult = (jpex, name, factory, value, namedParameters) => {

default:
// instance
namedParameters[name] = value;

@@ -329,6 +289,6 @@ break;

throw new Error("Recursive loop for dependency " + name + " encountered");
throw new Error(`Recursive loop for dependency ${name} encountered`);
};
const getNamedParameters = (namedParameters, opts) => {
const getNamedParameters = (namedParameters, opts = {}) => {
if (namedParameters) {

@@ -338,3 +298,3 @@ return namedParameters;

if (opts == null ? void 0 : opts.with) {
if (opts.with) {
return { ...opts.with

@@ -382,6 +342,4 @@ };

switch (name) {
case NAMED_PARAMS:
case "type:jpex/NamedParameters":
return namedParameters;
if (name === NAMED_PARAMS || name === "type:jpex/NamedParameters") {
return namedParameters;
}

@@ -403,3 +361,3 @@

const resolveMany = (jpex, definition, namedParameters, opts, stack) => {
if (!hasLength(definition == null ? void 0 : definition.dependencies)) {
if (!hasLength(definition.dependencies)) {
return [];

@@ -419,8 +377,10 @@ }

const resolve = (jpex, name, opts) => resolveOne(jpex, name, void 0, opts, []);
const resolveDependencies = (jpex, definition, opts) => {
return resolveMany(jpex, definition, void 0, opts, []);
};
const isResolved = (jpex, dependency) => {
var _jpex$$$factories$dep;
function resolve(name, opts) {
return resolveOne(this, name, void 0, opts, []);
}
function resolveDependencies(definition, opts) {
return resolveMany(this, definition, void 0, opts, []);
}
function isResolved(dependency) {
var _this$$$factories$dep;

@@ -431,7 +391,7 @@ if (!isString(dependency)) {

if (jpex.$$resolved[dependency] != null) {
if (this.$$resolved[dependency] != null) {
return true;
}
if ((_jpex$$$factories$dep = jpex.$$factories[dependency]) == null ? void 0 : _jpex$$$factories$dep.resolved) {
if ((_this$$$factories$dep = this.$$factories[dependency]) != null && _this$$$factories$dep.resolved) {
return true;

@@ -441,8 +401,9 @@ }

return false;
};
const allResolved = (jpex, dependencies) => {
return dependencies.every(isResolved.bind(null, jpex));
};
}
function allResolved(dependencies) {
return dependencies.every(isResolved.bind(this));
}
const encase = (jpex, dependencies, fn) => {
function encase(dependencies, fn) {
const jpex = this;
let result;

@@ -452,10 +413,10 @@

/* eslint-disable no-invalid-this */
if (result && allResolved(jpex, dependencies)) {
if (result && allResolved.call(jpex, dependencies)) {
return result.apply(this, args);
}
const deps = resolveDependencies(jpex, {
const deps = resolveDependencies.call(jpex, {
dependencies
});
result = fn.apply(this, deps);
result = fn.apply(jpex, deps);
return result.apply(this, args);

@@ -467,19 +428,19 @@ /* eslint-enable */

return encased;
};
}
const clearCache = (jpex, names) => {
function clearCache(...names) {
names = ensureArray(names);
for (const key in jpex.$$factories) {
for (const key in this.$$factories) {
if (!hasLength(names) || names.includes(key)) {
jpex.$$factories[key].resolved = false;
this.$$factories[key].resolved = false;
}
}
for (const key in jpex.$$resolved) {
for (const key in this.$$resolved) {
if (!hasLength(names) || names.includes(key)) {
delete jpex.$$resolved[key];
delete this.$$resolved[key];
}
}
};
}

@@ -493,83 +454,45 @@ const defaultConfig = {

};
class Jpex {
constructor(options = {}, parent) {
_defineProperty(this, "decorate", void 0);
_defineProperty(this, "$$config", void 0);
_defineProperty(this, "$$parent", void 0);
_defineProperty(this, "$$factories", {});
_defineProperty(this, "$$resolved", {});
const {
inherit = true,
...config
} = options;
this.$$parent = parent;
this.$$config = { ...defaultConfig,
function makeJpex({
inherit = true,
...config
} = {}, parent) {
const jpex = {
$$parent: parent,
$$config: { ...defaultConfig,
...(inherit ? parent == null ? void 0 : parent.$$config : {}),
...config
};
},
$$factories: parent && inherit ? Object.create(parent.$$factories) : {},
$$resolved: {},
constant,
factory,
service,
alias,
resolve,
encase,
clearCache,
if (parent && inherit) {
this.$$factories = Object.create(parent.$$factories);
}
}
extend(config) {
return makeJpex(config, this);
},
extend(config) {
return new Jpex(config, this);
}
resolveWith(name, namedParameters, opts) {
return this.resolve(name, {
with: namedParameters,
...opts
});
},
constant(name, obj) {
return constant(this, name, obj);
}
raw(name) {
return getFactory(this, name, {}).fn;
},
factory(name, deps, fn, opts) {
return factory(this, name, deps, fn, opts);
}
service(name, deps, fn, opts) {
return service(this, name, deps, fn, opts);
}
alias(alias$1, name) {
return alias(this, alias$1, name);
}
resolve(name, opts) {
return resolve(this, name, opts);
}
resolveWith(name, namedParameters, opts) {
return resolve(this, name, {
with: namedParameters,
...opts
});
}
raw(name) {
return getFactory(this, name, {}).fn;
} // eslint-disable-next-line @typescript-eslint/no-unused-vars
encase(deps, fn) {
return encase(this, deps, fn);
}
clearCache(...names) {
return clearCache(this, names);
}
infer() {
return '';
}
infer: () => ''
};
return jpex;
}
const jpex = new Jpex();
const jpex = makeJpex();
exports.default = jpex;
exports.jpex = jpex;

@@ -1,18 +0,3 @@

function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function constant(jpex, name, obj) {
return jpex.factory(name, [], () => obj, {
function constant(name, obj) {
return this.factory(name, [], () => obj, {
lifecycle: 'application'

@@ -62,17 +47,14 @@ });

if (!isString(name)) {
throw new Error("Factories must be given a name, but was called with [" + typeof name + "]");
throw new Error(`Factories must be given a name, but was called with [${typeof name}]`);
}
if (!Array.isArray(dependencies)) {
throw new Error("Expected an array of dependencies, but was called with [" + typeof dependencies + "]");
throw new Error(`Expected an array of dependencies, but was called with [${typeof dependencies}]`);
}
if (!isFunction(fn)) {
throw new Error("Factory " + name + " must be a [Function]");
throw new Error(`Factory ${name} must be a [Function]`);
}
};
function factory(jpex, name, dependencies, fn, opts) {
var _opts$precedence, _opts$lifecycle;
function factory(name, dependencies, fn, opts = {}) {
validateArgs(name, dependencies, fn);

@@ -82,8 +64,6 @@

dependencies = null;
}
} // eslint-disable-next-line max-len
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;
if (precedence === 'passive' && jpex.$$factories[name] != null) {
if ((opts.precedence || this.$$config.precedence) === 'passive' && this.$$factories[name] != null) {
return;

@@ -95,15 +75,13 @@ }

dependencies,
lifecycle
lifecycle: opts.lifecycle || this.$$config.lifecycle
};
jpex.$$factories[name] = f;
}
this.$$factories[name] = f;
const validateArgs$1 = (name, fn) => {
if (!isFunction(fn)) {
throw new Error("Factory " + name + " must be a [Function]");
if (opts.alias) {
ensureArray(opts.alias).forEach(alias => this.alias(alias, name));
}
};
}
function service(jpex, name, dependencies, fn, opts) {
validateArgs$1(name, fn);
function service(name, dependencies, fn, opts = {}) {
validateArgs(name, dependencies, fn);

@@ -113,3 +91,3 @@ function factory(...args) {

if (opts == null ? void 0 : opts.bindToInstance) {
if (opts.bindToInstance) {
dependencies.forEach((key, i) => {

@@ -120,21 +98,20 @@ context[key] = args[i];

args.unshift(context);
return instantiate(fn, args);
return instantiate(fn, [context, ...args]);
}
return jpex.factory(name, dependencies, factory, opts);
return this.factory(name, dependencies, factory, opts);
}
function alias(jpex, alias, name) {
if (jpex.$$factories[name] != null) {
jpex.$$factories[alias] = jpex.$$factories[name];
function alias(alias, name) {
if (this.$$factories[name] != null) {
this.$$factories[alias] = this.$$factories[name];
return;
}
if (jpex.$$factories[alias] != null) {
jpex.$$factories[name] = jpex.$$factories[alias];
if (this.$$factories[alias] != null) {
this.$$factories[name] = this.$$factories[alias];
return;
}
throw new Error("Cannot create an alias for [" + name + "|" + alias + "] as it does not exist");
throw new Error(`Cannot create an alias for [${name}|${alias}] as it does not exist`);
}

@@ -150,10 +127,6 @@

// inside an eval setup
if (!jpex.$$config.nodeModules) {
if (!jpex.$$config.nodeModules || !isNode()) {
return;
}
if (!isNode()) {
return;
}
try {

@@ -166,3 +139,3 @@ const value = unsafeRequire(target);

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 + "'")) {
if ((_e$message = e.message) != null && _e$message.includes != null && _e$message.includes(`Cannot find module '${target}'`)) {
// not found in node modules, just continue

@@ -225,5 +198,5 @@ return;

const validateArgs$2 = name => {
const validateArgs$1 = name => {
if (!isString(name)) {
throw new Error("Name must be a string, but recevied " + typeof name);
throw new Error(`Name must be a string, but recevied ${typeof name}`);
}

@@ -240,35 +213,21 @@ };

const getFactory = (jpex, name, opts) => {
const getFactory = (jpex, name, opts = {}) => {
var _opts$optional;
validateArgs$2(name);
let factory = getFromResolved(jpex, name);
validateArgs$1(name);
const fns = [getFromResolved, getFromRegistry, getFromGlobal, getFromNodeModules];
if (factory != null) {
return factory;
}
while (fns.length) {
const factory = fns.shift()(jpex, name);
factory = getFromRegistry(jpex, name);
if (factory != null) {
return factory;
if (factory != null) {
return factory;
}
}
factory = getFromGlobal(jpex, name);
if (factory != null) {
return factory;
}
factory = getFromNodeModules(jpex, name);
if (factory != null) {
return factory;
}
if ((_opts$optional = opts == null ? void 0 : opts.optional) != null ? _opts$optional : jpex.$$config.optional) {
if ((_opts$optional = opts.optional) != null ? _opts$optional : jpex.$$config.optional) {
return;
}
throw new Error("Unable to find required dependency [" + name + "]");
throw new Error(`Unable to find required dependency [${name}]`);
};

@@ -294,2 +253,3 @@ const cacheResult = (jpex, name, factory, value, namedParameters) => {

default:
// instance
namedParameters[name] = value;

@@ -324,6 +284,6 @@ break;

throw new Error("Recursive loop for dependency " + name + " encountered");
throw new Error(`Recursive loop for dependency ${name} encountered`);
};
const getNamedParameters = (namedParameters, opts) => {
const getNamedParameters = (namedParameters, opts = {}) => {
if (namedParameters) {

@@ -333,3 +293,3 @@ return namedParameters;

if (opts == null ? void 0 : opts.with) {
if (opts.with) {
return { ...opts.with

@@ -377,6 +337,4 @@ };

switch (name) {
case NAMED_PARAMS:
case "type:jpex/NamedParameters":
return namedParameters;
if (name === NAMED_PARAMS || name === "type:jpex/NamedParameters") {
return namedParameters;
}

@@ -398,3 +356,3 @@

const resolveMany = (jpex, definition, namedParameters, opts, stack) => {
if (!hasLength(definition == null ? void 0 : definition.dependencies)) {
if (!hasLength(definition.dependencies)) {
return [];

@@ -414,8 +372,10 @@ }

const resolve = (jpex, name, opts) => resolveOne(jpex, name, void 0, opts, []);
const resolveDependencies = (jpex, definition, opts) => {
return resolveMany(jpex, definition, void 0, opts, []);
};
const isResolved = (jpex, dependency) => {
var _jpex$$$factories$dep;
function resolve(name, opts) {
return resolveOne(this, name, void 0, opts, []);
}
function resolveDependencies(definition, opts) {
return resolveMany(this, definition, void 0, opts, []);
}
function isResolved(dependency) {
var _this$$$factories$dep;

@@ -426,7 +386,7 @@ if (!isString(dependency)) {

if (jpex.$$resolved[dependency] != null) {
if (this.$$resolved[dependency] != null) {
return true;
}
if ((_jpex$$$factories$dep = jpex.$$factories[dependency]) == null ? void 0 : _jpex$$$factories$dep.resolved) {
if ((_this$$$factories$dep = this.$$factories[dependency]) != null && _this$$$factories$dep.resolved) {
return true;

@@ -436,8 +396,9 @@ }

return false;
};
const allResolved = (jpex, dependencies) => {
return dependencies.every(isResolved.bind(null, jpex));
};
}
function allResolved(dependencies) {
return dependencies.every(isResolved.bind(this));
}
const encase = (jpex, dependencies, fn) => {
function encase(dependencies, fn) {
const jpex = this;
let result;

@@ -447,10 +408,10 @@

/* eslint-disable no-invalid-this */
if (result && allResolved(jpex, dependencies)) {
if (result && allResolved.call(jpex, dependencies)) {
return result.apply(this, args);
}
const deps = resolveDependencies(jpex, {
const deps = resolveDependencies.call(jpex, {
dependencies
});
result = fn.apply(this, deps);
result = fn.apply(jpex, deps);
return result.apply(this, args);

@@ -462,19 +423,19 @@ /* eslint-enable */

return encased;
};
}
const clearCache = (jpex, names) => {
function clearCache(...names) {
names = ensureArray(names);
for (const key in jpex.$$factories) {
for (const key in this.$$factories) {
if (!hasLength(names) || names.includes(key)) {
jpex.$$factories[key].resolved = false;
this.$$factories[key].resolved = false;
}
}
for (const key in jpex.$$resolved) {
for (const key in this.$$resolved) {
if (!hasLength(names) || names.includes(key)) {
delete jpex.$$resolved[key];
delete this.$$resolved[key];
}
}
};
}

@@ -488,83 +449,45 @@ const defaultConfig = {

};
class Jpex {
constructor(options = {}, parent) {
_defineProperty(this, "decorate", void 0);
_defineProperty(this, "$$config", void 0);
_defineProperty(this, "$$parent", void 0);
_defineProperty(this, "$$factories", {});
_defineProperty(this, "$$resolved", {});
const {
inherit = true,
...config
} = options;
this.$$parent = parent;
this.$$config = { ...defaultConfig,
function makeJpex({
inherit = true,
...config
} = {}, parent) {
const jpex = {
$$parent: parent,
$$config: { ...defaultConfig,
...(inherit ? parent == null ? void 0 : parent.$$config : {}),
...config
};
},
$$factories: parent && inherit ? Object.create(parent.$$factories) : {},
$$resolved: {},
constant,
factory,
service,
alias,
resolve,
encase,
clearCache,
if (parent && inherit) {
this.$$factories = Object.create(parent.$$factories);
}
}
extend(config) {
return makeJpex(config, this);
},
extend(config) {
return new Jpex(config, this);
}
resolveWith(name, namedParameters, opts) {
return this.resolve(name, {
with: namedParameters,
...opts
});
},
constant(name, obj) {
return constant(this, name, obj);
}
raw(name) {
return getFactory(this, name, {}).fn;
},
factory(name, deps, fn, opts) {
return factory(this, name, deps, fn, opts);
}
service(name, deps, fn, opts) {
return service(this, name, deps, fn, opts);
}
alias(alias$1, name) {
return alias(this, alias$1, name);
}
resolve(name, opts) {
return resolve(this, name, opts);
}
resolveWith(name, namedParameters, opts) {
return resolve(this, name, {
with: namedParameters,
...opts
});
}
raw(name) {
return getFactory(this, name, {}).fn;
} // eslint-disable-next-line @typescript-eslint/no-unused-vars
encase(deps, fn) {
return encase(this, deps, fn);
}
clearCache(...names) {
return clearCache(this, names);
}
infer() {
return '';
}
infer: () => ''
};
return jpex;
}
const jpex = new Jpex();
const jpex = makeJpex();
export default jpex;
export { jpex };

@@ -5,17 +5,2 @@ 'use strict';

function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function _extends() {

@@ -54,4 +39,4 @@ _extends = Object.assign || function (target) {

function constant(jpex, name, obj) {
return jpex.factory(name, [], function () {
function constant(name, obj) {
return this.factory(name, [], function () {
return obj;

@@ -120,5 +105,8 @@ }, {

};
function factory(name, dependencies, fn, opts) {
var _this = this;
function factory(jpex, name, dependencies, fn, opts) {
var _opts$precedence, _opts$lifecycle;
if (opts === void 0) {
opts = {};
}

@@ -129,8 +117,6 @@ validateArgs(name, dependencies, fn);

dependencies = null;
}
} // eslint-disable-next-line max-len
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;
if (precedence === 'passive' && jpex.$$factories[name] != null) {
if ((opts.precedence || this.$$config.precedence) === 'passive' && this.$$factories[name] != null) {
return;

@@ -142,15 +128,19 @@ }

dependencies: dependencies,
lifecycle: lifecycle
lifecycle: opts.lifecycle || this.$$config.lifecycle
};
jpex.$$factories[name] = f;
this.$$factories[name] = f;
if (opts.alias) {
ensureArray(opts.alias).forEach(function (alias) {
return _this.alias(alias, name);
});
}
}
var validateArgs$1 = function validateArgs(name, fn) {
if (!isFunction(fn)) {
throw new Error("Factory " + name + " must be a [Function]");
function service(name, dependencies, fn, opts) {
if (opts === void 0) {
opts = {};
}
};
function service(jpex, name, dependencies, fn, opts) {
validateArgs$1(name, fn);
validateArgs(name, dependencies, fn);

@@ -164,3 +154,3 @@ function factory() {

if (opts == null ? void 0 : opts.bindToInstance) {
if (opts.bindToInstance) {
dependencies.forEach(function (key, i) {

@@ -171,17 +161,16 @@ context[key] = args[i];

args.unshift(context);
return instantiate(fn, args);
return instantiate(fn, [context].concat(args));
}
return jpex.factory(name, dependencies, factory, opts);
return this.factory(name, dependencies, factory, opts);
}
function alias(jpex, alias, name) {
if (jpex.$$factories[name] != null) {
jpex.$$factories[alias] = jpex.$$factories[name];
function alias(alias, name) {
if (this.$$factories[name] != null) {
this.$$factories[alias] = this.$$factories[name];
return;
}
if (jpex.$$factories[alias] != null) {
jpex.$$factories[name] = jpex.$$factories[alias];
if (this.$$factories[alias] != null) {
this.$$factories[name] = this.$$factories[alias];
return;

@@ -201,10 +190,6 @@ }

// inside an eval setup
if (!jpex.$$config.nodeModules) {
if (!jpex.$$config.nodeModules || !isNode()) {
return;
}
if (!isNode()) {
return;
}
try {

@@ -217,3 +202,3 @@ var value = unsafeRequire(target);

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 + "'")) {
if ((_e$message = e.message) != null && _e$message.includes != null && _e$message.includes("Cannot find module '" + target + "'")) {
// not found in node modules, just continue

@@ -276,3 +261,3 @@ return;

var validateArgs$2 = function validateArgs(name) {
var validateArgs$1 = function validateArgs(name) {
if (!isString(name)) {

@@ -294,28 +279,18 @@ throw new Error("Name must be a string, but recevied " + typeof name);

validateArgs$2(name);
var factory = getFromResolved(jpex, name);
if (factory != null) {
return factory;
if (opts === void 0) {
opts = {};
}
factory = getFromRegistry(jpex, name);
validateArgs$1(name);
var fns = [getFromResolved, getFromRegistry, getFromGlobal, getFromNodeModules];
if (factory != null) {
return factory;
}
while (fns.length) {
var factory = fns.shift()(jpex, name);
factory = getFromGlobal(jpex, name);
if (factory != null) {
return factory;
if (factory != null) {
return factory;
}
}
factory = getFromNodeModules(jpex, name);
if (factory != null) {
return factory;
}
if ((_opts$optional = opts == null ? void 0 : opts.optional) != null ? _opts$optional : jpex.$$config.optional) {
if ((_opts$optional = opts.optional) != null ? _opts$optional : jpex.$$config.optional) {
return;

@@ -345,2 +320,3 @@ }

default:
// instance
namedParameters[name] = value;

@@ -379,2 +355,6 @@ break;

var getNamedParameters = function getNamedParameters(namedParameters, opts) {
if (opts === void 0) {
opts = {};
}
if (namedParameters) {

@@ -384,3 +364,3 @@ return namedParameters;

if (opts == null ? void 0 : opts.with) {
if (opts.with) {
return _extends({}, opts.with);

@@ -427,6 +407,4 @@ }

switch (name) {
case NAMED_PARAMS:
case "type:jpex/NamedParameters":
return namedParameters;
if (name === NAMED_PARAMS || name === "type:jpex/NamedParameters") {
return namedParameters;
}

@@ -448,3 +426,3 @@

var resolveMany = function resolveMany(jpex, definition, namedParameters, opts, stack) {
if (!hasLength(definition == null ? void 0 : definition.dependencies)) {
if (!hasLength(definition.dependencies)) {
return [];

@@ -464,10 +442,10 @@ }

var resolve = function resolve(jpex, name, opts) {
return resolveOne(jpex, name, void 0, opts, []);
};
var resolveDependencies = function resolveDependencies(jpex, definition, opts) {
return resolveMany(jpex, definition, void 0, opts, []);
};
var isResolved = function isResolved(jpex, dependency) {
var _jpex$$$factories$dep;
function resolve(name, opts) {
return resolveOne(this, name, void 0, opts, []);
}
function resolveDependencies(definition, opts) {
return resolveMany(this, definition, void 0, opts, []);
}
function isResolved(dependency) {
var _this$$$factories$dep;

@@ -478,7 +456,7 @@ if (!isString(dependency)) {

if (jpex.$$resolved[dependency] != null) {
if (this.$$resolved[dependency] != null) {
return true;
}
if ((_jpex$$$factories$dep = jpex.$$factories[dependency]) == null ? void 0 : _jpex$$$factories$dep.resolved) {
if ((_this$$$factories$dep = this.$$factories[dependency]) != null && _this$$$factories$dep.resolved) {
return true;

@@ -488,8 +466,9 @@ }

return false;
};
var allResolved = function allResolved(jpex, dependencies) {
return dependencies.every(isResolved.bind(null, jpex));
};
}
function allResolved(dependencies) {
return dependencies.every(isResolved.bind(this));
}
var encase = function encase(jpex, dependencies, fn) {
function encase(dependencies, fn) {
var jpex = this;
var result;

@@ -503,10 +482,10 @@

/* eslint-disable no-invalid-this */
if (result && allResolved(jpex, dependencies)) {
if (result && allResolved.call(jpex, dependencies)) {
return result.apply(this, args);
}
var deps = resolveDependencies(jpex, {
var deps = resolveDependencies.call(jpex, {
dependencies: dependencies
});
result = fn.apply(this, deps);
result = fn.apply(jpex, deps);
return result.apply(this, args);

@@ -518,19 +497,23 @@ /* eslint-enable */

return encased;
};
}
var clearCache = function clearCache(jpex, names) {
function clearCache() {
for (var _len = arguments.length, names = new Array(_len), _key = 0; _key < _len; _key++) {
names[_key] = arguments[_key];
}
names = ensureArray(names);
for (var key in jpex.$$factories) {
for (var key in this.$$factories) {
if (!hasLength(names) || names.includes(key)) {
jpex.$$factories[key].resolved = false;
this.$$factories[key].resolved = false;
}
}
for (var _key in jpex.$$resolved) {
if (!hasLength(names) || names.includes(_key)) {
delete jpex.$$resolved[_key];
for (var _key2 in this.$$resolved) {
if (!hasLength(names) || names.includes(_key2)) {
delete this.$$resolved[_key2];
}
}
};
}

@@ -544,91 +527,41 @@ var defaultConfig = {

};
function makeJpex(_temp, parent) {
var _ref = _temp === void 0 ? {} : _temp,
_ref$inherit = _ref.inherit,
inherit = _ref$inherit === void 0 ? true : _ref$inherit,
config = _objectWithoutPropertiesLoose(_ref, ["inherit"]);
var Jpex = /*#__PURE__*/function () {
function Jpex(options, parent) {
if (options === void 0) {
options = {};
var jpex = {
$$parent: parent,
$$config: _extends({}, defaultConfig, inherit ? parent == null ? void 0 : parent.$$config : {}, config),
$$factories: parent && inherit ? Object.create(parent.$$factories) : {},
$$resolved: {},
constant: constant,
factory: factory,
service: service,
alias: alias,
resolve: resolve,
encase: encase,
clearCache: clearCache,
extend: function extend(config) {
return makeJpex(config, this);
},
resolveWith: function resolveWith(name, namedParameters, opts) {
return this.resolve(name, _extends({
with: namedParameters
}, opts));
},
raw: function raw(name) {
return getFactory(this, name, {}).fn;
},
infer: function infer() {
return '';
}
_defineProperty(this, "decorate", void 0);
_defineProperty(this, "$$config", void 0);
_defineProperty(this, "$$parent", void 0);
_defineProperty(this, "$$factories", {});
_defineProperty(this, "$$resolved", {});
var _options = options,
_options$inherit = _options.inherit,
inherit = _options$inherit === void 0 ? true : _options$inherit,
config = _objectWithoutPropertiesLoose(_options, ["inherit"]);
this.$$parent = parent;
this.$$config = _extends({}, defaultConfig, inherit ? parent == null ? void 0 : parent.$$config : {}, config);
if (parent && inherit) {
this.$$factories = Object.create(parent.$$factories);
}
}
var _proto = Jpex.prototype;
_proto.extend = function extend(config) {
return new Jpex(config, this);
};
return jpex;
}
_proto.constant = function constant$1(name, obj) {
return constant(this, name, obj);
};
var jpex = makeJpex();
_proto.factory = function factory$1(name, deps, fn, opts) {
return factory(this, name, deps, fn, opts);
};
_proto.service = function service$1(name, deps, fn, opts) {
return service(this, name, deps, fn, opts);
};
_proto.alias = function alias$1(_alias, name) {
return alias(this, _alias, name);
};
_proto.resolve = function resolve$1(name, opts) {
return resolve(this, name, opts);
};
_proto.resolveWith = function resolveWith(name, namedParameters, opts) {
return resolve(this, name, _extends({
with: namedParameters
}, opts));
};
_proto.raw = function raw(name) {
return getFactory(this, name, {}).fn;
} // eslint-disable-next-line @typescript-eslint/no-unused-vars
;
_proto.encase = function encase$1(deps, fn) {
return encase(this, deps, fn);
};
_proto.clearCache = function clearCache$1() {
for (var _len = arguments.length, names = new Array(_len), _key = 0; _key < _len; _key++) {
names[_key] = arguments[_key];
}
return clearCache(this, names);
};
_proto.infer = function infer() {
return '';
};
return Jpex;
}();
var jpex = new Jpex();
exports.default = jpex;
exports.jpex = jpex;
{
"name": "jpex",
"version": "4.1.0",
"version": "4.2.0",
"description": "Javascript Prototype Extension",
"main": "dist/cjs/jpex.js",
"module": "dist/es/jpex.js",
"types": "dist/es/index.d.ts",
"types": "dist/ts/index.d.ts",
"scripts": {

@@ -13,6 +13,6 @@ "clear-cache": "rm -rf node_modules/.cache",

"coverage": "nyc ava",
"lint": "eslint './src/**/*.ts' && tsc --noEmit",
"lint": "eslint './src/**/*.ts' --fix && tsc --noEmit",
"build:prepare": "rm -rf dist",
"build:js": "rollup --config ./rollup.config.js",
"build:ts": "tsc -d --outDir dist/es --emitDeclarationOnly ./src/index.ts",
"build:ts": "tsc -d --outDir dist/ts --emitDeclarationOnly ./src/index.ts",
"build:post": "node ./postbuild-checks.js",

@@ -33,3 +33,3 @@ "build": "yarn build:prepare && yarn build:js && yarn build:ts && yarn build:post",

"src/**/*.{js,ts}": [
"eslint -c ./.eslintrc.js",
"eslint",
"git add"

@@ -64,3 +64,2 @@ ]

"@commitlint/config-conventional": "^8.3.4",
"@team-griffin/eslint-config": "^3.2.0",
"@types/babel__core": "^7.1.9",

@@ -75,2 +74,3 @@ "@types/mocha": "^8.0.0",

"eslint": "^7.5.0",
"eslint-plugin-import": "^2.22.1",
"husky": "^4.0.3",

@@ -87,4 +87,4 @@ "lint-staged": "^8.1.5",

"dependencies": {
"@jpex-js/babel-plugin": "^1.0.0"
"@jpex-js/babel-plugin": "^1.2.0"
}
}

@@ -19,4 +19,30 @@

- [API](#api)
- [jpex](#jpex)
- [types](#types)
+ [jpex](#jpex)
- [constant](#jpexconstant)
- [factory](#jpexfactory)
* [lifecycle](#lifecycle)
* [precedence](#precedence)
* [bindToInstance](#bindtoinstance)
* [alias](#alias)
- [service](#jpexservice)
- [alias](#jpexalias)
- [resolve](#jpexresolve)
* [optional](#optional)
* [with](#with)
- [resolveWith](#jpexresolvewith)
- [encase](#jpexencase)
- [extend](#jpexextend)
* [inherit](#inherit)
* [lifecycle](#lifecycle-1)
* [precedence](#precedence-1)
* [optional](#optional-1)
* [nodeModules](#nodemodules)
* [globals](#globals)
- [raw](#jpexraw)
- [clearCache](#jpexclearcache)
- [infer](#jpexinfer)
+ [Types](#types)
- [Jpex](#jpex)
- [NodeModule](#nodemodule)
- [Global](#global)
- [caveats](#caveats)

@@ -34,5 +60,5 @@ - [react](#react)

### Plugin
Jpex uses babel to infer type interfaces at build time. You can do this with one of several methods:
[@jpex-js/babel-plugin](https://github.com/jpex-js/babel-plugin)
[@jpex-js/rollup-plugin](https://github.com/jpex-js/rollup-plugin)
Jpex uses babel to infer type interfaces at build time. You can do this with one of several methods:
[@jpex-js/babel-plugin](https://github.com/jpex-js/babel-plugin)
[@jpex-js/rollup-plugin](https://github.com/jpex-js/rollup-plugin)
[@jpex-js/webpack-plugin](https://github.com/jpex-js/webpack-loader)

@@ -52,7 +78,7 @@

import jpex from 'jpex';
import { IFoo, IBah } from './types';
import { Foo, Bah } from './types';
jpex.factory<IFoo>((bah: IBah) => bah.baz);
jpex.factory<Foo>((bah: Bah) => bah.baz);
const foo = jpex.resolve<IFoo>();
const foo = jpex.resolve<Foo>();
```

@@ -63,3 +89,3 @@

## Registering Dependencies
Services and factories are small modules or functions that provide a reusable or common piece of functionality.
Services and factories are small modules or functions that provide a common piece of functionality.

@@ -77,9 +103,9 @@ ### factories

```ts
type MyService = { method: () => any };
class MyService = {
method: (): any {
// ...
}
};
jpex.service<MyService>(function(){
this.method = function(){
...
};
});
jpex.service(MyService);
```

@@ -103,3 +129,3 @@

### dependent factories
You can also request a dependency from within another factory:
A factory can request another dependency and jpex will resolve it on the fly:
```ts

@@ -111,10 +137,14 @@ jpex.constant<MyConstant>('foo');

});
jpex.resolve<MyFactory>(); // "my constant is foo"
```
### encase
Or you can *encase* a function so that dependencies are injected into it on-the-fly:
Or you can *encase* a regular function so that dependencies are injected into it when called:
```ts
const myFn = jpex.encase((value: MyFactory) => (arg1, arg2) => {
const fn = jpex.encase((value: MyFactory) => (arg1, arg2) => {
return value + arg1 + arg2;
});
fn(1, 2);
```

@@ -144,2 +174,4 @@

> By default jpex will automatically resolve global types like Window or Document. In a node environment it will also be able to resolve node_modules.
The following options can be provided for both factories and services:

@@ -177,2 +209,8 @@

##### alias
```ts
string | string[]
```
Creates aliases for the factory. This is essentially just shorthand for writing `jpex.factory(...); jpex.alias(...);`
#### jpex.service

@@ -191,3 +229,3 @@ ```ts

jpex.service<Foo>(Foo);
jpex.service(Foo);
```

@@ -219,8 +257,2 @@

##### with
```ts
object
```
Lets you pass in static values to use when resolving dependencies. This should be used as an escape hatch, as `resolveWith` was created specifically for this purpose.
#### jpex.resolveWith

@@ -289,3 +321,3 @@ ```ts

When trying to resolve a tdependency, should it attempt to import the dependency from node modules?
When trying to resolve a dependency, should it attempt to import the it from node modules?

@@ -391,12 +423,12 @@ ##### globals

```tsx
import base, { Provider, useJpex } from 'jpex';
import { Provider } from 'react-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(
<Provider value={jpex}>
<Provider
inherit={false}
// register our stub dependency on an isolated container
onMount={jpex => jpex.constant<SaveData>(saveData)}
>
{/* when we render MyComponent, it will be given our stubbed dependency */}

@@ -403,0 +435,0 @@ <MyComponent/>

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