reader-files
TypeScript icon, indicating that this package has built-in type declarations

1.0.4 • Public • Published

Reader-Files

This Angular Module allows you to read and parse CSV files based on object type yu provided and JSON files. This service cna read single and batch files.

For multiple CSV files it merges the data <T[]> into one observable For multiple json files it provides an array with { file : string, data: any }

NOTE: If you are using multiple instances of this class to read files, extend the class of the complent and add the type of the object and do not import it as a dependency into the class of the component.

For CSV files there are 2 options for the object that is defined

  1. If parseAsObject is FALSE, then the returned data will be a array[]

  2. If parseAsObject is TRUE, then the returned data will be an object defined by the header (firstline of data) data will be array[T]

Option 1:

["Government of Canada",Canada,CA,Ontario,ON,K1A],
["Government of Canada",Canada,CA,Ontario,ON,K1A]

Option 2:

[
  { city: "Government of Canada", country: "Canada', country_abbr: "CA", region: "Ontario", region_abbr: "ON",zip: "ON,K1A" },
  { city: "Government of Canada", country: "Canada', country_abbr: "CA", region: "Ontario", region_abbr: "ON",zip: "ON,K1A" }
]

File Strtcture for CSV

Your first line should define the model of the data

Installation

npm install reader-files

Scaffolding

Import the module into your project under imports

imports: [
    BrowserModule,
    AppRoutingModule,
    ReaderFilesModule,
  ],

Use

In your component file, import the module in the constructor and add the generic type

private readerParser: ReaderFileParserService<CountryRegionCity>,

Then call any of the functions, like the eample below

const file = 'assets/data/united-states.csv'
this.dataTextFile = this.readerParser.readCSVFile(file, true)

Here is a sample of a component setup

export class AppComponent implements OnInit {

  dataTextFile: Observable<CountryRegionCity[]>
  dataTextFiles: Observable<CountryRegionCity[]>

  dataJsonFile: any
  dataJsonFiles: any

  constructor(
    private readerParser: ReaderFileParserService<CountryRegionCity>
    ) {}

  ngOnInit() {

    const file = 'assets/data/united-states.csv'
    const files = ['assets/data/canada.csv', 'assets/data/united-states.csv']

    const jsonFile = 'assets/data/sample-1.json'
    const jsonFiles = ['assets/data/sample-1.file.service.json', 'assets/data/sample-2.json']

    this.dataTextFile = this.readerParser.readCSVFile(file, true)
    this.dataTextFiles = this.readerParser.readCSVFiles(files, true)

    this.dataJsonFile = this.readerParser.readJSONFile(jsonFile)
    this.dataJsonFiles = this.readerParser.readJSONFiles(jsonFiles)

    console.log(this.dataJson)

  }

}

Package Sidebar

Install

npm i reader-files

Weekly Downloads

1

Version

1.0.4

License

ISC

Unpacked Size

97 kB

Total Files

16

Last publish

Collaborators

  • wavecoders