diff --git a/src/index.test.ts b/src/index.test.ts index d1eba76..8dbfda9 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -40,6 +40,11 @@ describe('chardet', () => { const res = await chardet.detectFile(path, { sampleSize: 32 }); expect(res).toBe('UTF-8'); }); + + it('should detect encoding with smaller sample size and position offset', async () => { + const res = await chardet.detectFile(path, { sampleSize: 32, position: 64 }); + expect(res).toBe('UTF-8'); + }); }); describe('#detectFileSync', () => { @@ -50,6 +55,10 @@ describe('chardet', () => { it('should detect encoding with smaller sample size', () => { expect(chardet.detectFileSync(path, { sampleSize: 32 })).toBe('UTF-8'); }); + + it('should detect encoding with smaller sample size and position offset', () => { + expect(chardet.detectFileSync(path, { sampleSize: 32, position: 64 })).toBe('UTF-8'); + }); }); describe('#analyse', () => { diff --git a/src/index.ts b/src/index.ts index 2030222..d6e5fc7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,7 +10,8 @@ import * as sbcs from './encoding/sbcs'; import * as iso2022 from './encoding/iso2022'; interface FullOptions { - sampleSize: number + sampleSize: number, + position: number } type Options = Partial @@ -107,7 +108,7 @@ export const detectFile = (filepath: string, opts: Options = {}): Promise { + fs.read(fd, sample, 0, opts.sampleSize, opts.position, (err?: Error) => { handler(err, sample); }); return; @@ -123,7 +124,7 @@ export const detectFileSync = (filepath: string, opts: Options = {}): DetectResu const fd = fs.openSync(filepath, 'r'); const sample = Buffer.allocUnsafe(opts.sampleSize); - fs.readSync(fd, sample, 0, opts.sampleSize); + fs.readSync(fd, sample, 0, opts.sampleSize, opts.position); fs.closeSync(fd); return detect(sample); }