message-accumulator
Advanced tools
Comparing version 1.1.0 to 2.0.0
@@ -19,3 +19,4 @@ 'use strict';Object.defineProperty(exports,'__esModule',{value:true});var _createClass=function(){function defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if('value'in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor)}}return function(Constructor,protoProps,staticProps){if(protoProps)defineProperties(Constructor.prototype,protoProps);if(staticProps)defineProperties(Constructor,staticProps);return Constructor}}();/** | ||
* limitations under the License. | ||
*/var _ilibTreeNode=require('ilib-tree-node');var _ilibTreeNode2=_interopRequireDefault(_ilibTreeNode);function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError('Cannot call a class as a function')}}function clone(obj){return obj?Object.assign({},obj):{}}/** | ||
*/var _ilibTreeNode=require('ilib-tree-node');var _ilibTreeNode2=_interopRequireDefault(_ilibTreeNode);function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError('Cannot call a class as a function')}}// take care of all Unicode whitespace as well as what JS thinks is whitespace | ||
var whiteSpaceStart=/^[\t-\r \xA0\u1680\u2000-\u200D\u2028\u2029\u202F\u205F\u2060\u3000\uFEFF]+/;var whiteSpaceEnd=/[\t-\r \xA0\u1680\u2000-\u200D\u2028\u2029\u202F\u205F\u2060\u3000\uFEFF]+$/;var whiteSpace=/[\t-\r \xA0\u1680\u2000-\u200D\u2028\u2029\u202F\u205F\u2060\u3000\uFEFF]+/g;/** | ||
* MessageAccumulator.js - accumulate a translatable message as a string | ||
@@ -51,3 +52,3 @@ */var MessageAccumulator=function(){/** | ||
* be a node in an AST from parsing the original text. | ||
*/},{key:'push',value:function push(extra){var newNode=new _ilibTreeNode2.default({type:'component',parent:this.currentLevel,index:this.componentIndex++,extra:extra});this.currentLevel.add(newNode);this.currentLevel=newNode;this.mapping['c'+newNode.index]=extra}/** | ||
*/},{key:'push',value:function push(extra){var newNode=new _ilibTreeNode2.default({type:'component',parent:this.currentLevel,index:this.componentIndex++,extra:extra,closed:false});this.currentLevel.add(newNode);this.currentLevel=newNode;this.mapping['c'+newNode.index]=extra}/** | ||
* Pop the current context from the stack and return to the previous | ||
@@ -61,3 +62,3 @@ * context. If the current context is already the root, then this | ||
console.log('Unbalanced component error...');// eslint-disable-line no-console | ||
return}var extra=this.currentLevel.extra;this.currentLevel=this.currentLevel.parent;return extra}/** | ||
return}var extra=this.currentLevel.extra;this.currentLevel.closed=true;this.currentLevel=this.currentLevel.parent;return extra}/** | ||
* @private | ||
@@ -67,12 +68,12 @@ */},{key:'_getString',value:function _getString(rootnode){if(rootnode.children.length===0){return rootnode.value||''}return rootnode.children.map(function(child){return child.toArray().map(function(node){if(node.type==='component'){if(node.index>-1){if(node.use==='start'){return'<c'+node.index+'>'}else if(node.use==='end'){return'</c'+node.index+'>'}else{// self-closing | ||
* @private | ||
*/},{key:'_isEmpty',value:function _isEmpty(node){var _this=this;if(node.type==='text'&&node.value.trim()!=='')return false;if(node.type==='component'&&node.children&&node.children.length){return node.children.every(function(child){return _this._isEmpty(child)})}return true}/** | ||
*/},{key:'_isEmpty',value:function _isEmpty(node){var _this=this;whiteSpace.lastIndex=0;if(node.type==='text'&&node.value.replace(whiteSpace,'')!=='')return false;if(node.type==='component'&&node.children&&node.children.length){return node.children.every(function(child){return _this._isEmpty(child)})}return true}/** | ||
* @private | ||
*/},{key:'_renumber',value:function _renumber(node){var _this2=this;if(node.type==='component'){node.index=this.componentIndex++;this.mapping['c'+node.index]=node.extra}if(node.children){node.children.forEach(function(child){_this2._renumber(child)})}}/** | ||
* @private | ||
*/},{key:'_minimize',value:function _minimize(){if(this.minimized)return;var value,changed=true;if(!this.prefixes)this.prefixes=[];if(!this.suffixes)this.suffixes=[];function valueMap(node){if(node.type==='component'){var value=clone(node.extra);value.use=node.use;return value}return node.value}// keep stripping off parts until we haven't changed anything, or we have stripped off everything | ||
*/},{key:'_minimize',value:function _minimize(){if(this.minimized)return;var value,changed=true;if(!this.prefixes)this.prefixes=[];if(!this.suffixes)this.suffixes=[];// keep stripping off parts until we haven't changed anything, or we have stripped off everything | ||
while(changed&&this.root.children&&this.root.children.length){changed=false;var subroot=this.root;// check for "outer" components -- components that surround localizable text without adding anything to it | ||
while(subroot.children&&subroot.children.length===1&&subroot.children[0].type!=='text'){subroot=subroot.children[0];value=clone(subroot.extra);value.use='start';this.prefixes.push(value);value=clone(subroot.extra);value.use='end';this.suffixes=[value].concat(this.suffixes);changed=true}var children=subroot.children;// find empty components at the start | ||
var i=0;while(i<children.length&&children[i]&&this._isEmpty(children[i])){this.prefixes=this.prefixes.concat(children[i].toArray().map(valueMap));i++;changed=true}children=i>0?children.slice(i):children;// then find empty components at the end | ||
var i=children.length-1;while(i>0&&children[i]&&this._isEmpty(children[i])){this.suffixes=children[i].toArray().map(valueMap).concat(this.suffixes);i--;changed=true}// now strip off the leading and trailing whitespace | ||
if(children.length&&children[0].type==='text'){var re=/^\s+/;var match=re.exec(children[0].value);if(match){children[0].value=children[0].value.substring(match[0].length);this.prefixes.push(match[0]);changed=true}}var last=children.length-1;if(children.length&&children[last].type==='text'){var re=/\s+$/;var match=re.exec(children[last].value);if(match){children[last].value=children[last].value.substring(0,children[last].value.length-match[0].length);this.suffixes=[match[0]].concat(this.suffixes);changed=true}}this.root.children=i<children.length-1?children.slice(0,i+1):children;// then do it all again until nothing changes! | ||
while(subroot.children&&subroot.children.length===1&&subroot.children[0].type!=='text'){subroot=subroot.children[0];value=new _ilibTreeNode2.default(subroot);value.use='start';this.prefixes.push(value);value=new _ilibTreeNode2.default(subroot);value.use='end';this.suffixes=[value].concat(this.suffixes);changed=true}var children=subroot.children;// find empty components at the start | ||
var i=0;while(i<children.length&&children[i]&&this._isEmpty(children[i])){this.prefixes=this.prefixes.concat(children[i].toArray());i++;changed=true}children=i>0?children.slice(i):children;// then find empty components at the end | ||
var i=children.length-1;while(i>0&&children[i]&&this._isEmpty(children[i])){this.suffixes=children[i].toArray().concat(this.suffixes);i--;changed=true}// now strip off the leading and trailing whitespace | ||
if(children.length&&children[0].type==='text'){whiteSpaceStart.lastIndex=0;var match=whiteSpaceStart.exec(children[0].value);if(match){children[0].value=children[0].value.substring(match[0].length);this.prefixes.push(new _ilibTreeNode2.default({type:'text',value:match[0]}));changed=true}}var last=children.length-1;if(children.length&&children[last].type==='text'){whiteSpaceEnd.lastIndex=0;var match=whiteSpaceEnd.exec(children[last].value);if(match){children[last].value=children[last].value.substring(0,children[last].value.length-match[0].length);this.suffixes=[new _ilibTreeNode2.default({type:'text',value:match[0]})].concat(this.suffixes);changed=true}}this.root.children=i<children.length-1?children.slice(0,i+1):children;// then do it all again until nothing changes! | ||
}// now walk the tree again and renumber any components so that we don't start at some number greater | ||
@@ -134,3 +135,3 @@ // than zero | ||
* @return {number} the length of the non-whitespace text accumulated so far | ||
*/},{key:'getTextLength',value:function getTextLength(){return this.text.replace(/\s+/g,'').trim().length}/** | ||
*/},{key:'getTextLength',value:function getTextLength(){whiteSpace.lastIndex=0;return this.text.replace(whiteSpace,'').trim().length}/** | ||
* @private | ||
@@ -137,0 +138,0 @@ * Return the depth of the stack from the given node. |
@@ -23,5 +23,6 @@ /** | ||
function clone(obj) { | ||
return obj ? Object.assign({}, obj) : {}; | ||
} | ||
// take care of all Unicode whitespace as well as what JS thinks is whitespace | ||
var whiteSpaceStart = /^[\s\u2000-\u200D\u2028\u2029\u202F\u205F\u2060]+/u; | ||
var whiteSpaceEnd = /[\s\u2000-\u200D\u2028\u2029\u202F\u205F\u2060]+$/u; | ||
var whiteSpace = /[\s\u2000-\u200D\u2028\u2029\u202F\u205F\u2060]+/ug; | ||
@@ -148,3 +149,5 @@ /** | ||
index: this.componentIndex++, | ||
extra | ||
extra, | ||
closed: false | ||
}); | ||
@@ -171,2 +174,3 @@ this.currentLevel.add(newNode); | ||
var extra = this.currentLevel.extra; | ||
this.currentLevel.closed = true; | ||
this.currentLevel = this.currentLevel.parent; | ||
@@ -207,3 +211,4 @@ return extra; | ||
_isEmpty(node) { | ||
if (node.type === "text" && node.value.trim() !== "") return false; | ||
whiteSpace.lastIndex = 0; | ||
if (node.type === "text" && node.value.replace(whiteSpace, '') !== "") return false; | ||
if (node.type === "component" && node.children && node.children.length) { | ||
@@ -243,11 +248,2 @@ return node.children.every(child => { | ||
function valueMap(node) { | ||
if (node.type === "component") { | ||
var value = clone(node.extra); | ||
value.use = node.use; | ||
return value; | ||
} | ||
return node.value; | ||
} | ||
// keep stripping off parts until we haven't changed anything, or we have stripped off everything | ||
@@ -260,6 +256,6 @@ while (changed && this.root.children && this.root.children.length) { | ||
subroot = subroot.children[0]; | ||
value = clone(subroot.extra); | ||
value = new Node(subroot); | ||
value.use = "start"; | ||
this.prefixes.push(value); | ||
value = clone(subroot.extra); | ||
value = new Node(subroot); | ||
value.use = "end"; | ||
@@ -276,3 +272,3 @@ this.suffixes = [value].concat(this.suffixes); | ||
while (i < children.length && children[i] && this._isEmpty(children[i])) { | ||
this.prefixes = this.prefixes.concat(children[i].toArray().map(valueMap)); | ||
this.prefixes = this.prefixes.concat(children[i].toArray()); | ||
i++; | ||
@@ -287,3 +283,3 @@ changed = true; | ||
while (i > 0 && children[i] && this._isEmpty(children[i])) { | ||
this.suffixes = children[i].toArray().map(valueMap).concat(this.suffixes); | ||
this.suffixes = children[i].toArray().concat(this.suffixes); | ||
i--; | ||
@@ -295,7 +291,10 @@ changed = true; | ||
if (children.length && children[0].type === "text") { | ||
var re = /^\s+/; | ||
var match = re.exec(children[0].value); | ||
whiteSpaceStart.lastIndex = 0; | ||
var match = whiteSpaceStart.exec(children[0].value); | ||
if (match) { | ||
children[0].value = children[0].value.substring(match[0].length); | ||
this.prefixes.push(match[0]); | ||
this.prefixes.push(new Node({ | ||
type: "text", | ||
value: match[0] | ||
})); | ||
changed = true; | ||
@@ -306,7 +305,10 @@ } | ||
if (children.length && children[last].type === "text") { | ||
var re = /\s+$/; | ||
var match = re.exec(children[last].value); | ||
whiteSpaceEnd.lastIndex = 0; | ||
var match = whiteSpaceEnd.exec(children[last].value); | ||
if (match) { | ||
children[last].value = children[last].value.substring(0, children[last].value.length - match[0].length); | ||
this.suffixes = [match[0]].concat(this.suffixes); | ||
this.suffixes = [new Node({ | ||
type: "text", | ||
value: match[0] | ||
})].concat(this.suffixes); | ||
changed = true; | ||
@@ -408,3 +410,4 @@ } | ||
getTextLength() { | ||
return this.text.replace(/\s+/g, '').trim().length; | ||
whiteSpace.lastIndex = 0; | ||
return this.text.replace(whiteSpace, '').trim().length; | ||
} | ||
@@ -411,0 +414,0 @@ |
{ | ||
"name": "message-accumulator", | ||
"version": "1.1.0", | ||
"version": "2.0.0", | ||
"main": "./message-accumulator-es5.js", | ||
@@ -5,0 +5,0 @@ "main-es6": "./message-accumulator.js", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
76557
586