Skip to content

Commit

Permalink
Implement a rounding function to process numeric & duration type
Browse files Browse the repository at this point in the history
  • Loading branch information
johardi committed Jul 30, 2017
1 parent 82348be commit 0f33ca0
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions app/components/search/cse-data.factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,29 +135,33 @@ function(schemaorgVocab) {
}

function refineNumericData(value, unit) {
var number = -1;
if (unit != null) {
try {
return Qty(value).to(unit).scalar;
number = Qty(value).to(unit).scalar;
} catch (err) {
return autoFixNumericData(value);
number = autoFixNumericData(value);
}
} else {
return autoFixNumericData(value);
number = autoFixNumericData(value);
}
return round(number, 1);
}

function refineDurationData(value, unit) {
var number = -1;
var duration = moment.duration(value);
if (duration._milliseconds != 0) {
return duration.as(unit);
number = duration.as(unit);
} else { // invalid ISO8601 value
if (value.charAt(0) !== "P") {
var newValue = "P" + value;
return refineDurationData(newValue, unit);
} else { // give up
return autoFixDurationData(value);
number = autoFixDurationData(value);
}
}
return round(number, 0);
}

function refineUrlData(url) {
Expand Down Expand Up @@ -257,6 +261,21 @@ function(schemaorgVocab) {
return +value;
}

function round(number, digits) {
if (!isInt(number)) {
if (digits == 0) {
number = number.toFixed();
} else {
number = number.toFixed(digits);
}
}
return +number;
}

function isInt(number) {
return parseInt(number) === number;
}

function findIndex(arr, key, value) {
for(var i = 0; i < arr.length; i++) {
if (arr[i][key] === value) return i;
Expand Down

0 comments on commit 0f33ca0

Please sign in to comment.