contexify
Version:
A TypeScript library providing a powerful dependency injection container with context-based IoC capabilities, inspired by LoopBack's Context system.
97 lines • 3.66 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
function isBindingKey(selector) {
if (selector == null || typeof selector !== "object") return false;
return typeof selector.key === "string" && typeof selector.deepProperty === "function";
}
__name(isBindingKey, "isBindingKey");
function isBindingAddress(bindingSelector) {
return typeof bindingSelector !== "function" && (typeof bindingSelector === "string" || // Check for binding key by duck typing
// `bindingSelector instanceof BindingKey` is not always reliable as the
// `contexify` module might be loaded from multiple locations if
// `npm install` does not dedupe or there are mixed versions in the tree
isBindingKey(bindingSelector));
}
__name(isBindingAddress, "isBindingAddress");
function isBindingTagFilter(filter) {
if (filter == null || !("bindingTagPattern" in filter)) return false;
const tagPattern = filter.bindingTagPattern;
return tagPattern instanceof RegExp || typeof tagPattern === "string" || typeof tagPattern === "object";
}
__name(isBindingTagFilter, "isBindingTagFilter");
const ANY_TAG_VALUE = /* @__PURE__ */ __name((_tagValue, tagName, tagMap) => tagName in tagMap, "ANY_TAG_VALUE");
function includesTagValue(...itemValues) {
return (tagValue) => {
return itemValues.some((itemValue) => (
// The tag value equals the item value
tagValue === itemValue || // The tag value contains the item value
Array.isArray(tagValue) && tagValue.includes(itemValue)
));
};
}
__name(includesTagValue, "includesTagValue");
function filterByTag(tagPattern) {
let filter;
let regex;
if (tagPattern instanceof RegExp) {
regex = tagPattern;
}
if (typeof tagPattern === "string" && (tagPattern.includes("*") || tagPattern.includes("?"))) {
regex = wildcardToRegExp(tagPattern);
}
if (regex != null) {
filter = /* @__PURE__ */ __name((b) => b.tagNames.some((t) => regex?.test(t) ?? false), "filter");
} else if (typeof tagPattern === "string") {
filter = /* @__PURE__ */ __name((b) => b.tagNames.includes(tagPattern), "filter");
} else {
const tagMap = tagPattern;
filter = /* @__PURE__ */ __name((b) => {
for (const t in tagMap) {
if (!matchTagValue(tagMap[t], t, b.tagMap)) return false;
}
return true;
}, "filter");
}
const tagFilter = filter;
tagFilter.bindingTagPattern = regex ?? tagPattern;
return tagFilter;
}
__name(filterByTag, "filterByTag");
function matchTagValue(tagValueOrMatcher, tagName, tagMap) {
const tagValue = tagMap[tagName];
if (tagValue === tagValueOrMatcher) return true;
if (typeof tagValueOrMatcher === "function") {
return tagValueOrMatcher(tagValue, tagName, tagMap);
}
return false;
}
__name(matchTagValue, "matchTagValue");
function filterByKey(keyPattern) {
if (typeof keyPattern === "string") {
const regex = wildcardToRegExp(keyPattern);
return (binding) => regex.test(binding.key);
}
if (keyPattern instanceof RegExp) {
return (binding) => keyPattern.test(binding.key);
}
if (typeof keyPattern === "function") {
return keyPattern;
}
return () => true;
}
__name(filterByKey, "filterByKey");
function wildcardToRegExp(pattern) {
let regexp = pattern.replace(/[-[\]/{}()+.\\^$|:]/g, "\\$&");
regexp = regexp.replace(/\*/g, "[^.:]*").replace(/\?/g, "[^.:]");
return new RegExp(`^${regexp}$`);
}
__name(wildcardToRegExp, "wildcardToRegExp");
export {
ANY_TAG_VALUE,
filterByKey,
filterByTag,
includesTagValue,
isBindingAddress,
isBindingTagFilter
};
//# sourceMappingURL=binding-filter.js.map