@gooddata/js-utils
Advanced tools
Comparing version 3.10.7 to 3.10.8
import { EmbeddedGdc } from '@gooddata/typings'; | ||
export declare const EXTERNAL_DATE_FILTER_FORMAT = "YYYY-MM-DD"; | ||
export interface IExternalFiltersObject { | ||
attributeFilters: ITransformedAttributeFilterItem[]; | ||
dateFilters: ITransformedDateFilterItem[]; | ||
} | ||
export interface ITransformedDateFilterItem { | ||
@@ -20,3 +25,3 @@ granularity?: string; | ||
export declare function isValidFiltersFormat(filters: any[]): boolean; | ||
export declare function transformFilterContext(filters: EmbeddedGdc.FilterItem[]): ITransformedFilterItem[]; | ||
export declare function transformFilterContext(filters: EmbeddedGdc.FilterItem[]): IExternalFiltersObject; | ||
export declare function isTransformedDateFilterItem(filterItem: ITransformedFilterItem): filterItem is ITransformedDateFilterItem; | ||
@@ -23,0 +28,0 @@ export declare function isTransformedAttributeFilterItem(filterItem: ITransformedFilterItem): filterItem is ITransformedAttributeFilterItem; |
@@ -6,2 +6,3 @@ "use strict"; | ||
var typings_1 = require("@gooddata/typings"); | ||
exports.EXTERNAL_DATE_FILTER_FORMAT = 'YYYY-MM-DD'; | ||
var DATE_FORMAT_REGEX = /^\d{4}-\d{2}-\d{2}$/; | ||
@@ -11,11 +12,31 @@ exports.ALL_TIME_GRANULARITY = 'ALL_TIME_GRANULARITY'; | ||
if (typings_1.EmbeddedGdc.isAbsoluteDateFilter(filterItem)) { | ||
var _a = filterItem.absoluteDateFilter, from = _a.from, to = _a.to; | ||
return lodash_1.isString(from) && lodash_1.isString(to) && DATE_FORMAT_REGEX.test(from) && DATE_FORMAT_REGEX.test(to); | ||
var _a = filterItem.absoluteDateFilter, from = _a.from, to = _a.to, dataSet = _a.dataSet; | ||
var _b = getObjectUriIdentifier(dataSet), uri = _b.uri, identifier = _b.identifier; | ||
return (lodash_1.isString(uri) || lodash_1.isString(identifier)) | ||
&& lodash_1.isString(from) && lodash_1.isString(to) | ||
&& DATE_FORMAT_REGEX.test(from) && DATE_FORMAT_REGEX.test(to); | ||
} | ||
else { | ||
var _b = filterItem.relativeDateFilter, from = _b.from, to = _b.to; | ||
return lodash_1.isNumber(from) && lodash_1.isNumber(to) && from <= to; | ||
var _c = filterItem.relativeDateFilter, from = _c.from, to = _c.to, dataSet = _c.dataSet; | ||
var _d = getObjectUriIdentifier(dataSet), uri = _d.uri, identifier = _d.identifier; | ||
return (lodash_1.isString(uri) || lodash_1.isString(identifier)) && lodash_1.isNumber(from) && lodash_1.isNumber(to); | ||
} | ||
} | ||
exports.isValidDateFilterFormat = isValidDateFilterFormat; | ||
function isValidAttributeFilterFormat(filterItem) { | ||
if (!typings_1.EmbeddedGdc.isAttributeFilter(filterItem)) { | ||
return false; | ||
} | ||
if (typings_1.EmbeddedGdc.isPositiveAttributeFilter(filterItem)) { | ||
var _a = filterItem.positiveAttributeFilter, displayForm = _a.displayForm, attributeElements = _a.in; | ||
var _b = getObjectUriIdentifier(displayForm), uri = _b.uri, identifier = _b.identifier; | ||
return (lodash_1.isString(uri) || lodash_1.isString(identifier)) && attributeElements.length !== 0; | ||
} | ||
else { | ||
var displayForm = filterItem.negativeAttributeFilter.displayForm; | ||
var _c = getObjectUriIdentifier(displayForm), uri = _c.uri, identifier = _c.identifier; | ||
// attributeElements could be empty in case of setting All Value | ||
return lodash_1.isString(uri) || lodash_1.isString(identifier); | ||
} | ||
} | ||
function isValidFilterItemFormat(filterItem) { | ||
@@ -26,3 +47,3 @@ if (typings_1.EmbeddedGdc.isDateFilter(filterItem)) { | ||
else { | ||
return typings_1.EmbeddedGdc.isAttributeFilter(filterItem); | ||
return isValidAttributeFilterFormat(filterItem); | ||
} | ||
@@ -87,10 +108,20 @@ } | ||
function transformFilterContext(filters) { | ||
var defaultFiltersObject = { | ||
attributeFilters: [], | ||
dateFilters: [] | ||
}; | ||
if (lodash_1.isEmpty(filters)) { | ||
return []; | ||
return defaultFiltersObject; | ||
} | ||
return filters.map(function (filterItem) { | ||
return typings_1.EmbeddedGdc.isDateFilter(filterItem) | ||
? transformDateFilterItem(filterItem) | ||
: transformAttributeFilterItem(filterItem); | ||
}); | ||
return filters.reduce(function (externalFilters, filterItem) { | ||
if (typings_1.EmbeddedGdc.isDateFilter(filterItem)) { | ||
var dateFilter = transformDateFilterItem(filterItem); | ||
externalFilters.dateFilters.push(dateFilter); | ||
} | ||
else { | ||
var attributeFilter = transformAttributeFilterItem(filterItem); | ||
externalFilters.attributeFilters.push(attributeFilter); | ||
} | ||
return externalFilters; | ||
}, defaultFiltersObject); | ||
} | ||
@@ -97,0 +128,0 @@ exports.transformFilterContext = transformFilterContext; |
{ | ||
"name": "@gooddata/js-utils", | ||
"version": "3.10.7", | ||
"version": "3.10.8", | ||
"description": "Various utils shared on GoodData frontend", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -5,2 +5,9 @@ // (C) 2020 GoodData Corporation | ||
export const EXTERNAL_DATE_FILTER_FORMAT = 'YYYY-MM-DD'; | ||
export interface IExternalFiltersObject { | ||
attributeFilters: ITransformedAttributeFilterItem[]; | ||
dateFilters: ITransformedDateFilterItem[]; | ||
} | ||
export interface ITransformedDateFilterItem { | ||
@@ -29,13 +36,38 @@ granularity?: string; | ||
const { | ||
absoluteDateFilter: { from, to } | ||
absoluteDateFilter: { from, to, dataSet } | ||
} = filterItem; | ||
return isString(from) && isString(to) && DATE_FORMAT_REGEX.test(from) && DATE_FORMAT_REGEX.test(to); | ||
const { uri, identifier } = getObjectUriIdentifier(dataSet); | ||
return (isString(uri) || isString(identifier)) | ||
&& isString(from) && isString(to) | ||
&& DATE_FORMAT_REGEX.test(from) && DATE_FORMAT_REGEX.test(to); | ||
} else { | ||
const { | ||
relativeDateFilter: { from, to } | ||
relativeDateFilter: { from, to, dataSet } | ||
} = filterItem; | ||
return isNumber(from) && isNumber(to) && from <= to; | ||
const { uri, identifier } = getObjectUriIdentifier(dataSet); | ||
return (isString(uri) || isString(identifier)) && isNumber(from) && isNumber(to); | ||
} | ||
} | ||
function isValidAttributeFilterFormat(filterItem: EmbeddedGdc.AttributeFilterItem): boolean { | ||
if (!EmbeddedGdc.isAttributeFilter(filterItem)) { | ||
return false; | ||
} | ||
if (EmbeddedGdc.isPositiveAttributeFilter(filterItem)) { | ||
const { | ||
positiveAttributeFilter: { displayForm, in: attributeElements } | ||
} = filterItem; | ||
const { uri, identifier } = getObjectUriIdentifier(displayForm); | ||
return (isString(uri) || isString(identifier)) && attributeElements.length !== 0; | ||
} else { | ||
const { | ||
negativeAttributeFilter: { displayForm } | ||
} = filterItem; | ||
const { uri, identifier } = getObjectUriIdentifier(displayForm); | ||
// attributeElements could be empty in case of setting All Value | ||
return isString(uri) || isString(identifier); | ||
} | ||
} | ||
export function isValidFilterItemFormat(filterItem: any): boolean { | ||
@@ -45,3 +77,3 @@ if (EmbeddedGdc.isDateFilter(filterItem)) { | ||
} else { | ||
return EmbeddedGdc.isAttributeFilter(filterItem); | ||
return isValidAttributeFilterFormat(filterItem); | ||
} | ||
@@ -61,3 +93,3 @@ } | ||
function transformDateFilterItem(dateFilterItem: EmbeddedGdc.DateFilterItem): ITransformedFilterItem { | ||
function transformDateFilterItem(dateFilterItem: EmbeddedGdc.DateFilterItem): ITransformedDateFilterItem { | ||
if (EmbeddedGdc.isAbsoluteDateFilter(dateFilterItem)) { | ||
@@ -91,3 +123,3 @@ const { | ||
attributeFilterItem: EmbeddedGdc.AttributeFilterItem | ||
): ITransformedFilterItem { | ||
): ITransformedAttributeFilterItem { | ||
if (EmbeddedGdc.isPositiveAttributeFilter(attributeFilterItem)) { | ||
@@ -118,14 +150,26 @@ const { | ||
export function transformFilterContext(filters: EmbeddedGdc.FilterItem[]): ITransformedFilterItem[] { | ||
export function transformFilterContext(filters: EmbeddedGdc.FilterItem[]): IExternalFiltersObject { | ||
const defaultFiltersObject = { | ||
attributeFilters: [], | ||
dateFilters: [] | ||
}; | ||
if (isEmpty(filters)) { | ||
return []; | ||
return defaultFiltersObject; | ||
} | ||
return filters.map( | ||
(filterItem: EmbeddedGdc.FilterItem): ITransformedFilterItem => { | ||
return EmbeddedGdc.isDateFilter(filterItem) | ||
? transformDateFilterItem(filterItem) | ||
: transformAttributeFilterItem(filterItem); | ||
} | ||
); | ||
return filters.reduce( | ||
( | ||
externalFilters: IExternalFiltersObject, | ||
filterItem: EmbeddedGdc.FilterItem | ||
): IExternalFiltersObject => { | ||
if (EmbeddedGdc.isDateFilter(filterItem)) { | ||
const dateFilter = transformDateFilterItem(filterItem); | ||
externalFilters.dateFilters.push(dateFilter); | ||
} else { | ||
const attributeFilter = transformAttributeFilterItem(filterItem); | ||
externalFilters.attributeFilters.push(attributeFilter); | ||
} | ||
return externalFilters; | ||
}, defaultFiltersObject); | ||
} | ||
@@ -132,0 +176,0 @@ |
@@ -49,2 +49,16 @@ // (C) 2020 GoodData Corporation | ||
}; | ||
const positiveAttributeFilterWithoutDisplayForm = { | ||
positiveAttributeFilter: { | ||
in: ['uri1'], | ||
displayForm: {} | ||
} | ||
}; | ||
const positiveAttributeFilterWithoutValue = { | ||
positiveAttributeFilter: { | ||
in: [], | ||
displayForm: { | ||
uri: 'dfUri' | ||
} | ||
} | ||
}; | ||
@@ -92,2 +106,10 @@ it('should return false if have any item is not the filter item', () => { | ||
it('should return false when attribute filter item is missing uri and identifier', () => { | ||
expect(isValidFiltersFormat([positiveAttributeFilterWithoutDisplayForm])).toBe(false); | ||
}); | ||
it('should return false when positive attribute filter item without filter values', () => { | ||
expect(isValidFiltersFormat([positiveAttributeFilterWithoutValue])).toBe(false); | ||
}); | ||
it('should transform filter context', () => { | ||
@@ -100,29 +122,33 @@ const filters: EmbeddedGdc.FilterItem[] = [ | ||
]; | ||
expect(transformFilterContext(filters)).toEqual([ | ||
{ | ||
datasetIdentifier: undefined, | ||
datasetUri: 'uri', | ||
from: '2020-01-01', | ||
to: '2020-02-01' | ||
}, | ||
{ | ||
datasetIdentifier: undefined, | ||
datasetUri: 'uri', | ||
from: -1, | ||
granularity: 'gdc.time.month', | ||
to: 1 | ||
}, | ||
{ | ||
attributeElements: ['uri1'], | ||
dfIdentifier: undefined, | ||
dfUri: 'dfuri', | ||
negativeSelection: true | ||
}, | ||
{ | ||
attributeElements: ['uri1'], | ||
dfIdentifier: undefined, | ||
dfUri: 'dfuri', | ||
negativeSelection: false | ||
} | ||
]); | ||
expect(transformFilterContext(filters)).toEqual({ | ||
attributeFilters: [ | ||
{ | ||
attributeElements: ['uri1'], | ||
dfIdentifier: undefined, | ||
dfUri: 'dfuri', | ||
negativeSelection: true | ||
}, | ||
{ | ||
attributeElements: ['uri1'], | ||
dfIdentifier: undefined, | ||
dfUri: 'dfuri', | ||
negativeSelection: false | ||
} | ||
], | ||
dateFilters: [ | ||
{ | ||
datasetIdentifier: undefined, | ||
datasetUri: 'uri', | ||
from: '2020-01-01', | ||
to: '2020-02-01' | ||
}, | ||
{ | ||
datasetIdentifier: undefined, | ||
datasetUri: 'uri', | ||
from: -1, | ||
granularity: 'gdc.time.month', | ||
to: 1 | ||
} | ||
] | ||
}); | ||
}); | ||
@@ -129,0 +155,0 @@ |
Sorry, the diff of this file is not supported yet
97834
1985