Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

pta-tools

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pta-tools - npm Package Compare versions

Comparing version 1.1.5 to 1.2.0

build/formatter/format-comment.d.ts

3

build/array.d.ts

@@ -0,1 +1,4 @@

/// <reference types="node" />
import { Readable } from 'stream';
export declare function unique<T>(list: T[]): T[];
export declare function collect<T>(readable: Readable): Promise<T[]>;

9

build/index.d.ts

@@ -1,4 +0,7 @@

import formatTransaction from './format-transaction';
import isTransaction from './is-transaction';
import Formatter from './formatter';
import formatComment from './formatter/format-comment';
import formatTransaction from './formatter/format-transaction';
import parse, { ParseResult } from './parse';
import Parser from './parser';
import { isTransaction } from './type-guards';
import { Comment, Journal, Posting, Transaction } from './types';

@@ -9,2 +12,2 @@ /**

declare type TransactionEntry = Posting;
export { Comment, formatTransaction, isTransaction, Journal, parse, ParseResult, Posting, Transaction, TransactionEntry, };
export { Comment, formatComment, Formatter, formatTransaction, isTransaction, Journal, parse, Parser, ParseResult, Posting, Transaction, TransactionEntry, };

@@ -5,4 +5,4 @@ 'use strict';

var stream = require('stream');
var split2 = require('split2');
var stream = require('stream');

@@ -13,2 +13,124 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
/* global Reflect, Promise */
var extendStatics = function(d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
function __extends(d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
}
var __assign = function() {
__assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
function __awaiter(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());
});
}
function __generator(thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
}
function __values(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
}
function __asyncValues(o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
}
function isTransaction(data) {
return (data === null || data === void 0 ? void 0 : data.date) instanceof Date && (data === null || data === void 0 ? void 0 : data.entries) instanceof Array;
}
function isComment$1(data) {
return data && typeof data.message === "string";
}
function isPosting(data) {
return data && typeof data.account === "string";
}
function isDirective$1(data) {
return data && data.symbol === "P";
}
function formatComment(comment) {
return "; ".concat(comment.message, "\n\n");
}
function formatDirective(d) {
return "".concat(d.symbol, " ").concat(d.data, "\n\n");
}
var INDENT = " ";
function formatDate(date) {

@@ -22,2 +144,5 @@ var d = new Date(date), month = "" + (d.getMonth() + 1), day = "" + d.getDate(), year = d.getFullYear();

}
function formatCommentInline(comment) {
return "; ".concat(comment.message);
}
var spaces = function (num, min) {

@@ -27,15 +152,34 @@ if (min === void 0) { min = 0; }

};
function formatPosting(posting) {
var str = "".concat(posting.account);
if (posting.amount) {
str = "".concat(str, " ").concat(spaces(30 - posting.account.length - posting.amount.toString().length, 2)).concat(posting.amount);
if (posting.commodity) {
str = "".concat(str, " ").concat(posting.commodity);
}
}
return str;
}
function formatHeader(trx) {
var str = formatDate(trx.date);
if (trx.confirmed) {
str = "".concat(str, " *");
}
str = "".concat(str, " ").concat(trx.description);
if (trx.comment) {
str = "".concat(str, " ; ").concat(trx.comment);
}
str = str + "\n";
return str;
}
function formatTransaction(trx) {
var dateStr = formatDate(trx.date);
var str = "".concat(dateStr, " ").concat(trx.description, "\n");
var str = formatHeader(trx);
for (var _i = 0, _a = trx.entries; _i < _a.length; _i++) {
var line = _a[_i];
str = "".concat(str, " ").concat(line.account);
if (line.amount) {
str = "".concat(str, " ").concat(spaces(30 - line.account.length - line.amount.length, 2)).concat(line.amount);
if (line.commodity) {
str = "".concat(str, " ").concat(line.commodity);
}
if (isPosting(line)) {
str = "".concat(str).concat(INDENT).concat(formatPosting(line), "\n");
}
str = "".concat(str, "\n");
else if (isComment$1(line)) {
str = "".concat(str).concat(INDENT).concat(formatCommentInline(line), "\n");
}
}

@@ -46,5 +190,71 @@ str = str + "\n";

function isTransaction(data) {
return (data === null || data === void 0 ? void 0 : data.date) instanceof Date && (data === null || data === void 0 ? void 0 : data.entries) instanceof Array;
var Formatter = /** @class */ (function (_super) {
__extends(Formatter, _super);
function Formatter() {
return _super.call(this, {
objectMode: true,
}) || this;
}
Formatter.prototype._transform = function (chunk, encoding, callback) {
if (isTransaction(chunk)) {
this.push(formatTransaction(chunk));
}
else if (isComment$1(chunk)) {
this.push(formatComment(chunk));
}
else if (isDirective$1(chunk)) {
this.push(formatDirective(chunk));
}
callback();
return;
};
return Formatter;
}(stream.Transform));
function unique(list) {
return Array.from(new Set(list));
}
function collect(readable) {
var readable_1, readable_1_1;
var e_1, _a;
return __awaiter(this, void 0, void 0, function () {
var result, chunk, e_1_1;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
result = [];
_b.label = 1;
case 1:
_b.trys.push([1, 6, 7, 12]);
readable_1 = __asyncValues(readable);
_b.label = 2;
case 2: return [4 /*yield*/, readable_1.next()];
case 3:
if (!(readable_1_1 = _b.sent(), !readable_1_1.done)) return [3 /*break*/, 5];
chunk = readable_1_1.value;
result.push(chunk);
_b.label = 4;
case 4: return [3 /*break*/, 2];
case 5: return [3 /*break*/, 12];
case 6:
e_1_1 = _b.sent();
e_1 = { error: e_1_1 };
return [3 /*break*/, 12];
case 7:
_b.trys.push([7, , 10, 11]);
if (!(readable_1_1 && !readable_1_1.done && (_a = readable_1.return))) return [3 /*break*/, 9];
return [4 /*yield*/, _a.call(readable_1)];
case 8:
_b.sent();
_b.label = 9;
case 9: return [3 /*break*/, 11];
case 10:
if (e_1) throw e_1.error;
return [7 /*endfinally*/];
case 11: return [7 /*endfinally*/];
case 12: return [2 /*return*/, result];
}
});
});
}

