@codecovevienna/gittt-cli
Version:
Tracking time with CLI into a git repository
157 lines (156 loc) • 6.21 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RecordHelper = void 0;
const uuid_1 = require("uuid");
const inquirer_1 = __importDefault(require("inquirer"));
const moment_1 = __importDefault(require("moment"));
class RecordHelper {
static filterRecordsByYear(records) {
return __awaiter(this, void 0, void 0, function* () {
const allYears = [];
for (const rc of records) {
const currentYear = (0, moment_1.default)(rc.end).format("YYYY");
if (allYears.indexOf(currentYear) === -1) {
allYears.push(currentYear);
}
}
// Check if records spanning over more than one year
if (allYears.length > 1) {
const choiceYear = yield inquirer_1.default.prompt([
{
choices: allYears,
message: "List of years",
name: "year",
type: "list",
},
]);
return records.filter((rc) => {
const currentYear = (0, moment_1.default)(rc.end).format("YYYY");
return currentYear === choiceYear.year;
});
}
else {
return records;
}
});
}
static filterRecordsByMonth(records) {
return __awaiter(this, void 0, void 0, function* () {
// Check for month
const allMonths = [];
for (const rc of records) {
const currentMonth = (0, moment_1.default)(rc.end).format("MMMM");
if (allMonths.indexOf(currentMonth) === -1) {
allMonths.push(currentMonth);
}
}
// Check if records spanning over more than one month
if (allMonths.length > 1) {
const choiceMonth = yield inquirer_1.default.prompt([
{
choices: allMonths,
message: "List of Month",
name: "month",
type: "list",
},
]);
return records.filter((rc) => {
const currentMonth = (0, moment_1.default)(rc.end).format("MMMM");
return currentMonth === choiceMonth.month;
});
}
else {
return records;
}
});
}
static filterRecordsByDay(records) {
return __awaiter(this, void 0, void 0, function* () {
// Check for days
const allDays = [];
for (const rc of records) {
const currentDay = (0, moment_1.default)(rc.end).format("DD");
if (allDays.indexOf(currentDay) === -1) {
allDays.push(currentDay);
}
}
// Check if records spanning over more than one day
if (allDays.length > 1) {
const choiceDay = yield inquirer_1.default.prompt([
{
choices: allDays,
message: "List of Days",
name: "day",
type: "list",
},
]);
return records.filter((rc) => {
const currentDay = (0, moment_1.default)(rc.end).format("DD");
return currentDay === choiceDay.day;
});
}
else {
return records;
}
});
}
}
exports.RecordHelper = RecordHelper;
/*
* returns {boolean} true if provided record is identical to any record in records
*/
// FIXME could be covered by lodash, e.g. _.uniqWith(records, _.isEqual)
RecordHelper.isRecordUnique = (record, records) => {
// check if amount, end, message and type is found in records
return records.find((existingRecord) => {
return existingRecord.amount === record.amount &&
existingRecord.end === record.end &&
existingRecord.message === record.message &&
existingRecord.type === record.type;
}) === undefined;
};
/*
* returns {boolean} true if provided record is overlapping any record in records
*/
RecordHelper.isRecordOverlapping = (record, records) => {
// check if any overlapping records are present
return records.find((existingRecord) => {
const startExisting = existingRecord.end - existingRecord.amount;
const startAdd = record.end - record.amount;
const endExisting = existingRecord.end;
const endAdd = record.end;
if ((startAdd >= startExisting && startAdd < endExisting) ||
(endAdd > startAdd && endAdd <= endExisting) ||
(startAdd <= startExisting && endAdd >= endExisting)) {
return true;
}
return false;
}) === undefined;
};
/*
* Sets guid and current timestamps to given record object
*/
RecordHelper.setRecordDefaults = (record) => {
// Add unique identifier to each record
if (!record.guid) {
record.guid = (0, uuid_1.v1)();
}
if (!record.created) {
const now = Date.now();
record.created = now;
record.updated = now;
}
return record;
};