Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a button to normalize the date. #37

Open
wants to merge 2 commits into
base: gh-pages
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 14 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,16 @@ window.DataObject = class DataObject {
tmp_row[col] = cell;
}
break;
case "Date":
if (normalize_date) {
// Venmo outputs e.g. 2020-11-30T17:07:16
// As of late 2020, if hyphens are the separators, YNAB takes it as YYYY-MM-DD
if (cell.indexOf("T") >= 0)
tmp_row[col] = cell.substr(0, cell.indexOf("T"))
} else {
tmp_row[col] = cell;
}
break;
default:
tmp_row[col] = cell;
}
Expand All @@ -113,14 +124,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