Skip to content

Commit

Permalink
remove most augment functions into augment-vir
Browse files Browse the repository at this point in the history
Use augment-vir package in place of having all these one-off augments.
  • Loading branch information
electrovir committed Oct 25, 2021
1 parent eb53a57 commit 74b1adc
Show file tree
Hide file tree
Showing 31 changed files with 107 additions and 852 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,5 +260,3 @@ If you find that your statement PDF is coming from a bank or credit card that th
3. Add new `runTest` calls for the tests you want to add.

See other test files for examples, such as [`array.test.ts`](https://github.com/electrovir/statement-parser/tree/master/src/augments/array.test.ts).

> v2.0.0
19 changes: 9 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "statement-parser",
"version": "2.0.0",
"version": "2.0.1",
"description": "Parse bank and credit card statements.",
"keywords": [
"pdf",
Expand Down Expand Up @@ -36,18 +36,19 @@
"main": "dist/src/index.js",
"typings": "dist/src/index.d.ts",
"scripts": {
"format": "virmator format write",
"format": "virmator format",
"prepublishOnly": "npm run test:full",
"sanitize": "virmator compile && node dist/sanitizer/sanitize-for-test-file-cli.js",
"sanitize:all": "./bulk-sanitize.sh files/downloads",
"sanitize:no-compile": "node dist/sanitizer/sanitize-for-test-file-cli.js",
"spellcheck": "virmator spellcheck",
"update-docs": "virmator code-in-markdown README.md",
"test": "virmator test",
"test:file": "./test-specific-file.sh",
"test:full": "npm run test && npm run spellcheck && npm run format check"
"test:full": "npm run test && npm run spellcheck && npm run format check && npm run update-docs -- --check",
"update-docs": "virmator code-in-markdown README.md"
},
"dependencies": {
"augment-vir": "^1.3.0",
"fs-extra": "^10.0.0",
"fsm-vir": "^1.0.1",
"pdf-text-reader": "^3.0.0",
Expand Down
43 changes: 0 additions & 43 deletions src/augments/array.test.ts

This file was deleted.

9 changes: 0 additions & 9 deletions src/augments/array.ts

This file was deleted.

103 changes: 0 additions & 103 deletions src/augments/date.test.ts

This file was deleted.

101 changes: 22 additions & 79 deletions src/augments/date.ts
Original file line number Diff line number Diff line change
@@ -1,79 +1,18 @@
import {createDateFromUtcIsoFormat} from 'augment-vir';
import {isSanitizerMode} from '../global';
const longMonthNames = [
'january',
'february',
'march',
'april',
'may',
'june',
'july',
'august',
'september',
'october',
'november',
'december',
];

const shortMonthNames = longMonthNames.map((longMonthName) => longMonthName.slice(0, 3));

export class InvalidDateError extends Error {
public override readonly name = 'InvalidDateError';
}

export function dateFromSlashFormat(input: string, yearPrefix: number | string = '') {
const [month, day, rawYearEnding = ''] = input.split('/');

if (!month || !day) {
throw new Error(`Unable to extract month or day from "${input}"`);
}

const yearEnding =
rawYearEnding.length < 4 ? `${yearPrefix}${rawYearEnding.padStart(2, '0')}` : rawYearEnding;

const returnDate = createUtcDate(
`${yearEnding.padStart(4, '0')}-${month.padStart(2, '0')}-${day.padStart(2, '0')}`,
);
return returnDate;
}

export function dateFromNamedCommaFormat(input: string) {
const [monthName, dayNumber, fullYear] = input.replace(',', '').split(' ');
if (!monthName || !dayNumber || !fullYear) {
throw new InvalidDateError(`Invalid ${dateFromNamedCommaFormat.name} input: ${input}`);
}

const longMonthIndex = longMonthNames.indexOf(monthName.toLowerCase());
const shortMonthIndex = shortMonthNames.indexOf(monthName.toLowerCase());

let monthIndex = longMonthIndex === -1 ? shortMonthIndex : longMonthIndex;

if (monthIndex === -1) {
if (isSanitizerMode()) {
monthIndex = 4;
} else {
throw new InvalidDateError(`Month name ${monthName} was not found.`);
}
}

const returnDate = createUtcDate(
`${fullYear.padStart(4, '0')}-${String(monthIndex + 1).padStart(
2,
'0',
)}-${dayNumber.padStart(2, '0')}`,
);

return returnDate;
}

export function createUtcDate(isoFormatString: string) {
const utcDate = new Date(isoFormatString + 'T00:00:00.000Z');

if (isNaN(Number(utcDate))) {
throw new InvalidDateError(`Invalid utc date formed from input "${isoFormatString}"`);
}
return utcDate;
}

/**
* Creates a date object that's between the two dates given. If a valid date cannot be created with
* the given inputs, a date is created that is at least earlier than the given endDate.
*
* @param startDate Optional. The earlier (date wise) bounds for creating the new Date object. If
* this is not provided, a date is created that is as close to, but earlier than, the given endDate.
* @param endDate Required. The later bounds for creating the new Date object.
* @param monthNumber Month number for the new Date object. This is 1 indexed. So a `1` here
* corresponds to January.
* @param dayNumber Number for the month for the new Date object. This is 1 indexed; the first day
* of the mont is `1`.
*/
export function dateWithinRange(
startDate: Date | undefined,
endDate: Date,
Expand All @@ -90,18 +29,22 @@ export function dateWithinRange(
const day = dayNumber < 10 ? `0${dayNumber}` : String(dayNumber);

if (!startDate || startDate.getUTCFullYear() === endDate.getUTCFullYear()) {
const newDate = createUtcDate(`${endDate.getUTCFullYear()}-${month}-${day}`);
const newDate = createDateFromUtcIsoFormat(`${endDate.getUTCFullYear()}-${month}-${day}`);
if (newDate <= endDate) {
return newDate;
} else {
return createUtcDate(`${endDate.getUTCFullYear() - 1}-${month}-${day}`);
return createDateFromUtcIsoFormat(`${endDate.getUTCFullYear() - 1}-${month}-${day}`);
}
} else if (startDate) {
const dateFromStartYear = createUtcDate(`${startDate.getUTCFullYear()}-${month}-${day}`);
const dateFromStartYearPlus = createUtcDate(
const dateFromStartYear = createDateFromUtcIsoFormat(
`${startDate.getUTCFullYear()}-${month}-${day}`,
);
const dateFromStartYearPlus = createDateFromUtcIsoFormat(
`${startDate.getUTCFullYear() + 1}-${month}-${day}`,
);
const dateFromEndYear = createUtcDate(`${endDate.getUTCFullYear()}-${month}-${day}`);
const dateFromEndYear = createDateFromUtcIsoFormat(
`${endDate.getUTCFullYear()}-${month}-${day}`,
);
if (dateFromStartYear <= endDate && startDate <= dateFromStartYear) {
return dateFromStartYear;
} else if (dateFromEndYear <= endDate && startDate <= dateFromEndYear) {
Expand Down
13 changes: 0 additions & 13 deletions src/augments/object.ts

This file was deleted.

Loading

0 comments on commit 74b1adc

Please sign in to comment.