@loopback/context
Advanced tools
Comparing version 3.0.0 to 3.1.0
@@ -6,2 +6,13 @@ # Change Log | ||
# [3.1.0](https://github.com/strongloop/loopback-next/compare/@loopback/context@3.0.0...@loopback/context@3.1.0) (2020-03-17) | ||
### Features | ||
* **context:** introduce TagValueMatcher for more flexible tag matching ([deaf2ed](https://github.com/strongloop/loopback-next/commit/deaf2eda29421e73244d3d27006b502c7dcc25e2)) | ||
# [3.0.0](https://github.com/strongloop/loopback-next/compare/@loopback/context@2.1.1...@loopback/context@3.0.0) (2020-03-05) | ||
@@ -8,0 +19,0 @@ |
import { Binding, BindingTag } from './binding'; | ||
import { BindingAddress } from './binding-key'; | ||
import { MapObject } from './value-promise'; | ||
/** | ||
@@ -66,2 +67,14 @@ * A function that filters bindings. It returns `true` to select a given | ||
/** | ||
* A function to check if a given tag value is matched for `filterByTag` | ||
*/ | ||
export interface TagValueMatcher { | ||
/** | ||
* Check if the given tag value matches the search criteria | ||
* @param tagValue - Tag value from the binding | ||
* @param tagName - Tag name | ||
* @param tagMap - Tag map from the binding | ||
*/ | ||
(tagValue: unknown, tagName: string, tagMap: MapObject<unknown>): boolean; | ||
} | ||
/** | ||
* A symbol that can be used to match binding tags by name regardless of the | ||
@@ -80,4 +93,10 @@ * value. | ||
*/ | ||
export declare const ANY_TAG_VALUE: unique symbol; | ||
export declare const ANY_TAG_VALUE: TagValueMatcher; | ||
/** | ||
* Create a tag value matcher function that returns `true` if the target tag | ||
* value equals to the item value or is an array that includes the item value. | ||
* @param itemValue - Tag item value | ||
*/ | ||
export declare function includesTagValue(itemValue: unknown): TagValueMatcher; | ||
/** | ||
* Create a binding filter for the tag pattern | ||
@@ -84,0 +103,0 @@ * @param tagPattern - Binding tag name, regexp, or object |
@@ -59,4 +59,19 @@ "use strict"; | ||
*/ | ||
exports.ANY_TAG_VALUE = Symbol.for('loopback.AnyTagValue'); | ||
exports.ANY_TAG_VALUE = (tagValue, tagName, tagMap) => tagName in tagMap; | ||
/** | ||
* Create a tag value matcher function that returns `true` if the target tag | ||
* value equals to the item value or is an array that includes the item value. | ||
* @param itemValue - Tag item value | ||
*/ | ||
function includesTagValue(itemValue) { | ||
return tagValue => { | ||
return ( | ||
// The tag value equals the item value | ||
tagValue === itemValue || | ||
// The tag value contains the item value | ||
(Array.isArray(tagValue) && tagValue.includes(itemValue))); | ||
}; | ||
} | ||
exports.includesTagValue = includesTagValue; | ||
/** | ||
* Create a binding filter for the tag pattern | ||
@@ -89,7 +104,4 @@ * @param tagPattern - Binding tag name, regexp, or object | ||
filter = b => { | ||
for (const t in tagPattern) { | ||
if (tagMap[t] === exports.ANY_TAG_VALUE) | ||
return t in b.tagMap; | ||
// One tag name/value does not match | ||
if (b.tagMap[t] !== tagMap[t]) | ||
for (const t in tagMap) { | ||
if (!matchTagValue(tagMap[t], t, b.tagMap)) | ||
return false; | ||
@@ -107,2 +119,11 @@ } | ||
exports.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; | ||
} | ||
/** | ||
@@ -109,0 +130,0 @@ * Create a binding filter from key pattern |
{ | ||
"name": "@loopback/context", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "LoopBack's container for Inversion of Control", | ||
@@ -21,3 +21,3 @@ "engines": { | ||
"dependencies": { | ||
"@loopback/metadata": "^2.0.0", | ||
"@loopback/metadata": "^2.0.1", | ||
"debug": "^4.1.1", | ||
@@ -29,5 +29,5 @@ "p-event": "^4.1.0", | ||
"devDependencies": { | ||
"@loopback/build": "^4.0.0", | ||
"@loopback/eslint-config": "^6.0.0", | ||
"@loopback/testlab": "^2.0.0", | ||
"@loopback/build": "^4.0.1", | ||
"@loopback/eslint-config": "^6.0.1", | ||
"@loopback/testlab": "^2.0.1", | ||
"@types/bluebird": "^3.5.30", | ||
@@ -61,3 +61,3 @@ "@types/debug": "^4.1.5", | ||
}, | ||
"gitHead": "baf9c89decba06b826204cd43e58a11be05408f8" | ||
"gitHead": "85a906121febac2dbd2d4adcdc21b5939a770951" | ||
} |
@@ -115,2 +115,15 @@ // Copyright IBM Corp. 2019,2020. All Rights Reserved. | ||
/** | ||
* A function to check if a given tag value is matched for `filterByTag` | ||
*/ | ||
export interface TagValueMatcher { | ||
/** | ||
* Check if the given tag value matches the search criteria | ||
* @param tagValue - Tag value from the binding | ||
* @param tagName - Tag name | ||
* @param tagMap - Tag map from the binding | ||
*/ | ||
(tagValue: unknown, tagName: string, tagMap: MapObject<unknown>): boolean; | ||
} | ||
/** | ||
* A symbol that can be used to match binding tags by name regardless of the | ||
@@ -129,5 +142,22 @@ * value. | ||
*/ | ||
export const ANY_TAG_VALUE = Symbol.for('loopback.AnyTagValue'); | ||
export const ANY_TAG_VALUE: TagValueMatcher = (tagValue, tagName, tagMap) => | ||
tagName in tagMap; | ||
/** | ||
* Create a tag value matcher function that returns `true` if the target tag | ||
* value equals to the item value or is an array that includes the item value. | ||
* @param itemValue - Tag item value | ||
*/ | ||
export function includesTagValue(itemValue: unknown): TagValueMatcher { | ||
return tagValue => { | ||
return ( | ||
// The tag value equals the item value | ||
tagValue === itemValue || | ||
// The tag value contains the item value | ||
(Array.isArray(tagValue) && tagValue.includes(itemValue)) | ||
); | ||
}; | ||
} | ||
/** | ||
* Create a binding filter for the tag pattern | ||
@@ -161,6 +191,4 @@ * @param tagPattern - Binding tag name, regexp, or object | ||
filter = b => { | ||
for (const t in tagPattern) { | ||
if (tagMap[t] === ANY_TAG_VALUE) return t in b.tagMap; | ||
// One tag name/value does not match | ||
if (b.tagMap[t] !== tagMap[t]) return false; | ||
for (const t in tagMap) { | ||
if (!matchTagValue(tagMap[t], t, b.tagMap)) return false; | ||
} | ||
@@ -177,2 +205,16 @@ // All tag name/value pairs match | ||
function matchTagValue( | ||
tagValueOrMatcher: unknown, | ||
tagName: string, | ||
tagMap: MapObject<unknown>, | ||
) { | ||
const tagValue = tagMap[tagName]; | ||
if (tagValue === tagValueOrMatcher) return true; | ||
if (typeof tagValueOrMatcher === 'function') { | ||
return (tagValueOrMatcher as TagValueMatcher)(tagValue, tagName, tagMap); | ||
} | ||
return false; | ||
} | ||
/** | ||
@@ -179,0 +221,0 @@ * Create a binding filter from key pattern |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
607641
13394
Updated@loopback/metadata@^2.0.1