Skip to content

Commit

Permalink
fix: Make date measure parsing optional
Browse files Browse the repository at this point in the history
Fixes #602
  • Loading branch information
paveltiunov committed Apr 20, 2020
1 parent cf44753 commit d199cd5
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 22 deletions.
17 changes: 12 additions & 5 deletions packages/cubejs-client-core/dist/cubejs-client-core.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,20 @@ var TIME_SERIES = {
}
};
var DateRegex = /^\d\d\d\d-\d\d-\d\d$/;
var ISO8601_REGEX = /^([+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24:?00)([.,]\d+(?!:))?)?(\17[0-5]\d([.,]\d+)?)?([zZ]|([+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/;
var LocalDateRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z?$/;
/**
* Provides a convenient interface for data manipulation.
*/

var ResultSet =
/*#__PURE__*/
function () {
function ResultSet(loadResponse) {
function ResultSet(loadResponse, options) {
_classCallCheck(this, ResultSet);

options = options || {};
this.loadResponse = loadResponse;
this.parseDateMeasures = options.parseDateMeasures;
}
/**
* Returns an array of series with key, title and series data.
Expand Down Expand Up @@ -424,7 +426,7 @@ function () {
var _this3 = this;

var validate = function validate(value) {
if (ISO8601_REGEX.test(value)) {
if (_this3.parseDateMeasures && LocalDateRegex.test(value)) {
return new Date(value);
} else if (!Number.isNaN(Number.parseFloat(value))) {
return Number.parseFloat(value);
Expand Down Expand Up @@ -994,6 +996,7 @@ function () {
headers: this.headers
});
this.pollInterval = options.pollInterval || 5;
this.parseDateMeasures = options.parseDateMeasures;
}

_createClass(CubejsApi, [{
Expand Down Expand Up @@ -1433,7 +1436,9 @@ function () {
query: query
});
}, function (body) {
return new ResultSet(body);
return new ResultSet(body, {
parseDateMeasures: _this2.parseDateMeasures
});
}, options, callback);
}
/**
Expand Down Expand Up @@ -1485,7 +1490,9 @@ function () {
query: query
});
}, function (body) {
return new ResultSet(body);
return new ResultSet(body, {
parseDateMeasures: _this5.parseDateMeasures
});
}, _objectSpread2({}, options, {
subscribe: true
}), callback);
Expand Down
17 changes: 12 additions & 5 deletions packages/cubejs-client-core/dist/cubejs-client-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,20 @@ var TIME_SERIES = {
}
};
var DateRegex = /^\d\d\d\d-\d\d-\d\d$/;
var ISO8601_REGEX = /^([+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24:?00)([.,]\d+(?!:))?)?(\17[0-5]\d([.,]\d+)?)?([zZ]|([+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/;
var LocalDateRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z?$/;
/**
* Provides a convenient interface for data manipulation.
*/

var ResultSet =
/*#__PURE__*/
function () {
function ResultSet(loadResponse) {
function ResultSet(loadResponse, options) {
_classCallCheck(this, ResultSet);

options = options || {};
this.loadResponse = loadResponse;
this.parseDateMeasures = options.parseDateMeasures;
}
/**
* Returns an array of series with key, title and series data.
Expand Down Expand Up @@ -430,7 +432,7 @@ function () {
var _this3 = this;

var validate = function validate(value) {
if (ISO8601_REGEX.test(value)) {
if (_this3.parseDateMeasures && LocalDateRegex.test(value)) {
return new Date(value);
} else if (!Number.isNaN(Number.parseFloat(value))) {
return Number.parseFloat(value);
Expand Down Expand Up @@ -1000,6 +1002,7 @@ function () {
headers: this.headers
});
this.pollInterval = options.pollInterval || 5;
this.parseDateMeasures = options.parseDateMeasures;
}

_createClass(CubejsApi, [{
Expand Down Expand Up @@ -1439,7 +1442,9 @@ function () {
query: query
});
}, function (body) {
return new ResultSet(body);
return new ResultSet(body, {
parseDateMeasures: _this2.parseDateMeasures
});
}, options, callback);
}
/**
Expand Down Expand Up @@ -1491,7 +1496,9 @@ function () {
query: query
});
}, function (body) {
return new ResultSet(body);
return new ResultSet(body, {
parseDateMeasures: _this5.parseDateMeasures
});
}, _objectSpread2({}, options, {
subscribe: true
}), callback);
Expand Down
17 changes: 12 additions & 5 deletions packages/cubejs-client-core/dist/cubejs-client-core.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -13599,18 +13599,20 @@
}
};
var DateRegex = /^\d\d\d\d-\d\d-\d\d$/;
var ISO8601_REGEX = /^([+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24:?00)([.,]\d+(?!:))?)?(\17[0-5]\d([.,]\d+)?)?([zZ]|([+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/;
var LocalDateRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z?$/;
/**
* Provides a convenient interface for data manipulation.
*/

var ResultSet =
/*#__PURE__*/
function () {
function ResultSet(loadResponse) {
function ResultSet(loadResponse, options) {
_classCallCheck(this, ResultSet);

options = options || {};
this.loadResponse = loadResponse;
this.parseDateMeasures = options.parseDateMeasures;
}
/**
* Returns an array of series with key, title and series data.
Expand Down Expand Up @@ -13947,7 +13949,7 @@
var _this3 = this;

var validate = function validate(value) {
if (ISO8601_REGEX.test(value)) {
if (_this3.parseDateMeasures && LocalDateRegex.test(value)) {
return new Date(value);
} else if (!Number.isNaN(Number.parseFloat(value))) {
return Number.parseFloat(value);
Expand Down Expand Up @@ -15448,6 +15450,7 @@
headers: this.headers
});
this.pollInterval = options.pollInterval || 5;
this.parseDateMeasures = options.parseDateMeasures;
}

_createClass(CubejsApi, [{
Expand Down Expand Up @@ -15887,7 +15890,9 @@
query: query
});
}, function (body) {
return new ResultSet(body);
return new ResultSet(body, {
parseDateMeasures: _this2.parseDateMeasures
});
}, options, callback);
}
/**
Expand Down Expand Up @@ -15939,7 +15944,9 @@
query: query
});
}, function (body) {
return new ResultSet(body);
return new ResultSet(body, {
parseDateMeasures: _this5.parseDateMeasures
});
}, _objectSpread({}, options, {
subscribe: true
}), callback);
Expand Down
8 changes: 5 additions & 3 deletions packages/cubejs-client-core/src/ResultSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ const TIME_SERIES = {
};

const DateRegex = /^\d\d\d\d-\d\d-\d\d$/;
const ISO8601_REGEX = /^([+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24:?00)([.,]\d+(?!:))?)?(\17[0-5]\d([.,]\d+)?)?([zZ]|([+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/;
const LocalDateRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z?$/;

/**
* Provides a convenient interface for data manipulation.
*/
class ResultSet {
constructor(loadResponse) {
constructor(loadResponse, options) {
options = options || {};
this.loadResponse = loadResponse;
this.parseDateMeasures = options.parseDateMeasures;
}

/**
Expand Down Expand Up @@ -289,7 +291,7 @@ class ResultSet {
*/
chartPivot(pivotConfig) {
const validate = (value) => {
if (ISO8601_REGEX.test(value)) {
if (this.parseDateMeasures && LocalDateRegex.test(value)) {
return new Date(value);
} else if (!Number.isNaN(Number.parseFloat(value))) {
return Number.parseFloat(value);
Expand Down
4 changes: 2 additions & 2 deletions packages/cubejs-client-core/src/ResultSet.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ describe('ResultSet', () => {
},
data: [{
'Foo.name': 'Name 1',
'Foo.count': 10,
'Foo.count': '10',
}],
lastRefreshTime: '2020-03-18T13:41:04.436Z',
usedPreAggregations: {},
Expand Down Expand Up @@ -292,7 +292,7 @@ describe('ResultSet', () => {
segments: {},
timeDimensions: {},
},
});
}, { parseDateMeasures: true });

expect(resultSet.chartPivot()).toEqual([{
x: 'Name 1',
Expand Down
5 changes: 3 additions & 2 deletions packages/cubejs-client-core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class CubejsApi {
headers: this.headers
});
this.pollInterval = options.pollInterval || 5;
this.parseDateMeasures = options.parseDateMeasures;
}

request(method, params) {
Expand Down Expand Up @@ -204,7 +205,7 @@ class CubejsApi {
load(query, options, callback) {
return this.loadMethod(
() => this.request(`load`, { query }),
(body) => new ResultSet(body),
(body) => new ResultSet(body, { parseDateMeasures: this.parseDateMeasures }),
options,
callback
);
Expand Down Expand Up @@ -244,7 +245,7 @@ class CubejsApi {
subscribe(query, options, callback) {
return this.loadMethod(
() => this.request(`subscribe`, { query }),
(body) => new ResultSet(body),
(body) => new ResultSet(body, { parseDateMeasures: this.parseDateMeasures }),
{ ...options, subscribe: true },
callback
);
Expand Down

0 comments on commit d199cd5

Please sign in to comment.