Skip to content

Commit

Permalink
Merge pull request #42 from AndyBitz/support-for-binary-files
Browse files Browse the repository at this point in the history
Support for binary files
  • Loading branch information
sokra authored Oct 23, 2018
2 parents 0f99964 + a366b80 commit 1f11ae4
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ All methods should be considered as expensive as they may need to do computation
#### `source`

``` js
Source.prototype.source() -> String
Source.prototype.source() -> String | ArrayBuffer
```

Returns the represented source code as string.
Expand Down
6 changes: 4 additions & 2 deletions lib/CachedSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ class CachedSource extends Source {

size() {
if(typeof this._cachedSize !== "undefined") return this._cachedSize;
if(typeof this._cachedSource !== "undefined")
return this._cachedSize = this._cachedSource.length;
if(typeof this._cachedSource !== "undefined") {
if(Buffer.from.length === 1) return new Buffer(this._cachedSource).length;
return this._cachedSize = Buffer.byteLength(this._cachedSource);
}
return this._cachedSize = this._source.size();
}

Expand Down
3 changes: 2 additions & 1 deletion lib/Source.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class Source {
}

size() {
return this.source().length;
if(Buffer.from.length === 1) return new Buffer(this.source()).length;
return Buffer.byteLength(this.source())
}

map(options) {
Expand Down
56 changes: 56 additions & 0 deletions test/CachedSource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
var should = require("should");
var CachedSource = require("../lib/CachedSource");
var OriginalSource = require('../lib/OriginalSource');

describe("CachedSource", function() {
it("should return the correct size for binary files", function() {
var source = new OriginalSource(new ArrayBuffer(256), "file.wasm");
var cachedSource = new CachedSource(source);

cachedSource.size().should.be.eql(256);
cachedSource.size().should.be.eql(256);
});

it("should return the correct size for cached binary sources", function() {
var source = new OriginalSource(new ArrayBuffer(256), "file.wasm");
var cachedSource = new CachedSource(source);

cachedSource.source();
cachedSource.size().should.be.eql(256);
cachedSource.size().should.be.eql(256);
});

it("should return the correct size for text files", function() {
var source = new OriginalSource("TestTestTest", "file.js");
var cachedSource = new CachedSource(source);

cachedSource.size().should.be.eql(12);
cachedSource.size().should.be.eql(12);
});

it("should return the correct size for cached text files", function() {
var source = new OriginalSource("TestTestTest", "file.js");
var cachedSource = new CachedSource(source);

cachedSource.source();
cachedSource.size().should.be.eql(12);
cachedSource.size().should.be.eql(12);
});

it("should return the correct size for unicode files", function() {
var source = new OriginalSource("😋", "file.js");
var cachedSource = new CachedSource(source);

cachedSource.size().should.be.eql(4);
cachedSource.size().should.be.eql(4);
});

it("should return the correct size for cached unicode files", function() {
var source = new OriginalSource("😋", "file.js");
var cachedSource = new CachedSource(source);

cachedSource.source();
cachedSource.size().should.be.eql(4);
cachedSource.size().should.be.eql(4);
});
});
10 changes: 10 additions & 0 deletions test/OriginalSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,14 @@ describe("OriginalSource", function() {

resultMap.mappings.should.be.eql("AAAA;AACA;AACA");
});

it("should return the correct size for binary files", function() {
var source = new OriginalSource(new ArrayBuffer(256), "file.wasm");
source.size().should.be.eql(256);
});

it("should return the correct size for unicode files", function() {
var source = new OriginalSource("😋", "file.js");
source.size().should.be.eql(4);
});
});

0 comments on commit 1f11ae4

Please sign in to comment.