util.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. Copyright 2013 Daniel Wirtz <dcode@dcode.io>
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. var ProtoBuf = require("../../index.js");
  14. /**
  15. * Utility namespace.
  16. * @exports pbjs/util
  17. * @namespace
  18. */
  19. var util = module.exports = {};
  20. /**
  21. * Extracts builder options with the specified prefix from a set of CLI options.
  22. * @param {!Object.<string,*>} options CLI options
  23. * @param {string} prefix Prefix
  24. * @returns {!Object.<string,*>}
  25. */
  26. util.getBuilderOptions = function(options, prefix) {
  27. if (!options[prefix])
  28. return {};
  29. var builderOptions = {};
  30. options[prefix].forEach(function(kv) {
  31. var key, val;
  32. var p = kv.indexOf("=");
  33. if (p < 0) {
  34. key = kv;
  35. val = true;
  36. } else {
  37. key = kv.substring(0, p);
  38. val = kv.substring(p+1);
  39. if (val === "true")
  40. val = true;
  41. else if (val === "false")
  42. val = false;
  43. else {
  44. var intval = parseInt(val, 10);
  45. if (intval == val)
  46. val = intval;
  47. }
  48. }
  49. builderOptions[key] = val;
  50. });
  51. return builderOptions;
  52. };
  53. /**
  54. * Pads a string to the specified length.
  55. * @param {string} str String to pad
  56. * @param {number} len Pad length
  57. * @param {boolean=} left Whether to pad to the left, defaults to `false`
  58. * @returns {string}
  59. */
  60. util.pad = function(str, len, left) {
  61. while (str.length < len)
  62. left ? str = " "+str : str += " ";
  63. return str;
  64. };
  65. /**
  66. * Indents a string by the specified whitespace.
  67. * @param {string} str String to indent
  68. * @param {string|number} ws Whitespace string or number of whitespaces
  69. * @returns {string}
  70. */
  71. util.indent = function(str, ws) {
  72. if (ws === 0 || ws === "")
  73. return str;
  74. var lines = str.split(/\r?\n/);
  75. if (typeof ws === 'number') {
  76. var n = ws; ws = "";
  77. while (ws.length < n) ws += " ";
  78. }
  79. for (var i=1; i<lines.length; ++i)
  80. lines[i] = ws+lines[i];
  81. return lines.join("\n");
  82. };
  83. /**
  84. * Extends an object with additional properties.
  85. * @param {!Object.<string,*>} subject Subject to extend
  86. * @param {!Object.<string,*>} extension Extensions to apply
  87. */
  88. util.extend = function(subject, extension) {
  89. Object.keys(extension).forEach(function(key) {
  90. subject[key] = extension[key];
  91. });
  92. };
  93. /**
  94. * Groups extensions by extended message.
  95. * @param {!ProtoBuf.Reflect.Namespace} ns Namespace
  96. * @returns {?Object.<string,!Array.<!ProtoBuf.Reflect.Message.ExtensionField>>}
  97. */
  98. util.groupExtensions = function(ns) {
  99. var exts = {},
  100. n = 0;
  101. ns.getChildren(ProtoBuf.Reflect.Extension).forEach(function(ext) {
  102. var msg = ext.field.parent,
  103. fqn = msg.fqn();
  104. if (!exts[fqn])
  105. exts[fqn] = [];
  106. exts[fqn].push(ext.field);
  107. n++;
  108. });
  109. return n > 0 ? exts : null;
  110. };
  111. /**
  112. * Tests if the specified import name is referencing an internal descriptor.
  113. * @param {string} name Import name
  114. * @returns {boolean}
  115. */
  116. util.isDescriptor = function(name) {
  117. return /^google\/protobuf\/descriptor/.test(name);
  118. };