From 80702596b24bba7b86a89a3d7e02b933969de970 Mon Sep 17 00:00:00 2001 From: Florian Reiterer Date: Sun, 28 Dec 2014 15:38:57 +0100 Subject: [PATCH] Binary file support (Buffer instead of string), make no separator the default --- README.md | 12 ++++++------ index.js | 28 ++++++++++++++++++---------- package.json | 10 +++++----- test/index.js | 16 ++++++++++++++-- 4 files changed, 43 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 30f63e9..dc2a61f 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/index.js b/index.js index 74e2e39..8507f7a 100644 --- a/index.js +++ b/index.js @@ -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; } @@ -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]); 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); @@ -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; } diff --git a/package.json b/package.json index 3e7d1b7..9fd3dcd 100644 --- a/package.json +++ b/package.json @@ -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", @@ -18,13 +18,13 @@ "author": "Florian Reiterer ", "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" } diff --git a/test/index.js b/test/index.js index 01db578..9609b88 100644 --- a/test/index.js +++ b/test/index.js @@ -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 @@ -84,7 +96,7 @@ testCase('should concatenate without separator specified', { { content: 'CCC' } ], output: { - content: 'AAA\nBBB\nCCC' + content: 'AAABBBCCC' } });