11import { CpLUT } from './codePage' ;
2- import { encodingExists , decode } from 'iconv-lite ' ;
2+ import { Buffer } from 'buffer ' ;
33
44// ESRI article on encoding: https://support.esri.com/en/technical-article/000013192
55// "If a dBASE file lacks an LDID or a .CPG file, it assumes the file is encoded in the Windows (ANSI/Multi-byte) code page."
@@ -16,50 +16,49 @@ const regExUTF8 = /^.*UTF[-\s]?8\s*$/;
1616
1717export class DbfDecoder {
1818 public readonly encoding : string ;
19+ private decoder : TextDecoder ;
1920
2021 constructor ( encoding : string ) {
2122 this . encoding = encoding ;
23+ this . decoder = new TextDecoder ( encoding ) ;
2224 }
2325
24- decode ( str : Buffer ) : string {
25- return decode ( str , this . encoding ) ;
26+ decode ( str : Buffer ) {
27+ return this . decoder . decode ( str ) ;
2628 }
2729}
2830
29- export function fromCpgString ( cpg : string ) : DbfDecoder {
30- if ( ! cpg ) {
31- throw new Error ( 'No codepage/CPG string provided' ) ;
32- }
33- if ( cpg . match ( regExUTF8 ) ) {
34- return new DbfDecoder ( 'utf8' ) ;
31+ function encodingExists ( encoding : string ) : boolean {
32+ try {
33+ new TextDecoder ( encoding ) ;
34+ return true ;
35+ } catch ( e ) {
36+ return false ;
3537 }
38+ }
39+
40+ export function fromCpgString ( cpg : string ) : DbfDecoder {
41+ if ( ! cpg ) throw new Error ( 'No codepage/CPG string provided' ) ;
42+ if ( cpg . match ( regExUTF8 ) ) return new DbfDecoder ( 'utf8' ) ;
3643 let m = cpg . match ( regExIso ) ;
37- if ( m != null ) {
38- return new DbfDecoder ( `ISO-8859-${ m [ 1 ] } ` ) ;
39- }
44+ if ( m != null ) return new DbfDecoder ( `ISO-8859-${ m [ 1 ] } ` ) ;
4045 m = cpg . match ( regExAnsi ) ;
4146 if ( m != null ) {
4247 const code = parseInt ( m [ 1 ] ) ;
4348 const encoding = `cp${ code } ` ;
44- if ( ! encodingExists ( encoding ) ) {
45- throw new Error ( `Encoding ${ encoding } not supported` ) ;
46- }
49+ if ( ! encodingExists ( encoding ) ) throw new Error ( `Encoding ${ encoding } not supported` ) ;
50+
4751 return new DbfDecoder ( encoding ) ;
4852 }
4953 return new DbfDecoder ( 'cp1252' ) ;
5054}
5155
5256export function fromDbfLangCode ( code : number ) : DbfDecoder | undefined {
53- if ( code === 0 ) {
54- // Default = 1252
55- return new DbfDecoder ( 'cp1252' ) ;
56- }
57+ if ( code === 0 ) return new DbfDecoder ( 'cp1252' ) ; //Default
5758 if ( code in CpLUT ) {
5859 const cpId = CpLUT [ code ] [ 0 ] as number ;
5960 const encoding = `cp${ cpId } ` ;
60- if ( ! encodingExists ( encoding ) ) {
61- throw new Error ( `Encoding ${ encoding } not supported` ) ;
62- }
61+ if ( ! encodingExists ( encoding ) ) throw new Error ( `Encoding ${ encoding } not supported` ) ;
6362 return new DbfDecoder ( encoding ) ;
6463 }
6564 throw new Error ( `Could not find converter for codepage ${ code } ` ) ;
0 commit comments