Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions decoder-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@ function defaultDecoder(data) {
return out.replace(/\0/g, '').trim();
}
module.exports = createDecoder;
var regex = /^(?:ASNI\s)?(\d+)$/m;
var regex = /^(?:ANSI\s)?(\d+)$/m;

function createDecoder(encoding) {
if (!encoding) {
return defaultDecoder;
}
encoding = String(encoding).trim();
var match = regex.exec(encoding);
if (match) {
encoding = 'windows-' + match[1];
}
try {
new TextDecoder(encoding.trim());
new TextDecoder(encoding);
return browserDecoder;
} catch(e) {
var match = regex.exec(encoding);
if (match) {
encoding = 'windows-' + match[1];
}
return defaultDecoder;
}
return browserDecoder;
function browserDecoder(buffer) {
Expand Down
16 changes: 16 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<html>
<head>
<meta charset="utf-8">
<title>Mocha Tests</title>
<link rel="stylesheet" href="/node_modules/mocha/mocha.css" />
<script src="/node_modules/mocha/mocha.js"></script>
<script>mocha.setup({ ui: "bdd" })</script>
</head>
<body>
<div id="mocha"></div>
<script src="/test/bundle.js"></script>
<script>
mocha.run();
</script>
</body>
</html>
17 changes: 10 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "The DBF parsing component of shapefile-js",
"main": "index.js",
"scripts": {
"test-browser": "budo test/basic.test.js --serve test/bundle.js --open --live -- -t brfs",
"test": "jshint index.js && mocha -R spec test/*.test.js"
},
"repository": {
Expand All @@ -20,15 +21,17 @@
"url": "https://github.com/calvinmetcalf/parseDBF/issues"
},
"devDependencies": {
"chai": "^3.3.0",
"jshint": "^2.8.0",
"mocha": "^2.3.3"
},
"browser": {
"./decoder.js": "./decoder-browser.js"
"brfs": "^1.4.3",
"budo": "^10.0.4",
"chai": "^4.1.2",
"jshint": "^2.9.5",
"mocha": "^3.5.3"
},
"dependencies": {
"iconv-lite": "^0.4.15",
"iconv-lite": "^0.4.19",
"text-encoding-polyfill": "^0.6.7"
},
"browser": {
"./decoder.js": "./decoder-browser.js"
}
}
27 changes: 25 additions & 2 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,39 @@ describe('dbf',function(){
done();
});
});
it('should handle other charicters',function(done){
it('should handle ANSI characters with different encoding names',function(done){
fs.readFile('./test/data/codepage.dbf',function(err,data){
if(err){
return done(err);
}
dbf(data)[1].should.not.deep.equal(utf[1]);
dbf(data, '1250')[1].should.deep.equal(utf[1]);
dbf(data, 'ASNI 1250')[1].should.deep.equal(utf[1]);
dbf(data, 'ANSI 1250')[1].should.deep.equal(utf[1]);
dbf(data, 'windows-1250')[1].should.deep.equal(utf[1]);
done();
});
});
it('should handle invalid encodings',function(done){
var htmlpage = fs.readFileSync('./index.html', 'utf-8');
fs.readFile('./test/data/watershed.dbf',function(err,data){
if(err){
return done(err);
}
dbf(data, data).should.deep.equal(basic);
dbf(data, htmlpage).should.deep.equal(basic);
done();
});
});
it('should handle non-decoded encodings',function(done){
var encoding = fs.readFileSync('./test/data/encoding.cpg', 'utf-8');
var encodingBinary = fs.readFileSync('./test/data/encoding.cpg');
fs.readFile('./test/data/utf.dbf',function(err,data){
if(err){
return done(err);
}
dbf(data, encoding).should.deep.equal(utf);
dbf(data, encodingBinary).should.deep.equal(utf);
done();
});
});
});
1 change: 1 addition & 0 deletions test/data/encoding.cpg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
utf8