Fixed width file processing/parsing with TS Decorators
This lib provide decorator for fixed width file so that the domain class and spec can stay together. No separate schema creation is needed so that it is more readable, maintainable and extensible.
Usage
npm i fixed-width-ts-decorator
Example
This lib uses reflect-metadata for Meta programming.
Inheritance is supported that fields decorated in parent classes will also be included/processed.
Model definition
export class Transaction extends FixedWidthConvertible {
@FixedWidth({ start: 0, width: 5 })
clientId: string;
@FixedWidth({ start: 5, width: 40 })
parentName: string;
@FixedWidth({ start: 45, width: 40, format: { type: DataType.Float } })
taxAmount: number;
@FixedWidth({
start: 85,
width: 40,
format: {
type: DataType.Float,
precision: 3,
},
})
paymentAmount: number;
@FixedWidth({
start: 125,
width: 20,
format: { type: DataType.Integer },
})
paymentTimeStamp: number;
@FixedWidth({ start: 145, width: 40 })
userId: string;
@FixedWidth({ start: 225, width: 40 })
paymentId: string;
otherField: string;
}
Parse the file to Objects
(() => {
const file = fs.readFileSync('data/sample.txt', { encoding: 'utf8' });
const lines = file.split('\n').filter(l => l);
const rs: Array<Transaction> = lines.map(line => {
const trans = new Transaction();
trans.convertFixedWidth(line);
return trans;
});
console.log(rs);
})()
Format Options
- support parse to Integer or Float(with precision) other than the default String type.