[Pkg-javascript-commits] [node-entities] 20/63: simplified the code, removed unused stuff

Wolfgang Borgert debacle at moszumanska.debian.org
Mon Sep 22 08:15:45 UTC 2014


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

debacle pushed a commit to branch master
in repository node-entities.

commit dafffca32787fa08e14be4a7b01921d7157c186b
Author: Felix Böhm <me at feedic.com>
Date:   Fri Apr 19 23:33:53 2013 +0200

    simplified the code, removed unused stuff
    
    also got rid of the weird prefix + syntax used before
---
 index.js | 42 +++++++++++++++++-------------------------
 1 file changed, 17 insertions(+), 25 deletions(-)

diff --git a/index.js b/index.js
index b2a74e9..35a0f50 100644
--- a/index.js
+++ b/index.js
@@ -1,35 +1,27 @@
-var re_hex = /&#[xX][\da-fA-F]+;?/g,
-	re_strictHex = /&#x[\da-f]+;/gi,
-	re_charCode = /&#\d+;?/g,
-	re_strictCharCode = /&#\d+;/g,
-	re_notUTF8 = /[\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u02ff\u0370-\u037d\u037f-\u1fff\u200c\u200d\u2070-\u218f\u2c00-\u2fef\u3001-\ud7ff\uf900-\ufdcf\ufdf0-\ufffd]/g,
-	fromCharCode = String.fromCharCode,
-	num_func = function(num){return fromCharCode(parseInt(num.substr(2), 10));},
-	hex_func = function(hex){return fromCharCode(parseInt(hex.substr(3), 16));},
-	strictNum_func = function(num){return fromCharCode(num.slice(2, -1));},
-	strictHex_func = function(num){return fromCharCode(parseInt(num.slice(3, -1), 16));},
-	charCode_func = function(c){ return "&#" +c.charCodeAt(0) +";";};
+var re_notUTF8 = /[\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u02ff\u0370-\u037d\u037f-\u1fff\u200c\u200d\u2070-\u218f\u2c00-\u2fef\u3001-\ud7ff\uf900-\ufdcf\ufdf0-\ufffd]/g,
+	charCode_func = function(c){ return "&#" + c.charCodeAt(0) + ";";};
 
 var fetch = function(filename, inherits){
-	var obj = require("./entities/" +filename +".json");
+	var obj = require("./entities/" + filename + ".json");
 	
 	if(inherits) for(var name in inherits) obj[name] = inherits[name];
 	
 	var re = Object.keys(obj).sort().join("|").replace(/(\w+)\|\1;/g, "$1;?");
-	// add regex for hex and char codes
-	re += '|' + re_hex.source.substr(1) + '|' + re_charCode.source.substr(1);
+	
+	// also match hex and char codes
+	re += "|#[xX][0-9a-fA-F]+;?|#\\d+;?";
 
 	return {
 		func: function(name){
-			if (name.charAt(1) === '#') {
-				if (name.charAt(2).toLowerCase() === 'x') {
-					return hex_func(name);
+			if (name.charAt(1) === "#") {
+				if (name.charAt(2).toLowerCase() === "x") {
+					return String.fromCharCode(parseInt(name.substr(3), 16));
 				}
-				return num_func(name);
+				return String.fromCharCode(parseInt(name.substr(2), 10));
 			}
 			return obj[name.substr(1)];
 		},
-		re: new RegExp("&(?:" +re +")", "g"),
+		re: new RegExp("&(?:" + re + ")", "g"),
 		obj: obj
 	};
 };
@@ -41,8 +33,8 @@ var getReverse = function(obj){
 	}, {});
 	
 	return {
-		func: function(name){ return "&" +reverse[name]; },
-		re: new RegExp("\\" +Object.keys(reverse).sort().join("|\\"), "g")
+		func: function(name){ return "&" + reverse[name]; },
+		re: new RegExp("\\" + Object.keys(reverse).sort().join("|\\"), "g")
 	};
 };
 
@@ -51,11 +43,11 @@ var modes = ["XML", "HTML4", "HTML5"];
 module.exports = {
 	decode: function(data, level){
 		if(!modes[level]) level = 0;
-		return module.exports["decode" +modes[level]](data);
+		return module.exports["decode" + modes[level]](data);
 	},
 	encode: function(data, level){
 		if(!modes[level]) level = 0;
-		return module.exports["encode" +modes[level]](data);
+		return module.exports["encode" + modes[level]](data);
 	}
 };
 
@@ -68,7 +60,7 @@ modes.forEach(function(name){
 	
 	tmp = obj.obj;
 	
-	module.exports["decode" +name] = function(data){
+	module.exports["decode" + name] = function(data){
 		return data
 			.replace(regex, func);
 	};
@@ -77,7 +69,7 @@ modes.forEach(function(name){
 		reverse_re = reverse.re,
 		reverse_func = reverse.func;
 	
-	module.exports["encode" +name] = function(data){
+	module.exports["encode" + name] = function(data){
 		return data
 			.replace(reverse_re, reverse_func)
 			.replace(re_notUTF8, charCode_func);

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



More information about the Pkg-javascript-commits mailing list