Skip to content

Commit

Permalink
delete some unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
freedom-git committed Nov 17, 2022
1 parent 6eaab92 commit 1d6f4f0
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 94 deletions.
3 changes: 0 additions & 3 deletions dist/esc-pos-encoder.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
declare module 'iconv-lite' {
const encodings: Array<string>;
}
export declare enum PrinterWidthEnum {
'_58' = 58,
'_80' = 80
Expand Down
58 changes: 11 additions & 47 deletions dist/esc-pos-encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,61 +378,25 @@ var EscPosEncoder = /** @class */ (function () {
*/
EscPosEncoder.prototype.codepage = function (value) {
var codepages = {
'cp437': [0x00, false],
'cp737': [0x40, false],
'utf-8': [null, false],
'cp850': [0x02, false],
'cp775': [0x5f, false],
'cp852': [0x12, false],
'cp855': [0x3c, false],
'cp857': [0x3d, false],
'cp858': [0x13, false],
'cp860': [0x03, false],
'cp861': [0x38, false],
'cp862': [0x3e, false],
'cp863': [0x04, false],
'cp864': [0x1c, false],
'cp865': [0x05, false],
'cp866': [0x11, false],
'cp869': [0x42, false],
'cp936': [0xff, true],
'cp949': [0xfd, true],
'cp950': [0xfe, true],
'cp1252': [0x10, false],
'iso88596': [0x16, false],
'shiftjis': [0xfc, true],
'windows874': [0x1e, false],
'windows1250': [0x48, false],
'windows1251': [0x49, false],
'windows1252': [0x47, false],
'windows1253': [0x5a, false],
'windows1254': [0x5b, false],
'windows1255': [0x20, false],
'windows1256': [0x5c, false],
'windows1257': [0x19, false],
'windows1258': [0x5e, false],
'tcvn': [0x1c, false],
'cp949': [null, true],
'cp936': [null, true],
'cp1258': [null, false],
};
var codepage;
if (!iconv.encodingExists(value)) {
throw new Error('Unknown codepage');
}
if (value in iconv.encodings) {
if (typeof iconv.encodings[value] === 'string') {
codepage = iconv.encodings[value];
}
else {
codepage = value;
}
}
else {
throw new Error('Unknown codepage');
}
var codepage = value;
if (typeof codepages[codepage] !== 'undefined') {
this._codepage = codepage;
this._state.hanzi = codepages[codepage][1];
this._queue([
0x1b, 0x74, codepages[codepage][0],
]);
// 大部分打印机的codepage顺序表都是不一样的, 只有比较确定的这里开启自动设置, 不太确定的这里跳过设置, 要让用户自己用工具设置打印机
if (codepages[codepage][0] !== null) {
this._queue([
0x1b, 0x74, codepages[codepage][0],
]);
}
}
else {
throw new Error('Codepage not supported by printer');
Expand Down
48 changes: 17 additions & 31 deletions src/esc-pos-encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ import * as Dither from 'canvas-dither';
import * as Flatten from 'canvas-flatten';
import * as QRCode from 'qrcode';

declare module 'iconv-lite' {
export const encodings: Array<string>;
}

interface PrinterParam {
width: number;
singleCharLength: number;
Expand Down Expand Up @@ -54,7 +50,7 @@ export default class EscPosEncoder {
* Create a new EscPosEncoder
*
*/
constructor() {
constructor() {
this._reset();
}

Expand Down Expand Up @@ -407,34 +403,24 @@ export default class EscPosEncoder {
// 'windows1258': [0x5e, false],
};

let codepage;
if (!iconv.encodingExists(value)) {
throw new Error('Unknown codepage');
}

if (!iconv.encodingExists(value)) {
throw new Error('Unknown codepage');
}

if (value in iconv.encodings) {
if (typeof iconv.encodings[value] === 'string') {
codepage = iconv.encodings[value];
} else {
codepage = value;
}
} else {
throw new Error('Unknown codepage');
const codepage = value;

if (typeof codepages[codepage] !== 'undefined') {
this._codepage = codepage;
this._state.hanzi = codepages[codepage][1];

// 大部分打印机的codepage顺序表都是不一样的, 只有比较确定的这里开启自动设置, 不太确定的这里跳过设置, 要让用户自己用工具设置打印机
if (codepages[codepage][0]!==null) {
this._queue([
0x1b, 0x74, codepages[codepage][0],
]);
}

if (typeof codepages[codepage] !== 'undefined') {
this._codepage = codepage;
this._state.hanzi = codepages[codepage][1];

// 大部分打印机的codepage顺序表都是不一样的, 只有比较确定的这里开启自动设置, 不太确定的这里跳过设置, 要让用户自己用工具设置打印机
if(codepages[codepage][0]!==null) {
this._queue([
0x1b, 0x74, codepages[codepage][0],
]);
}
} else {
throw new Error('Codepage not supported by printer');
} else {
throw new Error('Codepage not supported by printer');
}

return this;
Expand Down
26 changes: 13 additions & 13 deletions test/esc-pos-encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,29 @@ describe('EscPosEncoder', function() {
});
});

describe('codepage(cp437).text(héllo) - é -> 130', function() {
const result = encoder.codepage('cp437').text('héllo').encode();
// describe('codepage(cp437).text(héllo) - é -> 130', function() {
// const result = encoder.codepage('cp437').text('héllo').encode();

it('should be [ 27, 116, 0, 104, 130, 108, 108, 111 ]', function() {
assert.deepEqual(new Uint8Array([27, 116, 0, 104, 130, 108, 108, 111]), result);
});
});
// it('should be [ 27, 116, 0, 104, 130, 108, 108, 111 ]', function() {
// assert.deepEqual(new Uint8Array([27, 116, 0, 104, 130, 108, 108, 111]), result);
// });
// });

describe('codepage(cp936).text(简体中文) - simplified chinese', function() {
const result = encoder.codepage('cp936').text('简体中文').encode();

it('should be [ 27, 116, 255, 28, 38, 188, 242, 204, 229, 214, 208, 206, 196, 28, 46 ]', function() {
assert.deepEqual(new Uint8Array([27, 116, 255, 28, 38, 188, 242, 204, 229, 214, 208, 206, 196, 28, 46]), result);
assert.deepEqual(new Uint8Array([28, 38, 188, 242, 204, 229, 214, 208, 206, 196, 28, 46]), result);
});
});

describe('codepage(win1252).text(héllo) - é -> 233', function() {
const result = encoder.codepage('win1252').text('héllo').encode();
// describe('codepage(win1252).text(héllo) - é -> 233', function() {
// const result = encoder.codepage('win1252').text('héllo').encode();

it('should be [ 27, 116, 71, 104, 233, 108, 108, 111 ]', function() {
assert.deepEqual(new Uint8Array([27, 116, 71, 104, 233, 108, 108, 111]), result);
});
});
// it('should be [ 27, 116, 71, 104, 233, 108, 108, 111 ]', function() {
// assert.deepEqual(new Uint8Array([27, 116, 71, 104, 233, 108, 108, 111]), result);
// });
// });

describe('codepage(utf8).text(héllo)', function() {
it('should throw an "Codepage not supported by printer" error', function() {
Expand Down

0 comments on commit 1d6f4f0

Please sign in to comment.