Added MetadataValueInterface and MetadataMapInterface

This commit is contained in:
Giuseppe Digilio
2019-03-08 15:48:17 +01:00
parent ab42f4b190
commit a21bd40925
6 changed files with 49 additions and 35 deletions

View File

@@ -1,6 +1,6 @@
import { isEmpty, isNotUndefined, isUndefined } from '../../shared/empty.util';
import {
MetadataMap,
MetadataMapInterface,
MetadataValue,
MetadataValueFilter,
MetadatumViewModel
@@ -25,23 +25,23 @@ export class Metadata {
/**
* Gets all matching metadata in the map(s).
*
* @param {MetadataMap|MetadataMap[]} mapOrMaps The source map(s). When multiple maps are given, they will be
* @param {MetadataMapInterface|MetadataMapInterface[]} mapOrMaps The source map(s). When multiple maps are given, they will be
* checked in order, and only values from the first with at least one match will be returned.
* @param {string|string[]} keyOrKeys The metadata key(s) in scope. Wildcards are supported; see above.
* @param {MetadataValueFilter} filter The value filter to use. If unspecified, no filtering will be done.
* @returns {MetadataValue[]} the matching values or an empty array.
*/
public static all(mapOrMaps: MetadataMap | MetadataMap[], keyOrKeys: string | string[],
public static all(mapOrMaps: MetadataMapInterface | MetadataMapInterface[], keyOrKeys: string | string[],
filter?: MetadataValueFilter): MetadataValue[] {
const mdMaps: MetadataMap[] = mapOrMaps instanceof Array ? mapOrMaps : [mapOrMaps];
const mdMaps: MetadataMapInterface[] = mapOrMaps instanceof Array ? mapOrMaps : [mapOrMaps];
const matches: MetadataValue[] = [];
for (const mdMap of mdMaps) {
for (const mdKey of Metadata.resolveKeys(mdMap, keyOrKeys)) {
const candidates = mdMap[mdKey];
if (candidates) {
for (const candidate of candidates) {
if (Metadata.valueMatches(candidate, filter)) {
matches.push(candidate);
if (Metadata.valueMatches(candidate as MetadataValue, filter)) {
matches.push(candidate as MetadataValue);
}
}
}
@@ -56,13 +56,13 @@ export class Metadata {
/**
* Like [[Metadata.all]], but only returns string values.
*
* @param {MetadataMap|MetadataMap[]} mapOrMaps The source map(s). When multiple maps are given, they will be
* @param {MetadataMapInterface|MetadataMapInterface[]} mapOrMaps The source map(s). When multiple maps are given, they will be
* checked in order, and only values from the first with at least one match will be returned.
* @param {string|string[]} keyOrKeys The metadata key(s) in scope. Wildcards are supported; see above.
* @param {MetadataValueFilter} filter The value filter to use. If unspecified, no filtering will be done.
* @returns {string[]} the matching string values or an empty array.
*/
public static allValues(mapOrMaps: MetadataMap | MetadataMap[], keyOrKeys: string | string[],
public static allValues(mapOrMaps: MetadataMapInterface | MetadataMapInterface[], keyOrKeys: string | string[],
filter?: MetadataValueFilter): string[] {
return Metadata.all(mapOrMaps, keyOrKeys, filter).map((mdValue) => mdValue.value);
}
@@ -70,17 +70,17 @@ export class Metadata {
/**
* Gets the first matching MetadataValue object in the map(s), or `undefined`.
*
* @param {MetadataMap|MetadataMap[]} mapOrMaps The source map(s).
* @param {MetadataMapInterface|MetadataMapInterface[]} mapOrMaps The source map(s).
* @param {string|string[]} keyOrKeys The metadata key(s) in scope. Wildcards are supported; see above.
* @param {MetadataValueFilter} filter The value filter to use. If unspecified, no filtering will be done.
* @returns {MetadataValue} the first matching value, or `undefined`.
*/
public static first(mdMapOrMaps: MetadataMap | MetadataMap[], keyOrKeys: string | string[],
public static first(mdMapOrMaps: MetadataMapInterface | MetadataMapInterface[], keyOrKeys: string | string[],
filter?: MetadataValueFilter): MetadataValue {
const mdMaps: MetadataMap[] = mdMapOrMaps instanceof Array ? mdMapOrMaps : [mdMapOrMaps];
const mdMaps: MetadataMapInterface[] = mdMapOrMaps instanceof Array ? mdMapOrMaps : [mdMapOrMaps];
for (const mdMap of mdMaps) {
for (const key of Metadata.resolveKeys(mdMap, keyOrKeys)) {
const values: MetadataValue[] = mdMap[key];
const values: MetadataValue[] = mdMap[key] as MetadataValue[];
if (values) {
return values.find((value: MetadataValue) => Metadata.valueMatches(value, filter));
}
@@ -91,12 +91,12 @@ export class Metadata {
/**
* Like [[Metadata.first]], but only returns a string value, or `undefined`.
*
* @param {MetadataMap|MetadataMap[]} mapOrMaps The source map(s).
* @param {MetadataMapInterface|MetadataMapInterface[]} mapOrMaps The source map(s).
* @param {string|string[]} keyOrKeys The metadata key(s) in scope. Wildcards are supported; see above.
* @param {MetadataValueFilter} filter The value filter to use. If unspecified, no filtering will be done.
* @returns {string} the first matching string value, or `undefined`.
*/
public static firstValue(mdMapOrMaps: MetadataMap | MetadataMap[], keyOrKeys: string | string[],
public static firstValue(mdMapOrMaps: MetadataMapInterface | MetadataMapInterface[], keyOrKeys: string | string[],
filter?: MetadataValueFilter): string {
const value = Metadata.first(mdMapOrMaps, keyOrKeys, filter);
return isUndefined(value) ? undefined : value.value;
@@ -105,12 +105,12 @@ export class Metadata {
/**
* Checks for a matching metadata value in the given map(s).
*
* @param {MetadataMap|MetadataMap[]} mapOrMaps The source map(s).
* @param {MetadataMapInterface|MetadataMapInterface[]} mapOrMaps The source map(s).
* @param {string|string[]} keyOrKeys The metadata key(s) in scope. Wildcards are supported; see above.
* @param {MetadataValueFilter} filter The value filter to use. If unspecified, no filtering will be done.
* @returns {boolean} whether a match is found.
*/
public static has(mdMapOrMaps: MetadataMap | MetadataMap[], keyOrKeys: string | string[],
public static has(mdMapOrMaps: MetadataMapInterface | MetadataMapInterface[], keyOrKeys: string | string[],
filter?: MetadataValueFilter): boolean {
return isNotUndefined(Metadata.first(mdMapOrMaps, keyOrKeys, filter));
}
@@ -146,10 +146,10 @@ export class Metadata {
/**
* Gets the list of keys in the map limited by, and in the order given by `keyOrKeys`.
*
* @param {MetadataMap} mdMap The source map.
* @param {MetadataMapInterface} mdMap The source map.
* @param {string|string[]} keyOrKeys The metadata key(s) in scope. Wildcards are supported; see above.
*/
private static resolveKeys(mdMap: MetadataMap = {}, keyOrKeys: string | string[]): string[] {
private static resolveKeys(mdMap: MetadataMapInterface = {}, keyOrKeys: string | string[]): string[] {
const inputKeys: string[] = keyOrKeys instanceof Array ? keyOrKeys : [keyOrKeys];
const outputKeys: string[] = [];
for (const inputKey of inputKeys) {
@@ -168,12 +168,12 @@ export class Metadata {
}
/**
* Creates an array of MetadatumViewModels from an existing MetadataMap.
* Creates an array of MetadatumViewModels from an existing MetadataMapInterface.
*
* @param {MetadataMap} mdMap The source map.
* @param {MetadataMapInterface} mdMap The source map.
* @returns {MetadatumViewModel[]} List of metadata view models based on the source map.
*/
public static toViewModelList(mdMap: MetadataMap): MetadatumViewModel[] {
public static toViewModelList(mdMap: MetadataMapInterface): MetadatumViewModel[] {
let metadatumList: MetadatumViewModel[] = [];
Object.keys(mdMap)
.sort()
@@ -193,13 +193,13 @@ export class Metadata {
}
/**
* Creates an MetadataMap from an existing array of MetadatumViewModels.
* Creates an MetadataMapInterface from an existing array of MetadatumViewModels.
*
* @param {MetadatumViewModel[]} viewModelList The source list.
* @returns {MetadataMap} Map with metadata values based on the source list.
* @returns {MetadataMapInterface} Map with metadata values based on the source list.
*/
public static toMetadataMap(viewModelList: MetadatumViewModel[]): MetadataMap {
const metadataMap: MetadataMap = {};
public static toMetadataMap(viewModelList: MetadatumViewModel[]): MetadataMapInterface {
const metadataMap: MetadataMapInterface = {};
const groupedList = groupBy(viewModelList, (viewModel) => viewModel.key);
Object.keys(groupedList)
.sort()