@@ -54,3 +264,5 @@ function getAccountsSorted(trxs) {

.filter(isTransaction)
.flatMap(function (trx) { return trx.entries.map(function (entry) { return entry.account; }); })
.flatMap(function (trx) {
return trx.entries.filter(isPosting).map(function (entry) { return entry.account; });
})
.reduce(function (acc, cur) {

@@ -67,6 +279,2 @@ acc[cur] = (acc[cur] || 0) + 1;

function unique(list) {
return Array.from(new Set(list));
}
function getCommodities(trxs) {

@@ -76,8 +284,10 @@ return unique(trxs.filter(isTransaction).flatMap(function (trx) {

var l = [];
if (entry.commodity) {
l.push(entry.commodity);
if (isPosting(entry)) {
if (entry.commodity) {
l.push(entry.commodity);
}
if (entry.conversion) {
l.push(entry.conversion.commodity);
}
}
if (entry.conversion) {
l.push(entry.conversion.commodity);
}
return l;

@@ -88,44 +298,13 @@ });

/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
/* global Reflect, Promise */
var extendStatics = function(d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
function __extends(d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
function parseHeaderLine(str) {
var _a = str.split(/\s+;\s+/), main = _a[0], comment = _a[1];
var _b = main.split(/\s+/), date = _b[0], other = _b.slice(1);
var confirmed = other[0] === "*";
if (confirmed) {
other.shift();
}
var description = other.join(" ");
return { date: new Date(date), confirmed: confirmed, description: description, comment: comment };
}
var __assign = function() {
__assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
function getAmount(str) {

@@ -189,11 +368,2 @@ var matches = str.match(/[\-\+]?[0-9]*[\.0-9]+/);

function parseHeaderLine(str) {
var _a = str.split(/\s+/), date = _a[0], other = _a.slice(1);
var confirmed = other[0] === "*";
if (confirmed) {
other.shift();
}
var description = other.join(" ");
return { date: new Date(date), confirmed: confirmed, description: description };
}
function isDate(str) {

@@ -205,4 +375,4 @@ return /^\d{4}-\d{2}-\d{2}$/.test(str);

}
function hasEntries(trx) {
return isTransaction(trx) && trx.entries.length > 0;
function isDirective(str) {
return /^P/.test(str);
}

@@ -215,2 +385,14 @@ /**

}
function mkComment(str) {
return {
message: clearComment(str),
};
}
function mkDirective(str) {
return { symbol: "P", data: str.replace(/^P\s*/, "") };
}
function mkTransaction(other) {
if (other === void 0) { other = {}; }
return __assign({ date: new Date(), entries: [] }, other);
}
var Transformer = /** @class */ (function (_super) {

@@ -232,2 +414,3 @@ __extends(Transformer, _super);

var broken = trimmed.split(/\s+/);
var isIndented = line.startsWith(INDENT);
if (trimmed === "") {

@@ -239,4 +422,6 @@ callback();

// Either it is a comment alone
// or a comment within a transaction
if (isTransaction(this.chunk) && !hasEntries(this.chunk)) {
// or a comment within a transaction.
// If it is indented it belongs to the previous transaction.
if (isTransaction(this.chunk) && isIndented) {
this.chunk.entries.push(mkComment(trimmed));
this.chunk.comment = clearComment(trimmed);

@@ -253,6 +438,13 @@ }

}
if (isDirective(trimmed)) {
// a directive after some entries closes the transaction.
this.clearChunk();
this.chunk = mkDirective(trimmed);
callback();
return;
}
if (isDate(broken[0])) {
// a date starts a new chunk
this.clearChunk();
this.chunk = __assign(__assign({}, parseHeaderLine(trimmed)), { entries: [] });
this.chunk = mkTransaction(parseHeaderLine(trimmed));
}

@@ -274,20 +466,18 @@ else if (this.chunk && isTransaction(this.chunk)) {

function parse(stream) {
var trxs = [];
var transformer = new Transformer();
return new Promise(function (resolve, reject) {
stream
.pipe(split2__default["default"]())
.pipe(transformer)
.on("data", function (data) {
trxs.push(data);
})
.on("end", function () {
resolve({
journal: trxs,
accounts: getAccountsSorted(trxs),
commodities: getCommodities(trxs),
});
})
.on("error", function () {
reject();
return __awaiter(this, void 0, void 0, function () {
var transformer, readable, journal;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
transformer = new Transformer();
readable = stream.pipe(split2__default["default"]()).pipe(transformer);
return [4 /*yield*/, collect(readable)];
case 1:
journal = _a.sent();
return [2 /*return*/, {
journal: journal,
accounts: getAccountsSorted(journal),
commodities: getCommodities(journal),
}];
}
});

@@ -297,2 +487,5 @@ });

exports.Formatter = Formatter;
exports.Parser = Transformer;
exports.formatComment = formatComment;
exports.formatTransaction = formatTransaction;

@@ -299,0 +492,0 @@ exports.isTransaction = isTransaction;

export declare type Posting = {
account: string;
amount?: string;
amount?: string | number;
commodity?: string;

@@ -16,3 +16,3 @@ conversion?: {

confirmed?: boolean;
entries: Posting[];
entries: (Posting | Comment)[];
};

@@ -22,2 +22,7 @@ export declare type Comment = {

};
export declare type Journal = (Transaction | Comment)[];
export declare type Directive = {
symbol: "P";
data: string;
};
export declare type JournalEntries = Transaction | Comment | Directive;
export declare type Journal = JournalEntries[];

@@ -6,2 +6,23 @@ # Change Log

# [1.2.0](https://github.com/kajyr/pta-tools/compare/pta-tools@1.1.5...pta-tools@1.2.0) (2021-12-13)
### Bug Fixes
* added npmignore ([217378c](https://github.com/kajyr/pta-tools/commit/217378c09b72410c14b2df7076c7733cebe3f5a5))
* amount can be a number ([4c3638b](https://github.com/kajyr/pta-tools/commit/4c3638b41a826db47c7a07b5dafa47bd1f83efee))
* fix newline after comment blocks ([3e1922e](https://github.com/kajyr/pta-tools/commit/3e1922e034c72b4170183c750ebde01004c6dc35))
### Features
* comments in entries ([3b257c6](https://github.com/kajyr/pta-tools/commit/3b257c66175138c8a94c33553f137fb08f928bce))
* comments in header of trx ([d9ee3f0](https://github.com/kajyr/pta-tools/commit/d9ee3f06d7a015d5576b1a3c49cad0722647c9a4))
* directive parsing and formatting ([6ec135c](https://github.com/kajyr/pta-tools/commit/6ec135cae5b3f4623d9895be13b58835b147a4b0))
* Initial pta-format cli ([68120f8](https://github.com/kajyr/pta-tools/commit/68120f8c07b2eced9f5a69d20e80f407a14a58da))
## [1.1.5](https://github.com/kajyr/pta-tools/compare/pta-tools@1.1.4...pta-tools@1.1.5) (2021-12-13)

@@ -8,0 +29,0 @@

{
"name": "pta-tools",
"version": "1.1.5",
"version": "1.2.0",
"description": "Plain text accounting journal JS apis",

@@ -9,3 +9,2 @@ "main": "build/index.js",

"watch": "rollup -w -c rollup.config.js",
"test": "jest",
"prerelease": "npm run build"

@@ -24,10 +23,7 @@ },

"devDependencies": {
"@types/jest": "^27.0.2",
"@types/node": "^16.11.6",
"@types/split2": "^3.2.1",
"jest": "^27.3.1",
"rimraf": "^3.0.2",
"rollup": "^2.59.0",
"rollup-plugin-typescript2": "^0.30.0",
"ts-jest": "^27.0.7",
"tslib": "^2.3.1",

@@ -39,3 +35,3 @@ "typescript": "^4.4.4"

},
"gitHead": "b49b413084a955b916870c334c80e1e983e74e58"
"gitHead": "bb8c3822df58c3d736c189b150e5b725144bf53e"
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc