Skip to content

Commit

Permalink
feat: export all types and enums & cleanup a bit (#290)
Browse files Browse the repository at this point in the history
  • Loading branch information
hertzg authored Oct 26, 2021
1 parent 5b98f16 commit da502de
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 114 deletions.
2 changes: 1 addition & 1 deletion src/entities/_parse.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { gCharP, gDouble, gInt, gUInt32 } from './_g_types'
import type { gCharP, gDouble, gInt, gUInt32 } from './_g_types'

const parseAsInt = <T extends number>(s: string): T => parseInt(s, 10) as T
const parseAsString = <T extends string>(s: string) => s && (String(s) as T)
Expand Down
2 changes: 1 addition & 1 deletion src/entities/_serialize.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { sprintf } from 'printj'
import { gCharP, gDouble, gUInt32 } from './_g_types'
import type { gCharP, gDouble, gUInt32 } from './_g_types'

export const hb_xml_attr_txt = (attrName: string, value: gCharP) =>
value === null || value === undefined
Expand Down
6 changes: 3 additions & 3 deletions src/entities/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
hb_xml_attr_txt_crlf,
hb_xml_tag,
} from './_serialize'
import { gCharP, gDouble, gUInt32, gUShort } from './_g_types'
import type { gCharP, gDouble, gUInt32, gUShort } from './_g_types'

