Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
class="form-control"
>
</select>
</div>
</div>
<div class="form-group">
<label for="start_row">Start at row</label>
<input
Expand Down Expand Up @@ -176,10 +176,16 @@
</div>
<div
class="btn btn-outline-secondary"
ng-click="toggleColumnFormat()"
ng-click="toggleNormalizeDate()"
>
<i class="fa fa-exchange"></i> Toggle column format
<i class="fa fa-exchange"></i> Toggle normalized date
</div>
<div
class="btn btn-outline-secondary"
ng-click="toggleColumnFormat()"
>
<i class="fa fa-exchange"></i> Toggle column format
</div>
</div>
<h4>YNAB Data <small>(first 10 rows)</small></h4>
<div class="clearfix"></div>
Expand Down
24 changes: 18 additions & 6 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ var defaultProfile = {
}, {}),
chosenEncoding: "UTF-8",
chosenDelimiter: "auto",
startAtRow: 1
startAtRow: 1,
normalizeDate: false
};
var defaultProfiles = {
"default profile": defaultProfile
Expand Down Expand Up @@ -148,8 +149,9 @@ angular.element(document).ready(function () {
$scope.profile = $scope.profiles[$scope.profileName];
$scope.ynab_cols = $scope.profile.columnFormat;
$scope.data = {};
$scope.ynab_map = $scope.profile.chosenColumns
$scope.ynab_map = $scope.profile.chosenColumns;
$scope.inverted_outflow = false;
$scope.normalize_date = $scope.profile.normalizeDate;
$scope.file = {
encodings: encodings,
delimiters: delimiters,
Expand Down Expand Up @@ -183,6 +185,11 @@ angular.element(document).ready(function () {
$scope.nonDefaultProfilesExist = function() {
return Object.keys($scope.profiles).length > 1;
};
$scope.toggleNormalizeDate = function () {
$scope.normalize_date = !$scope.normalize_date;
$scope.profile.normalizeDate = $scope.normalize_date;
localStorage.setItem('profiles', JSON.stringify($scope.profiles));
};
$scope.toggleColumnFormat = function () {
if ($scope.ynab_cols == new_ynab_cols) {
$scope.ynab_cols = old_ynab_cols;
Expand All @@ -199,25 +206,30 @@ angular.element(document).ready(function () {
} else {
$scope.data_object.parseCsv(newValue, $scope.file.chosenEncoding, $scope.file.startAtRow, $scope.file.chosenDelimiter);
}
$scope.preview = $scope.data_object.converted_json(10, $scope.ynab_cols, $scope.ynab_map, $scope.inverted_outflow);
$scope.preview = $scope.data_object.converted_json(10, $scope.ynab_cols, $scope.ynab_map, $scope.inverted_outflow, $scope.normalize_date);
}
});
$scope.$watch("inverted_outflow", function (newValue, oldValue) {
if (newValue != oldValue) {
$scope.preview = $scope.data_object.converted_json(10, $scope.ynab_cols, $scope.ynab_map, $scope.inverted_outflow);
$scope.preview = $scope.data_object.converted_json(10, $scope.ynab_cols, $scope.ynab_map, $scope.inverted_outflow, $scope.normalize_date);
}
});
$scope.$watch("normalize_date", function (newValue, oldValue) {
if (newValue != oldValue) {
$scope.preview = $scope.data_object.converted_json(10, $scope.ynab_cols, $scope.ynab_map, $scope.inverted_outflow, $scope.normalize_date);
}
});
$scope.$watch(
"ynab_map",
function (newValue, oldValue) {
$scope.profile.chosenColumns = newValue;
localStorage.setItem('profiles', JSON.stringify($scope.profiles));
$scope.preview = $scope.data_object.converted_json(10, $scope.ynab_cols, newValue, $scope.inverted_outflow);
$scope.preview = $scope.data_object.converted_json(10, $scope.ynab_cols, newValue, $scope.inverted_outflow, $scope.normalize_date);
},
true
);
$scope.csvString = function () {
return $scope.data_object.converted_csv(null, $scope.ynab_cols, $scope.ynab_map, $scope.inverted_outflow);
return $scope.data_object.converted_csv(null, $scope.ynab_cols, $scope.ynab_map, $scope.inverted_outflow, $scope.normalize_date);
};
$scope.reloadApp = function () {
$scope.setInitialScopeState();
Expand Down
23 changes: 20 additions & 3 deletions src/data_object.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ window.DataObject = class DataObject {
// lookup: hash definition of YNAB column names to selected base column names. Lets us
// convert the uploaded CSV file into the columns that YNAB expects.
// inverted_outflow: if true, positive values represent outflow while negative values represent inflow
converted_json(limit, ynab_cols, lookup, inverted_outflow = false) {
// normalize_date: false leaves date as is, true outputs it as YYYY-MM-DD
converted_json(limit, ynab_cols, lookup, inverted_outflow = false, normalize_date = false) {
var value;
if (this.base_json === null) {
return null;
Expand Down Expand Up @@ -101,6 +102,22 @@ window.DataObject = class DataObject {
tmp_row[col] = cell;
}
break;
case "Date":
if (normalize_date) {
var d = new Date(cell),
Copy link

@denkristoffer denkristoffer Dec 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some date format detection and shaping is necessary before this will work, cell can be in a lot of formats that wont work with new Date(). I believe this is why date formatting has not been added so far: #10

month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();

if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
tmp_row[col] = [year, month, day].join('-');
} else {
tmp_row[col] = cell;
}
break;
default:
tmp_row[col] = cell;
}
Expand All @@ -113,14 +130,14 @@ window.DataObject = class DataObject {
return value;
}

converted_csv(limit, ynab_cols, lookup, inverted_outflow) {
converted_csv(limit, ynab_cols, lookup, inverted_outflow, normalize_date) {
var string;
if (this.base_json === null) {
return nil;
}
// Papa.unparse string
string = '"' + ynab_cols.join('","') + '"\n';
this.converted_json(limit, ynab_cols, lookup, inverted_outflow).forEach(function (row) {
this.converted_json(limit, ynab_cols, lookup, inverted_outflow, normalize_date).forEach(function (row) {
var row_values;
row_values = [];
ynab_cols.forEach(function (col) {
Expand Down