1.0.7 • Published 4 years ago

ngx-file-parser v1.0.7

Weekly downloads
5
License
MIT
Repository
github
Last release
4 years ago

ngx-file-parser

npm version

Simple component to parse files with extensions

  • CSV
  • JSON

To be implemented: XML....

See demo

Dependencies

Installation

1.

NPM: npm install ngx-file-parser --save

Yarn: yarn add ngx-file-parser

2.

Import the NgxFileParserModule to provide the necessary components and directives.

import { NgxFileParserModule } from 'ngx-file-parser';

@NgModule({
  ...
  imports: [ NgxFileParserModule ],
  ...
})
export class AppModule { }

Usage

Declare NgxFileParserConfig object to provide to the directive

NgxFileParserConfig has the following properties

PropertyDescriptionDefault
btnTextText to be displayed on buttonChoose file
btnIconMaterial icon to be displayed on buttonbackup
btnColor*Color accent to the buttontransparent
acceptsArray of file name extensions'.csv'
csvNamedPropertiesIf parsed CSV file should be returned as array of objects with named propertiesfalse

*See Angular Material buttons for accents

Example

import { NgxFileParserConfig } from "ngx-file-parser";

ngxFileParserConfig: NgxFileParserConfig = {
  btnText: "Upload",
  btnIcon: "backup",
  btnColor: "primary",
  accepts: [".csv"],
  csvNamedProperties: true,
};

Use the ngx-file-btn directive and provide the needed config object and event listener function to handle the parsed object

<ngx-file-btn
  [(config)]="ngxFileParserConfig"
  (parsedFile)="handleParsedFile($event)"
></ngx-file-btn>

ngx-file-btn has the following output events

EventDescriptionType
parsedFileThe result of the parsed file.INgxResult
processingIf the file is being processedboolean
export interface INgxResult {
  extension: string;
  result: INgxCsv | INgxJson | INgxComplexCsv;
}

Returns

All parsed object is returned as INgxResult with the extension of the file and a result object and is emitted to this event listener with the \$event containing the parsed object

  handleParsedFile(parsedFileObj: INgxResult) {
    this.parsedFile = parsedFileObj.result as INgxCsv | INgxJson | INgxComplexCsv;
  }

CSV

Returns the interface INgxCsv with properties | Property | Description | | -------- | ----------------| | headers | Array of strings | | data | Array of arrays of string |

CSV Named Properties

Returns the interface INgxComplexCsv with properties | Property | Description | | -------- | ----------------| | key: string: string; | Property as definied in the upload CSV |

JSON

Returns the interface INgxJson with the properties that are definied in the upload JSON file

Contribute

All PR's, issues and contributors are welcome! Feel free to start contributing

License

MIT