Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Automatically encode and decode Cyrillic, Hebrew, and KS C 5601 #127

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
36 changes: 36 additions & 0 deletions lib/defs.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,30 @@ encodings.LATIN1 = {
}
};

encodings.CYRILLIC = {
match: function(value) {
return false;
},
encode: function(value) {
return iconv.encode(value, 'iso88595');
},
decode: function(value) {
return iconv.decode(value, 'iso88595');
}
};

encodings.HEBREW = {
match: function(value) {
return false;
},
encode: function(value) {
return iconv.encode(value, 'iso88598');
},
decode: function(value) {
return iconv.decode(value, 'iso88598');
}
};

encodings.UCS2 = {
match: function(value) {
return true;
Expand All @@ -401,6 +425,18 @@ encodings.UCS2 = {
}
};

encodings.KS_C_5601 = {
match: function(value) {
return false;
},
encode: function(value) {
return iconv.encode(value, 'ksc5601');
},
decode: function(value) {
return iconv.decode(value, 'ksc5601');
}
};

Object.defineProperty(encodings, 'detect', {
value: function(value) {
for (var key in encodings) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"mocha": "2.x"
},
"dependencies": {
"iconv-lite": "0.x",
"iconv-lite": ">= 0.4 < 1",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why limit upper bound?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it was limited before this change too (to 0.x, effectively < 1). Probably good to keep it like that for compatibility reasons. Added lower bound only because it's required for ksc5601.

"safer-buffer": ">= 2.1.2 < 3"
}
}
96 changes: 96 additions & 0 deletions test/encodings.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,102 @@ describe('encodings', function() {
});
});

describe('CYRILLIC', function() {
var CYRILLIC = encodings.CYRILLIC;
var samples = {
'Здравствуйте': [0xb7, 0xd4, 0xe0, 0xd0, 0xd2, 0xe1, 0xe2, 0xd2, 0xe3, 0xd9, 0xe2, 0xd5],
};

describe('#match()', function() {
it('should always return false (at least for now)', function() {
assert(!CYRILLIC.match(''));
for(var str in samples) {
assert(!CYRILLIC.match(''));
}
});
});

describe('#encode', function() {
it('should properly encode the given string using ISO-8859-5 charset', function() {
for(var str in samples) {
assert.deepEqual(CYRILLIC.encode(str), Buffer.from(samples[str]));
}
});
});

describe('#decode', function() {
it('should properly decode the given buffer using ISO-8859-5 charset', function() {
for(var str in samples) {
assert.deepEqual(CYRILLIC.decode(samples[str]), str);
}
});
});
});

describe('HEBREW', function() {
var HEBREW = encodings.HEBREW;
var samples = {
'הלו': [0xe4, 0xec, 0xe5],
};

describe('#match()', function() {
it('should always return false (at least for now)', function() {
assert(!HEBREW.match(''));
for(var str in samples) {
assert(!HEBREW.match(''));
}
});
});

describe('#encode', function() {
it('should properly encode the given string using ISO-8859-8 charset', function() {
for(var str in samples) {
assert.deepEqual(HEBREW.encode(str), Buffer.from(samples[str]));
}
});
});

describe('#decode', function() {
it('should properly decode the given buffer using ISO-8859-8 charset', function() {
for(var str in samples) {
assert.deepEqual(HEBREW.decode(samples[str]), str);
}
});
});
});

describe('KS_C_5601', function() {
var KS_C_5601 = encodings.KS_C_5601;
var samples = {
'여보세요': [0xbf, 0xa9, 0xba, 0xb8, 0xbc, 0xbc, 0xbf, 0xe4],
};

describe('#match()', function() {
it('should always return false (at least for now)', function() {
assert(!KS_C_5601.match(''));
for(var str in samples) {
assert(!KS_C_5601.match(''));
}
});
});

describe('#encode', function() {
it('should properly encode the given string using KS C 5601 charset', function() {
for(var str in samples) {
assert.deepEqual(KS_C_5601.encode(str), Buffer.from(samples[str]));
}
});
});

describe('#decode', function() {
it('should properly decode the given buffer using KS C 5601 charset', function() {
for(var str in samples) {
assert.deepEqual(KS_C_5601.decode(samples[str]), str);
}
});
});
});

describe('#detect()', function() {
it('should return proper encoding for the given string', function() {
assert.equal(encodings.detect(''), 'ASCII');
Expand Down