[Pkg-javascript-commits] [node-jsesc] 01/03: Imported Upstream version 2.2.0

Julien Puydt julien.puydt at laposte.net
Fri Jun 10 15:30:51 UTC 2016


This is an automated email from the git hooks/post-receive script.

jpuydt-guest pushed a commit to branch master
in repository node-jsesc.

commit 41b6a4c26df50c09a86b9316f800a1278192dea3
Author: Julien Puydt <julien.puydt at laposte.net>
Date:   Fri Jun 10 17:14:54 2016 +0200

    Imported Upstream version 2.2.0
---
 README.md      |  2 +-
 jsesc.js       |  7 +++++++
 package.json   |  2 +-
 src/jsesc.js   |  7 +++++++
 tests/tests.js | 18 ++++++++++++++++++
 5 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/README.md b/README.md
index 7f0057a..d86c8fa 100644
--- a/README.md
+++ b/README.md
@@ -34,7 +34,7 @@ jsesc('foo 𝌆 bar');
 // → 'foo \\uD834\\uDF06 bar'
 ```
 
-Instead of a string, the `value` can also be an array, an object, a map, or a set. In such cases, `jsesc` will return a stringified version of the value where any characters that are not printable ASCII symbols are escaped in the same way.
+Instead of a string, the `value` can also be an array, an object, a map, a set, or a buffer. In such cases, `jsesc` will return a stringified version of the value where any characters that are not printable ASCII symbols are escaped in the same way.
 
 ```js
 // Escaping an array
diff --git a/jsesc.js b/jsesc.js
index 13988de..0280ef5 100644
--- a/jsesc.js
+++ b/jsesc.js
@@ -30,6 +30,7 @@ const forEach = function(array, callback) {
 
 const toString = object.toString;
 const isArray = Array.isArray;
+const isBuffer = Buffer.isBuffer;
 const isObject = function(value) {
 	// This is a very simple check, but it’s good enough for what we need.
 	return toString.call(value) == '[object Object]';
@@ -139,6 +140,12 @@ const jsesc = function(argument, options) {
 			}
 			return 'new Set(' + jsesc(Array.from(argument), options) + ')';
 		}
+		if (isBuffer(argument)) {
+			if (argument.length == 0) {
+				return 'Buffer()';
+			}
+			return 'Buffer(' + jsesc(Array.from(argument), options) + ')';
+		}
 		if (isArray(argument)) {
 			result = [];
 			options.wrap = true;
diff --git a/package.json b/package.json
index b37bcd5..994d11e 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "jsesc",
-  "version": "2.1.0",
+  "version": "2.2.0",
   "description": "A JavaScript library for escaping JavaScript strings while generating the shortest possible valid output.",
   "homepage": "https://mths.be/jsesc",
   "engines": {
diff --git a/src/jsesc.js b/src/jsesc.js
index 1514eca..fc0d22d 100644
--- a/src/jsesc.js
+++ b/src/jsesc.js
@@ -30,6 +30,7 @@ const forEach = function(array, callback) {
 
 const toString = object.toString;
 const isArray = Array.isArray;
+const isBuffer = Buffer.isBuffer;
 const isObject = function(value) {
 	// This is a very simple check, but it’s good enough for what we need.
 	return toString.call(value) == '[object Object]';
@@ -139,6 +140,12 @@ const jsesc = function(argument, options) {
 			}
 			return 'new Set(' + jsesc(Array.from(argument), options) + ')';
 		}
+		if (isBuffer(argument)) {
+			if (argument.length == 0) {
+				return 'Buffer()';
+			}
+			return 'Buffer(' + jsesc(Array.from(argument), options) + ')';
+		}
 		if (isArray(argument)) {
 			result = [];
 			options.wrap = true;
diff --git a/tests/tests.js b/tests/tests.js
index 51153f3..c0641ae 100644
--- a/tests/tests.js
+++ b/tests/tests.js
@@ -331,6 +331,24 @@ describe('common usage', function() {
 			'new Set([\n\t[\n\t\t\'a\'\n\t],\n\t\'b\',\n\t{}\n])',
 			'Stringifying a Set with `compact: false`'
 		);
+		// Buffer
+		assert.equal(
+			jsesc(
+				Buffer([0x13, 0x37, 0x42])
+			),
+			'Buffer([19,55,66])',
+			'Stringifying a Buffer'
+		);
+		assert.equal(
+			jsesc(
+				Buffer([0x13, 0x37, 0x42]),
+				{
+					'compact': false
+				}
+			),
+			'Buffer([\n\t19,\n\t55,\n\t66\n])',
+			'Stringifying a Buffer with `compact: false`'
+		);
 		// JSON
 		assert.equal(
 			jsesc('foo\x00bar\xFF\uFFFDbaz', {

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-jsesc.git



More information about the Pkg-javascript-commits mailing list