Skip to content

Commit

Permalink
Binary file support (Buffer instead of string), make no separator the…
Browse files Browse the repository at this point in the history
… default
  • Loading branch information
floridoo committed Dec 28, 2014
1 parent 4063427 commit 8070259
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 23 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Concat with source maps [![NPM version][npm-image]][npm-url] [![build status][travis-image]][travis-url] [![Test coverage][coveralls-image]][coveralls-url]

NPM module for concatenating text files and generating source maps.
NPM module for concatenating files and generating source maps.

### Usage example
```js
Expand All @@ -20,21 +20,21 @@ Initialize a new concat object.
Parameters:
- generateSourceMap: whether or not to generate a source map (default: false)
- outFileName: the file name/path of the output file (for the source map)
- separator: the string that should separate files (default: \n)
- separator: the string that should separate files (default: no separator)

#### concat.add(fileName, content, sourceMap)
Add a file to the output file.

Parameters:
- fileName: file name of the input file
- content: content (string) of the input file
- sourceMap: optional source map of the input file. Will be merged into the output source map.
- content: content (Buffer or string) of the input file
- sourceMap: optional source map of the input file (string). Will be merged into the output source map.

#### concat.content
The resulting concatenated file content.
The resulting concatenated file content (Buffer).

#### concat.sourceMap
The resulting source map of the concatenated files.
The resulting source map of the concatenated files (string).

[npm-image]: https://img.shields.io/npm/v/concat-with-sourcemaps.svg?style=flat
[npm-url]: https://npmjs.org/package/concat-with-sourcemaps
Expand Down
28 changes: 18 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,25 @@ function unixStylePath(filePath) {
}

function Concat(generateSourceMap, fileName, separator) {
this.separator = separator;
this.lineOffset = 0;
this.columnOffset = 0;
this.sourceMapping = generateSourceMap;
this.content = '';
this.content = new Buffer(0);

if (separator === undefined) {
this.separator = '\n';
this.separator = new Buffer(0);
} else {
this.separator = new Buffer(separator);
}

if (this.sourceMapping) {
this._sourceMap = new SourceMapGenerator({file: unixStylePath(fileName)});
this.separatorLineOffset = 0;
this.separatorColumnOffset = 0;
for (var i = 0; i < this.separator.length; i++) {
var separatorString = this.separator.toString();
for (var i = 0; i < separatorString.length; i++) {
this.separatorColumnOffset++;
if (this.separator[i] === '\n') {
if (separatorString[i] === '\n') {
this.separatorLineOffset++;
this.separatorColumnOffset = 0;
}
Expand All @@ -33,13 +35,19 @@ function Concat(generateSourceMap, fileName, separator) {

Concat.prototype.add = function(filePath, content, sourceMap) {
filePath = unixStylePath(filePath);
if (this.content !== '') {
this.content += this.separator;

if (!Buffer.isBuffer(content)) {
content = new Buffer(content);
}

if (this.content.length !== 0) {
this.content = Buffer.concat([this.content, this.separator]);
}
this.content += content;
this.content = Buffer.concat([this.content, content]);

This comment has been minimized.

Copy link
@yocontra

yocontra Feb 19, 2015

instead of doing this every time this can be done when the final file is spit out, which would save a lot of memory and CPU. right now it is allocating a new buffer of all of the previous files + the current file every time a file comes in


if (this.sourceMapping) {
var lines = content.split('\n').length;
var contentString = content.toString();
var lines = contentString.split('\n').length;

if (Object.prototype.toString.call(sourceMap) === '[object String]')
sourceMap = JSON.parse(sourceMap);
Expand Down Expand Up @@ -90,7 +98,7 @@ Concat.prototype.add = function(filePath, content, sourceMap) {
if (lines > 1)
this.columnOffset = 0;
if (this.separatorLineOffset === 0)
this.columnOffset += content.length - Math.max(0, content.lastIndexOf('\n')+1);
this.columnOffset += contentString.length - Math.max(0, contentString.lastIndexOf('\n')+1);
this.columnOffset += this.separatorColumnOffset;
this.lineOffset += lines - 1 + this.separatorLineOffset;
}
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "concat-with-sourcemaps",
"version": "0.1.6",
"version": "1.0.0",
"description": "Concatenate file contents with a custom separator and generate a source map",
"homepage": "http://github.com/floridoo/concat-with-sourcemaps",
"repository": "git://github.com/floridoo/concat-with-sourcemaps.git",
Expand All @@ -18,13 +18,13 @@
"author": "Florian Reiterer <me@florianreiterer.com>",
"license": "ISC",
"dependencies": {
"source-map": "^0.1.39"
"source-map": "^0.1.41"
},
"devDependencies": {
"jshint": "^2.5.0",
"tape": "^3.0.2",
"jshint": "^2.5.11",
"tape": "^3.0.3",
"argg": "0.0.1",
"istanbul": "^0.3.2",
"istanbul": "^0.3.5",
"faucet": "0.0.1",
"coveralls": "^2.10.0"
}
Expand Down
16 changes: 14 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,23 @@ var Concat = require('..');

function testCase(description, options) {
test(description, function(t) {
// content as Buffer
var concat = new Concat(options.sourceMapping, options.outFile, options.separator);
options.input.forEach(function(input, i) {
concat.add(input.fileName || 'test'+(i+1), new Buffer(input.content), input.sourceMap);
});
t.equal(concat.content.toString(), options.output.content, 'should produce the right output');
if (options.output.sourceMap)
t.deepEqual(JSON.parse(concat.sourceMap), JSON.parse(options.output.sourceMap), 'should produce the right source map');
else
t.equal(concat.sourceMap, undefined, 'should not produce a source map');

// content as string
concat = new Concat(options.sourceMapping, options.outFile, options.separator);
options.input.forEach(function(input, i) {
concat.add(input.fileName || 'test'+(i+1), input.content, input.sourceMap);
});
t.equal(concat.content, options.output.content, 'should produce the right output');
t.equal(concat.content.toString(), options.output.content, 'should produce the right output');
if (options.output.sourceMap)
t.deepEqual(JSON.parse(concat.sourceMap), JSON.parse(options.output.sourceMap), 'should produce the right source map');
else
Expand Down Expand Up @@ -84,7 +96,7 @@ testCase('should concatenate without separator specified', {
{ content: 'CCC' }
],
output: {
content: 'AAA\nBBB\nCCC'
content: 'AAABBBCCC'
}
});

Expand Down

0 comments on commit 8070259

Please sign in to comment.