polygonplanet / encoding.js Goto Github PK
View Code? Open in Web Editor NEWConvert and detect character encoding in JavaScript
License: MIT License
Convert and detect character encoding in JavaScript
License: MIT License
タイトルの通りなのですが、特定の文字(制御文字)を含んだ Unicode を SJIS に変換しようとすると文字化けする様でした。当方の仕様に対する理解が浅いのかもしれませんが、一応レポートしておきます。
以下が、問題のあった文字になります。
[
'\u0000',
'\u0001',
'\u0002',
'\u0003',
'\u0004',
'\u0005',
'\u0006',
'\u0007',
'\u0008',
'\u000B',
'\u000C',
'\u000E',
'\u000F',
'\u0010',
'\u0011',
'\u0012',
'\u0013',
'\u0014',
'\u0015',
'\u0016',
'\u0017',
'\u0018',
'\u0019',
'\u001A',
'\u001B',
'\u001C',
'\u001D',
'\u001E',
'\u001F',
'\u007F',
]
例えば、この様な事をすると文字化けします。
// あ[\u0008]いう
const string = '\u3042\u0008\u3044\u3046';
new Uint8Array(EncodingJapanese.convert(Buffer.from(string), 'SJIS'));
1.0.10から1.0.11への変更にかけて、ASCIIと判定されたコードを変換しようとすると例外をはくようになりました。
これは下記のように非対応のエンコードをスルーしていたものを例外をはくように変更した影響で、対応している文字コードの範囲内ではスルー処理しても問題のないASCIIがはじかれることによって起こります。
if (hasOwnProperty.call(Encoding, func)) {
return Encoding[func](data);
}
}
return data;
if (!hasOwnProperty.call(EncodingDetect, method)) {
throw new Error('Undefined encoding: ' + e);
} else if (EncodingDetect[method](data)) {
return encoding;
}
AUTOでdetectしてASCIIだったらてきとうにSJISだよとしてconvertしたりすれば回避可能ですが、不便なのでできればスルーあるいはメソッド定義など対応お願いします。
UTF-8N形式のファイルがUNICODEに分類されます
UTF-8Nの際はUTF-8として出せたら良いなと思います
I got dependency errors in the latest version of this package 1.0.28
Could you please check it ? @polygonplanet
6.9.1
3.10.8
^13.0.0
Error: Cannot find module './util'
Error: Cannot find module './encoding-table'
Error: Cannot find module './config'
Error: Cannot find module './encoding-detect'
Error: Cannot find module './utf8-to-jis-table'
Error: Cannot find module './utf8-to-jisx0212-table'
Error: Cannot find module './jis-to-utf8-table'
Error: Cannot find module './jisx0212-to-utf8-table'
Error: Cannot find module './encoding-convert'
Error: Cannot find module './kana-case-table'
Sorry solved the problem
In these days, I think there is some demands to use encoding in the browser directory using native import
.
If you think this is meaningful, I will help to make changes.
In the case of an invalid array input when converting to Unicode, currently encoding.js output the invalid bytes as normal question marks (char code 63), so I can't remove invalid characters from the result without accidentally remove valid question marks. Can you ask an option to remove invalid characters after decoding, or at least use a special character instead of question mark as a placeholder for invalid result characters?
Sample code:
const targetBytes = encoding.convert(
'??',
{
to: 'SJIS',
type: 'array',
}
);
const result = encoding.convert(targetBytes.slice(0, 2), {
to: 'UNICODE',
from: 'SJIS',
type: 'string',
});
Result: ??
. Both characters are ?
even though the last one should be an invalid character.
Vue3を使ったプロジェクトで使わせてもらっています。
UNICODEからSJISへの変換がうまくいかず、なぜかASCIIに変換されてしまっています。
もしかしたらVue3に対応していないのかと思い、issue投げさせてもらいました。
コード例
// composables/useFileControl.js
import Encoding from "encoding-japanese";
let unicodeArray = Encoding.stringToCode(csvData);
console.log(Encoding.detect(unicodeArray)); // UNICODE
let sjisArray = Encoding.convert(unicodeArray, "SJIS", "UNICODE");
console.log(Encoding.detect(sjisArray)); // ASCII
const EncodingJapanese = require('encoding-japanese');
console.log(EncodingJapanese.detect('chà')); // output: UNICODE
console.log(EncodingJapanese.detect('chào')); // output: SJIS
It seems like a bug, it's not SJIS
ES6から使えるようになったModuleの機能で使えるようにしてほしいです。
Dear Mr.
Could you please add the LICENSE file? I didn't see it.
Thanks!
When I used below codes, data was not converted correctly. Output was not displayed as Japanese, but strange characters.
コンバートのため下記のソースコードを使ったが、出力のデータは日本語で表示せずに変な文字で表示ました。
「観é�³ Kå�,ç�·,ç��å·� Yå�,女,」
var file = fileUploader.files[0];
var reader = new FileReader();
reader.readAsBinaryString(file);
reader.onload = function() {
console.log(originalText);
var inputArray = str2Array(originalText); //sjis
var outputArray = Encoding.convert(inputArray, 'UTF8', 'SJIS');
var result = Encoding.codeToString(outputArray);
console.log(result);
}
function str2Array(str) {
var array = [],i,il=str.length;
for(i=0;i<il;i++) array.push(str.charCodeAt(i));
return array;
}
I am getting difficulties while using const unicodeArray = Encoding.stringToCode(characters);
My environment:
Node version: v18.8.0
System: macOS Monterey
Error:
.../node_modules/encoding-japanese/src/util.js:154
code[i] = string.charCodeAt(i);
^
TypeError: string.charCodeAt is not a function
characters variable is UTF16 (for example):
肄㮚쨀艒ࠃ隫⢖즯際ᄹ丿௧僚痐ݮᴚﶈ뚳Ꝥ%00肸ꬖ겶Œ쉔⑊渾�ʜ꾐齏ⷄ霯ᛙ淨䫼拙䱇脋喳躰썗⭲蕒愝㾻ȉ�䴖跔㇆폝⻬ᕴ�퍎⍴偬릎殿婸φ叙疂ȏ덩셯幆下wᾷꭴ尕㢛匈夁ʁ步℘ꑻ瘼碞遗潪▅픰ꢘ銪㪺╷襞掣䤬믳贀
title is self explanatory :)
had the case with a little 4 lines extracted from Excel as CSV from a Windows computer
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.