Skip to content

Commit

Permalink
Improve convenience of the newDimension factory
Browse files Browse the repository at this point in the history
-  Now can also send IAttribute, code will obtain localId from it
  • Loading branch information
lupko committed Jun 23, 2020
1 parent 2a6f68a commit 881be7c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
12 changes: 8 additions & 4 deletions libs/sdk-model/src/execution/base/dimension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import invariant from "ts-invariant";
import { Identifier } from "../../objRef/index";
import { isTotal, ITotal } from "./totals";
import isEmpty = require("lodash/isEmpty");
import { attributeLocalId, IAttribute, isAttribute } from "../attribute";

/**
* Dimensions specify how to organize the results of an execution in a data view. Imagine an attribute in columns vs. rows.
Expand Down Expand Up @@ -87,11 +88,12 @@ export function dimensionSetTotals(dim: IDimension, totals: ITotal[] = []): IDim
}

/**
* Defines union of items that can be placed into a dimension.
* Defines union of items that can be placed into a dimension. Identifier can be attribute localId or the
* special {@link MeasureGroupIdentifier}. Attribute `localId` can be also specified by value of IAttribute.
*
* @public
*/
export type DimensionItem = Identifier | ITotal;
export type DimensionItem = Identifier | IAttribute | ITotal;

/**
* Creates new two dimensional specification where each dimension will have the provided set of
Expand Down Expand Up @@ -128,16 +130,18 @@ type CategorizedIdAndTotal = {
/**
* Creates new single-dimensional specification where the dimension will have the provided set of identifiers.
*
* @param items - allows for mix of item identifiers and total definitions to have in the new dimension
* @param items - allows for mix of item identifiers, attributes and total definitions to have in the new dimension
* @param totals - additional totals to add to the dimension
* @returns single dimension
* @public
*/
export function newDimension(items: DimensionItem[] = [], totals: ITotal[] = []): IDimension {
const input: CategorizedIdAndTotal = items.reduce(
(acc: CategorizedIdAndTotal, value: string | ITotal) => {
(acc: CategorizedIdAndTotal, value: DimensionItem) => {
if (isTotal(value)) {
acc.totals.push(value);
} else if (isAttribute(value)) {
acc.ids.push(attributeLocalId(value));
} else {
acc.ids.push(value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,15 @@ Object {
}
`;

exports[`newDimension should create a valid dimension when using attribute objects and localIds 1`] = `
Object {
"itemIdentifiers": Array [
"a_label.account.id.name",
"explicitLocalId1",
],
}
`;

exports[`newDimension should create a valid dimension without totals when totals arg empty 1`] = `
Object {
"itemIdentifiers": Array [
Expand Down Expand Up @@ -219,6 +228,23 @@ Array [
]
`;

exports[`newTwoDimensional should create both dimensions with correct ids when mixing attribute and localId 1`] = `
Array [
Object {
"itemIdentifiers": Array [
"localId1",
"a_label.account.id.name",
],
},
Object {
"itemIdentifiers": Array [
"a_label.activity.id.subject",
"localId2",
],
},
]
`;

exports[`newTwoDimensional should create first dimension empty 1`] = `
Array [
Object {
Expand Down
11 changes: 11 additions & 0 deletions libs/sdk-model/src/execution/base/tests/dimension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
newTwoDimensional,
} from "../dimension";
import { ITotal } from "../totals";
import { Account, Activity } from "../../../../__mocks__/model";

const Total1: ITotal = {
type: "avg",
Expand All @@ -33,6 +34,11 @@ describe("newDimension", () => {
undefined,
],
["create a valid dimension without totals when totals arg empty", ["localId1", "localId2"], []],
[
"create a valid dimension when using attribute objects and localIds",
[Account.Name, "explicitLocalId1"],
[],
],
[
"create a valid dimension when input arg is mix of identifiers and totals",
["localId1", Total1],
Expand All @@ -53,6 +59,11 @@ describe("newTwoDimensional", () => {
["create first dimension empty", [], ["localId1"]],
["create second dimension empty", ["localId1"], []],
["create both dimensions with correct ids", ["localId1"], ["localId2"]],
[
"create both dimensions with correct ids when mixing attribute and localId",
["localId1", Account.Name],
[Activity.Subject, "localId2"],
],
["create first dimension with totals", ["localId1", Total1], ["localId2"]],
["create second dimension with totals", ["localId1"], ["localId2", Total1]],
["create both dimension with totals", ["localId1", Total1], ["localId2", Total2]],
Expand Down

0 comments on commit 881be7c

Please sign in to comment.