message-accumulator
Advanced tools
Comparing version 2.2.0 to 2.2.1
@@ -1,20 +0,2 @@ | ||
'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}}();/** | ||
* message-accumulator.js - accumulate localizable messages | ||
* | ||
* @license | ||
* Copyright © 2019, JEDLSoft | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* | ||
* See the License for the specific language governing permissions and | ||
* 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')}}// take care of all Unicode whitespace as well as what JS thinks is whitespace | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports["default"]=void 0;var _ilibTreeNode=_interopRequireDefault(require("ilib-tree-node"));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 _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)}}function _createClass(Constructor,protoProps,staticProps){if(protoProps)_defineProperties(Constructor.prototype,protoProps);if(staticProps)_defineProperties(Constructor,staticProps);return Constructor}// 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;// use [\s\S]*? instead of .* with the "s" flag because node 6 and earlier throw errors about the unknown "s" flag | ||
@@ -24,5 +6,5 @@ var re=/(<(c\d+)>[\s\S]*?<\/\2>)/g;var first=/^<c(\d+)>/;var selfclosing=/(<([cp](\d+))\/>)/g;// "c" for component, "p" for parameter | ||
* MessageAccumulator.js - accumulate a translatable message as a string | ||
*/var MessageAccumulator=function(){/** | ||
*/var MessageAccumulator=/*#__PURE__*/function(){/** | ||
* Create a new accumulator instance. | ||
*/function MessageAccumulator(){_classCallCheck(this,MessageAccumulator);this.root=new _ilibTreeNode2.default({type:'root',parent:null,index:-1});this.currentLevel=this.root;this.componentIndex=0;this.paramIndex=0;this.text='';this.mapping={}}/** | ||
*/function MessageAccumulator(){_classCallCheck(this,MessageAccumulator);this.root=new _ilibTreeNode["default"]({type:"root",parent:null,index:-1});this.currentLevel=this.root;this.componentIndex=0;this.paramIndex=0;this.text="";this.mapping={}}/** | ||
* Factory method to create a new MessageAccumulator instance from | ||
@@ -40,12 +22,12 @@ * the given string and a source message accumulator. This will | ||
* instance equivalent to the given string | ||
*/_createClass(MessageAccumulator,[{key:'_parse',/** | ||
*/_createClass(MessageAccumulator,[{key:"_parse",/** | ||
* @private | ||
*/value:function _parse(string,mapping,parent){var match=void 0;re.lastIndex=0;var parts=string.split(re);for(var i=0;i<parts.length;i++){first.lastIndex=0;selfclosing.lastIndex=0;if((match=first.exec(parts[i]))!==null){var index=parseInt(match[1]);var len=match[0].length;// strip off the outer tags before processing the stuff in the middle | ||
var substr=parts[i].substring(len,parts[i].length-len-1);var component=new _ilibTreeNode2.default({type:'component',parent:parent,index:index,extra:mapping&&mapping['c'+index]});this._parse(substr,mapping,component);parent.add(component);i++;// skip the number in the next iteration | ||
}else if(parts[i]&&parts[i].length){var subparts=parts[i].split(selfclosing);for(var j=0;j<subparts.length;j++){selfclosing.lastIndex=0;if((match=selfclosing.exec(subparts[j]))!==null){var _index=parseInt(match[3]);parent.add(new _ilibTreeNode2.default({type:match[2][0]==='p'?'param':'component',parent:parent,index:_index,extra:mapping&&mapping[match[2]]}));j+=2;// skip the other parts of the match in the next iteration | ||
*/value:function _parse(string,mapping,parent){var match;re.lastIndex=0;var parts=string.split(re);for(var i=0;i<parts.length;i++){first.lastIndex=0;selfclosing.lastIndex=0;if((match=first.exec(parts[i]))!==null){var index=parseInt(match[1]);var len=match[0].length;// strip off the outer tags before processing the stuff in the middle | ||
var substr=parts[i].substring(len,parts[i].length-len-1);var component=new _ilibTreeNode["default"]({type:"component",parent:parent,index:index,extra:mapping&&mapping["c".concat(index)]});this._parse(substr,mapping,component);parent.add(component);i++;// skip the number in the next iteration | ||
}else if(parts[i]&&parts[i].length){var subparts=parts[i].split(selfclosing);for(var j=0;j<subparts.length;j++){selfclosing.lastIndex=0;if((match=selfclosing.exec(subparts[j]))!==null){var _index=parseInt(match[3]);parent.add(new _ilibTreeNode["default"]({type:match[2][0]==="p"?"param":"component",parent:parent,index:_index,extra:mapping&&mapping[match[2]]}));j+=2;// skip the other parts of the match in the next iteration | ||
}else if(subparts[j]&&subparts[j].length){// don't store empty strings | ||
parent.add(new _ilibTreeNode2.default({type:'text',value:subparts[j]}))}}}}}/** | ||
parent.add(new _ilibTreeNode["default"]({type:"text",value:subparts[j]}))}}}}}/** | ||
* Add text to the current context of the string. | ||
* @param {string} text the text to add | ||
*/},{key:'addText',value:function addText(text){if(typeof text==='string'){this.currentLevel.add(new _ilibTreeNode2.default({type:'text',value:text}))}this.text+=text}/** | ||
*/},{key:"addText",value:function addText(text){if(typeof text==="string"){this.currentLevel.add(new _ilibTreeNode["default"]({type:"text",value:text}))}this.text+=text}/** | ||
* Add a replacement parameter to the string. This is a coding | ||
@@ -83,3 +65,3 @@ * for a replacement parameter in the programming language | ||
* use to identify the original replacement parameter | ||
*/},{key:'addParam',value:function addParam(extra){var index=this.paramIndex++;this.currentLevel.add(new _ilibTreeNode2.default({type:'param',index:index,extra:extra,closed:true}));var contents='p'+index;this.text+='<'+contents+'/>';this.mapping[contents]=extra}/** | ||
*/},{key:"addParam",value:function addParam(extra){var index=this.paramIndex++;this.currentLevel.add(new _ilibTreeNode["default"]({type:"param",index:index,extra:extra,closed:true}));var contents="p".concat(index);this.text+="<".concat(contents,"/>");this.mapping[contents]=extra}/** | ||
* Create a new subcontext for a component such that all text | ||
@@ -101,3 +83,3 @@ * added to the accumulator goes into the new context.<p> | ||
* not optimized out during the getMinimalString | ||
*/},{key:'push',value:function push(extra,keep){var newNode=new _ilibTreeNode2.default({type:'component',parent:this.currentLevel,index:this.componentIndex++,extra:extra,closed:false,keep:keep});this.currentLevel.add(newNode);this.currentLevel=newNode;this.mapping['c'+newNode.index]=extra}/** | ||
*/},{key:"push",value:function push(extra,keep){var newNode=new _ilibTreeNode["default"]({type:"component",parent:this.currentLevel,index:this.componentIndex++,extra:extra,closed:false,keep:keep});this.currentLevel.add(newNode);this.currentLevel=newNode;this.mapping["c".concat(newNode.index)]=extra}/** | ||
* Pop the current context from the stack and return to the previous | ||
@@ -109,20 +91,20 @@ * context. If the current context is already the root, then this | ||
* root and there is nothing to pop | ||
*/},{key:'pop',value:function pop(){if(!this.currentLevel.parent){// oh oh, unbalanced? | ||
console.log('Unbalanced component error...');// eslint-disable-line no-console | ||
*/},{key:"pop",value:function pop(){if(!this.currentLevel.parent){// oh oh, unbalanced? | ||
console.log("Unbalanced component error...");// eslint-disable-line no-console | ||
return}var extra=this.currentLevel.extra;this.currentLevel.closed=true;this.currentLevel=this.currentLevel.parent;return extra}/** | ||
* @private | ||
*/},{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 | ||
return'<c'+node.index+'/>'}}}else if(node.type==='param'){// self-closing | ||
return'<p'+node.index+'/>'}else{return node.value}}).join('')}).join('')}/** | ||
*/},{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".concat(node.index,">")}else if(node.use==="end"){return"</c".concat(node.index,">")}else{// self-closing | ||
return"<c".concat(node.index,"/>")}}}else if(node.type==="param"){// self-closing | ||
return"<p".concat(node.index,"/>")}else{return node.value}}).join("")}).join("")}/** | ||
* @private | ||
*/},{key:'_isEmpty',value:function _isEmpty(node){var _this=this;whiteSpace.lastIndex=0;if(node.type==='param')return false;if(node.type==='text'&&node.value.replace(whiteSpace,'')!=='')return false;if(node.type==='component'){if(node.keep)return false;if(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==="param")return false;if(node.type==="text"&&node.value.replace(whiteSpace,"")!=="")return false;if(node.type==="component"){if(node.keep)return false;if(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}else if(node.type==='param'){this.mapping['p'+node.index]=node.extra}if(node.children){node.children.forEach(function(child){_this2._renumber(child)})}}/** | ||
*/},{key:"_renumber",value:function _renumber(node){var _this2=this;if(node.type==="component"){node.index=this.componentIndex++;this.mapping["c".concat(node.index)]=node.extra}else if(node.type==="param"){this.mapping["p".concat(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=[];// 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.children[0].keep){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 | ||
while(subroot.children&&subroot.children.length===1&&subroot.children[0].type!=="text"&&!subroot.children[0].keep){subroot=subroot.children[0];value=new _ilibTreeNode["default"](subroot);value.use="start";this.prefixes.push(value);value=new _ilibTreeNode["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! | ||
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 _ilibTreeNode["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 _ilibTreeNode["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 | ||
@@ -136,3 +118,3 @@ // than zero | ||
* @return {string} the accumulated string so far | ||
*/},{key:'getString',value:function getString(){return this._getString(this.root)}/** | ||
*/},{key:"getString",value:function getString(){return this._getString(this.root)}/** | ||
* Return all of the irrelevant parts of the string at the beginning | ||
@@ -147,3 +129,3 @@ * of the message.<p> | ||
* are irrelevant | ||
*/},{key:'getPrefix',value:function getPrefix(){this._minimize();return this.prefixes||[]}/** | ||
*/},{key:"getPrefix",value:function getPrefix(){this._minimize();return this.prefixes||[]}/** | ||
* Return the message accumulated so far as a string, including | ||
@@ -171,3 +153,3 @@ * any components, and leaving out any contexts that are irrelevant | ||
* components removed. | ||
*/},{key:'getMinimalString',value:function getMinimalString(){this._minimize();return this._getString(this.root)}/** | ||
*/},{key:"getMinimalString",value:function getMinimalString(){this._minimize();return this._getString(this.root)}/** | ||
* Return all of the irrelevant parts of the string at the end | ||
@@ -182,3 +164,3 @@ * of the message.<p> | ||
* are irrelevant | ||
*/},{key:'getSuffix',value:function getSuffix(){this._minimize();return this.suffixes||[]}/** | ||
*/},{key:"getSuffix",value:function getSuffix(){this._minimize();return this.suffixes||[]}/** | ||
* Return the number of characters of non-whitespace text that | ||
@@ -188,6 +170,6 @@ * have been accumulated so far in this accumulator. Components | ||
* @return {number} the length of the non-whitespace text accumulated so far | ||
*/},{key:'getTextLength',value:function getTextLength(){whiteSpace.lastIndex=0;return this.text.replace(whiteSpace,'').trim().length}/** | ||
*/},{key:"getTextLength",value:function getTextLength(){whiteSpace.lastIndex=0;return this.text.replace(whiteSpace,"").trim().length}/** | ||
* @private | ||
* Return the depth of the stack from the given node. | ||
*/},{key:'countCurrentLevel',value:function countCurrentLevel(node){return node.parent?this.countCurrentLevel(node.parent)+1:0}/** | ||
*/},{key:"countCurrentLevel",value:function countCurrentLevel(node){return node.parent?this.countCurrentLevel(node.parent)+1:0}/** | ||
* Return the current depth of the context stack. If the accumulator is | ||
@@ -197,6 +179,6 @@ * currently at the root, it will return 0. | ||
* is nothing on the stack yet | ||
*/},{key:'getCurrentLevel',value:function getCurrentLevel(){return this.countCurrentLevel(this.currentLevel)}/** | ||
*/},{key:"getCurrentLevel",value:function getCurrentLevel(){return this.countCurrentLevel(this.currentLevel)}/** | ||
* Return true if the current context is the root of the message. | ||
* @return {boolean} true if the current context is the root | ||
*/},{key:'isRoot',value:function isRoot(){return this.componentIndex===0}/** | ||
*/},{key:"isRoot",value:function isRoot(){return this.componentIndex===0}/** | ||
* Return the mapping between components and the "extra" | ||
@@ -210,3 +192,3 @@ * information used when creating those components. | ||
* given when the component was created | ||
*/},{key:'getExtra',value:function getExtra(componentNumber){return this.mapping['c'+componentNumber]}/** | ||
*/},{key:"getExtra",value:function getExtra(componentNumber){return this.mapping["c".concat(componentNumber)]}/** | ||
* Return the mapping between a replacement parameter | ||
@@ -221,3 +203,3 @@ * and the "extra" information used when creating those | ||
* given when the parameter was created | ||
*/},{key:'getParam',value:function getParam(paramNumber){return this.mapping['p'+paramNumber]}/** | ||
*/},{key:"getParam",value:function getParam(paramNumber){return this.mapping["p".concat(paramNumber)]}/** | ||
* Return the mappings between component names and | ||
@@ -227,3 +209,3 @@ * their "extra" information they represent. | ||
* component names and their "extra" information. | ||
*/},{key:'getMapping',value:function getMapping(){return this.mapping}}],[{key:'create',value:function create(str,source){var ma=new MessageAccumulator;if(str){ma._parse(str,source&&source.getMapping()||{},ma.root)}return ma}}]);return MessageAccumulator}();exports.default=MessageAccumulator; | ||
*/},{key:"getMapping",value:function getMapping(){return this.mapping}}],[{key:"create",value:function create(str,source){var ma=new MessageAccumulator;if(str){ma._parse(str,source&&source.getMapping()||{},ma.root)}return ma}}]);return MessageAccumulator}();exports["default"]=MessageAccumulator; | ||
//# sourceMappingURL=message-accumulator-es5.js.map |
{ | ||
"name": "message-accumulator", | ||
"version": "2.2.0", | ||
"version": "2.2.1", | ||
"main": "./message-accumulator-es5.js", | ||
@@ -51,22 +51,23 @@ "main-es6": "./message-accumulator.js", | ||
"test": "npm run build ; node test/testSuite.js", | ||
"debug": "npm run build ; node --inspect-brk test/testSuite.js" | ||
"debug": "npm run build ; node --inspect-brk test/testSuite.js", | ||
"clean": "git clean -f -d * ; rm -rf lib" | ||
}, | ||
"dependencies": { | ||
"ilib-tree-node": "^1.2.0" | ||
"ilib-tree-node": "^1.2.2" | ||
}, | ||
"devDependencies": { | ||
"babel-core": "^6.26.0", | ||
"babel-preset-env": "*", | ||
"babel-register": "^6.26.0", | ||
"babel-runtime": "^6.26.0", | ||
"grunt": "^1.0.3", | ||
"grunt-babel": "^7.0.0", | ||
"@babel/core": "^7.0.0", | ||
"@babel/preset-env": "^7.4.5", | ||
"@babel/register": "^7.0.0", | ||
"@babel/runtime": "^7.0.0", | ||
"grunt": "^1.0.4", | ||
"grunt-cli": "^1.3.2", | ||
"grunt-babel": "^8.0.0", | ||
"grunt-contrib-clean": "^2.0.0", | ||
"grunt-contrib-jshint": "^2.0.0", | ||
"grunt-contrib-jshint": "^2.1.0", | ||
"grunt-contrib-nodeunit": "^2.0.0", | ||
"grunt-contrib-uglify": "^4.0.0", | ||
"load-grunt-tasks": "^4.0.0", | ||
"nodeunit": "0.11.0", | ||
"promise": "8.0.1" | ||
"grunt-contrib-uglify": "^4.0.1", | ||
"load-grunt-tasks": "^5.0.0", | ||
"nodeunit": "0.11.0" | ||
} | ||
} |
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
89973
713
Updatedilib-tree-node@^1.2.2