export interface Account {
key: gUInt32
Expand Down Expand Up @@ -50,7 +50,7 @@ export enum AccountType {
//TYPE_EQUITY = 10 as gushort, // Capitaux propres
}

export function parse({ attributes }: Node): Account {
export function parseAccount({ attributes }: Node): Account {
return {
key: atoi(attributes.key),
flags: atoi(attributes.flags),
Expand All @@ -69,7 +69,7 @@ export function parse({ attributes }: Node): Account {
}
}

export const serialize = (account: Account): string =>
export const serializeAccount = (account: Account): string =>
hb_xml_tag(
'<account',
hb_xml_attr_int('key', account.key),
Expand Down
4 changes: 2 additions & 2 deletions src/entities/archive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export enum ScheduledWeekendPolicy {
AFTER = 2 as gUShort,
}

export function parse({ attributes }: Node): Archive {
export function parseArchive({ attributes }: Node): Archive {
const tags: gCharP[] = attributes.tags
? parseGCharP(attributes.tags).split(' ')
: []
Expand Down Expand Up @@ -128,7 +128,7 @@ const archiveSplitsToSplits = (aSplits: ArchiveSplit[]): AttrSplit[] =>
mem: aSplit.memo,
}))

export const serialize = (archive: Archive): string => {
export const serializeArchive = (archive: Archive): string => {
const tags = tags_toStr(archive.tags)
const splits = archiveSplitsToSplits(archive.splits)
return hb_xml_tag(
Expand Down
8 changes: 4 additions & 4 deletions src/entities/assign.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Node } from 'xml-parser'
import { PayMode } from './payee'
import { PayMode } from './operation'
import { atoi, parseGCharP } from './_parse'
import { VolatileXHB } from '../index'
import { hb_xml_attr_int, hb_xml_attr_txt, hb_xml_tag } from './_serialize'
import { gCharP, gUInt32, gUShort } from './_g_types'
import type { gCharP, gUInt32, gUShort } from './_g_types'

export interface Assign {
key: gUInt32
Expand Down Expand Up @@ -31,7 +31,7 @@ export enum AssignFlag {
OVWMOD = (1 << 11) as gUShort,
}

export function parse({ attributes }: Node, xhb: VolatileXHB): Assign {
export function parseAssign({ attributes }: Node, xhb: VolatileXHB): Assign {
const entry: Assign = {
key: atoi(attributes.key),
flags: atoi(attributes.flags),
Expand All @@ -53,7 +53,7 @@ export function parse({ attributes }: Node, xhb: VolatileXHB): Assign {
return entry
}

export const serialize = (assign: Assign): string =>
export const serializeAssign = (assign: Assign): string =>
hb_xml_tag(
'<asg',
hb_xml_attr_int('key', assign.key),
Expand Down
8 changes: 4 additions & 4 deletions src/entities/category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
hb_xml_tag,
} from './_serialize'
import { sprintf } from 'printj'
import { gCharP, gDouble, gUInt32, gUShort } from './_g_types'
import type { gCharP, gDouble, gUInt32, gUShort } from './_g_types'

export interface Category {
key: gUInt32
Expand All @@ -25,7 +25,7 @@ export enum CategoryFlag {
FORCED = (1 << 4) as gUShort,
}

export function parse({ attributes }: Node): Category {
export function parseCategory({ attributes }: Node): Category {
const budgets: gDouble[] = new Array(12)
for (let i = 0, ln = 12; i <= ln; i++) {
const bAttr = `b${i}`
Expand All @@ -46,11 +46,11 @@ export function parse({ attributes }: Node): Category {
const hb_xml_attrs_budgets = (budget) =>
Array.isArray(budget)
? budget
.filter((b) => b !== null || b !== undefined)
.filter((b) => b !== null && b !== undefined)
.map((v, i) => sprintf('b%d="%s"', i, dtostr(v)))
.join(' ')
: ''
export const serialize = (category: Category): string =>
export const serializeCategory = (category: Category): string =>
hb_xml_tag(
'<cat',
hb_xml_attr_int('key', category.key),
Expand Down
6 changes: 3 additions & 3 deletions src/entities/currency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Node } from 'xml-parser'
import { atoi, parseGCharP, parseGDouble } from './_parse'
import { sprintf } from 'printj'
import { dtostr } from './_serialize'
import { gBoolean, gCharP, gDouble, gShort, gUInt32, gUShort } from './_g_types'
import type { gBoolean, gCharP, gDouble, gShort, gUInt32, gUShort } from './_g_types'

export interface Currency {
key: gUInt32
Expand All @@ -23,7 +23,7 @@ export enum CurrencyFlag {
CUSTOM = (1 << 1) as gUShort,
}

export function parse({ attributes }: Node): Currency {
export function parseCurrency({ attributes }: Node): Currency {
return {
key: atoi(attributes.key),
flags: atoi(attributes.flags),
Expand All @@ -39,7 +39,7 @@ export function parse({ attributes }: Node): Currency {
}
}

export const serialize = (currency: Currency): string =>
export const serializeCurrency = (currency: Currency): string =>
sprintf(
'<cur key="%d" flags="%d" iso="%s" name="%s" symb="%s" syprf="%d" dchar="%s" gchar="%s" frac="%d" rate="%s" mdate="%d"/>',
currency.key,
Expand Down
13 changes: 0 additions & 13 deletions src/entities/index.ts

This file was deleted.

14 changes: 3 additions & 11 deletions src/entities/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
hb_xml_tag,
tags_toStr,
} from './_serialize'
import { gCharP, gDouble, gUInt32, gUShort } from './_g_types'
import type { gCharP, gDouble, gUInt32, gUShort } from './_g_types'

export interface Operation {
date: gUInt32
Expand Down Expand Up @@ -63,15 +63,7 @@ export enum OperationFlag {
SPLIT = (1 << 8) as gUShort,
}

export enum ArchiveStatus {
NONE,
CLEARED,
RECONCILED,
REMIND,
//VOID
}

export function parse({ attributes }: Node): Operation {
export function parseOperation({ attributes }: Node): Operation {
const tags: gCharP[] = attributes.tags
? parseGCharP(attributes.tags).split(' ')
: []
Expand Down Expand Up @@ -123,7 +115,7 @@ const operationSplitsToSplits = (aSplits: OperationSplit[]): AttrSplit[] =>
mem: aSplit.memo,
}))

export const serialize = (operation: Operation): string => {
export const serializeOperation = (operation: Operation): string => {
const tags = tags_toStr(operation.tags)
const splits = operationSplitsToSplits(operation.splits)
return hb_xml_tag(
Expand Down
22 changes: 3 additions & 19 deletions src/entities/payee.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Node } from 'xml-parser'
import { atoi, parseGCharP } from './_parse'
import { hb_xml_attr_int, hb_xml_attr_txt, hb_xml_tag } from './_serialize'
import { gCharP, gUInt32, gUShort } from './_g_types'
import type { gCharP, gUInt32, gUShort } from './_g_types'

export interface Payee {
key: gUInt32
Expand All @@ -10,23 +10,7 @@ export interface Payee {
category: gUInt32
}

export enum PayMode {
NONE = 0 as gUShort,
CCARD = 1 as gUShort,
CHECK = 2 as gUShort,
CASH = 3 as gUShort,
XFER = 4 as gUShort,
INTXFER = 5 as gUShort,
DCARD = 6 as gUShort,
REPEATPMT = 7 as gUShort,
EPAYMENT = 8 as gUShort,
DEPOSIT = 9 as gUShort,
FEE = 10 as gUShort,
DIRECTDEBIT = 11 as gUShort,
NUM_PAYMODE_MAX = 12 as gUShort,
}

export function parse({ attributes }: Node): Payee {
export function parsePayee({ attributes }: Node): Payee {
return {
key: atoi(attributes.key),
name: parseGCharP(attributes.name),
Expand All @@ -35,7 +19,7 @@ export function parse({ attributes }: Node): Payee {
}
}

export const serialize = (payee: Payee): string =>
export const serializePayee = (payee: Payee): string =>
hb_xml_tag(
'<pay',
hb_xml_attr_int('key', payee.key),
Expand Down
6 changes: 3 additions & 3 deletions src/entities/properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
hb_xml_attr_txt,
hb_xml_tag,
} from './_serialize'
import { gCharP, gUInt32, gUShort } from './_g_types'
import type { gCharP, gUInt32, gUShort } from './_g_types'

export interface Properties {
owner: gCharP
Expand All @@ -22,7 +22,7 @@ export enum VehicleScheduledTransactionMode {
NUMBER_OF_DAYS = 1 as gUShort,
}

export function parse({ attributes }: Node): Properties {
export function parseProperties({ attributes }: Node): Properties {
return {
owner: parseGCharP(attributes.title),
baseCurrency: atoi(attributes.curr),
Expand All @@ -33,7 +33,7 @@ export function parse({ attributes }: Node): Properties {
}
}

export const serialize = (properties: Properties): string =>
export const serializeProperties = (properties: Properties): string =>
hb_xml_tag(
'<properties',
hb_xml_attr_txt('title', properties.owner),
Expand Down
6 changes: 3 additions & 3 deletions src/entities/tag.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { Node } from 'xml-parser'
import { atoi, parseGCharP } from './_parse'
import { sprintf } from 'printj'
import { gCharP, gUInt32 } from './_g_types'
import type { gCharP, gUInt32 } from './_g_types'

export interface Tag {
key: gUInt32
name: gCharP
}

export function parse({ attributes }: Node): Tag {
export function parseTag({ attributes }: Node): Tag {
return {
key: atoi(attributes.key),
name: parseGCharP(attributes.name),
}
}

export const serialize = (tag: Tag): string =>
export const serializeTag = (tag: Tag): string =>
sprintf('<tag key="%d" name="%s"/>', tag.key, tag.name)
6 changes: 3 additions & 3 deletions src/entities/versions.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { Node } from 'xml-parser'
import { parseGDouble, parseGInt } from './_parse'
import { sprintf } from 'printj'
import { gDouble, gInt } from './_g_types'
import type { gDouble, gInt } from './_g_types'

export interface Versions {
file: gDouble
data: gInt
}

export function parse({ attributes }: Node): Versions {
export function parseVersions({ attributes }: Node): Versions {
return {
file: parseGDouble(attributes.v),
data: parseGInt(attributes.d),
}
}

export const serialize = (versions: Versions) =>
export const serializeVersions = (versions: Versions) =>
sprintf('<homebank v="%s" d="%06d">', versions.file, versions.data)
70 changes: 26 additions & 44 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,21 @@
import XMLParser, { Node } from 'xml-parser'
import {
parse as parseProperties,
parseProperties,
Properties,
serialize as serializeProperties,
serializeProperties,
} from './entities/properties'
import {
Category,
parse as parseCategory,
serialize as serializeCategory,
} from './entities/category'
import {
parse as parsePayee,
Payee,
serialize as serializePayee,
} from './entities/payee'
import {
Assign,
parse as parseAssign,
serialize as serializeAssign,
} from './entities/assign'
import {
Account,
parse as parseAccount,
serialize as serializeAccount,
} from './entities/account'
import {
parse as parseVersions,
serialize as serializeVersions,
Versions,
} from './entities/versions'
import {
Currency,
parse as parseCurrency,
serialize as serializeCurrency,
} from './entities/currency'
import {
parse as parseTag,
serialize as serializeTag,
Tag,
} from './entities/tag'
import {
Archive,
parse as parseArchive,
serialize as serializeArchive,
} from './entities/archive'
import { Category, parseCategory, serializeCategory } from './entities/category'
import { parsePayee, Payee, serializePayee } from './entities/payee'
import { Assign, parseAssign, serializeAssign } from './entities/assign'
import { Account, parseAccount, serializeAccount } from './entities/account'
import { parseVersions, serializeVersions, Versions } from './entities/versions'
import { Currency, parseCurrency, serializeCurrency } from './entities/currency'
import { parseTag, serializeTag, Tag } from './entities/tag'
import { Archive, parseArchive, serializeArchive } from './entities/archive'
import {
Operation,
parse as parseOperation,
serialize as serializeOperation,
parseOperation,
serializeOperation,
} from './entities/operation'

export interface XHB {
Expand Down Expand Up @@ -185,3 +153,17 @@ export const serialize = (xhb: XHB, options: SerializeOptions = {}): string => {
.filter((line) => line && line.length)
.join('\n')
}

export * from './entities/_g_types'
export * from './entities/_parse'
export * from './entities/_serialize'
export * from './entities/account'
export * from './entities/archive'
export * from './entities/assign'
export * from './entities/category'
export * from './entities/currency'
export * from './entities/operation'
export * from './entities/payee'
export * from './entities/properties'
export * from './entities/tag'
export * from './entities/versions'

0 comments on commit da502de

Please sign in to comment.