fixedwidth 0.2.0 copy "fixedwidth: ^0.2.0" to clipboard
fixedwidth: ^0.2.0 copied to clipboard

outdatedDart 1 only

A library for working with fixed width files in dart. Easily turn data to and convert from properly formatted fixed width strings with readable layout definitions.

example/fixedwidth_example.dart

import 'package:intl/intl.dart';
import 'package:fixedwidth/fixedwidth.dart';

class PhoneNumber extends Record {
  IntegerField area_code = new IntegerField(3);
  IntegerField prefix = new IntegerField(3);
  IntegerField line_number = new IntegerField(4);

  PhoneNumber();
  PhoneNumber.fromRecord(String record) : super.fromString(record);
}

/// Person Record demonstrates using most field types.
///
/// The phone_number field is a `RecordField` which is comprised of
/// an entire record.
class PersonRecord extends Record {
  StringField first_name = new StringField(20);
  StringField last_name = new StringField(20);
  DateTimeField dob =
      new DateTimeField(10, format: new DateFormat("yyyy-MM-dd"));
  IntegerField num_siblings = new IntegerField(2, defaultValue: 0);
  DecimalField amount_due = new DecimalField(8, decimals: 2);
  RecordField phone_number = new RecordField(PhoneNumber);

  PersonRecord();
  PersonRecord.fromRecord(String record) : super.fromString(record);
}

main() {
  // You can take a Record, populate it, and turn it to a fixed width string.
  var phone = new PhoneNumber()
    ..area_code.value = 215
    ..prefix.value = 222
    ..line_number.value = 3333;

  var record = new PersonRecord()
    ..first_name.value = "John"
    ..last_name.value = "Doe"
    ..dob.value = new DateTime(1980, 4, 16)
    ..num_siblings.value = 2
    ..amount_due.value = 24.75
    ..phone_number.value = phone;
  print("'${record.toString()}'");
  print("Total Record Length: ${record.length}");

  // You can take a fixed width string and turn it into the appropriate
  // dart object
  var record2 = new PersonRecord.fromRecord(
      "Benjamin            Franklin            1706-01-171600003.002151112222");
  print(record2.first_name);
  print(record2.last_name);
  print(record2.dob.value);
  print(record2.num_siblings.value);
  print(record2.amount_due.value);
  print(record2.phone_number.value.area_code.value);
  print(record2.phone_number.value.prefix.value);
  print(record2.phone_number.value.line_number.value);
}
0
likes
0
pub points
0%
popularity

Publisher

unverified uploader

A library for working with fixed width files in dart. Easily turn data to and convert from properly formatted fixed width strings with readable layout definitions.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

intl

More

Packages that depend on fixedwidth