utils.js 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright 2016 Palantir Technologies, Inc.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. Object.defineProperty(exports, "__esModule", { value: true });
  19. /**
  20. * Enforces the invariant that the input is an array.
  21. */
  22. function arrayify(arg) {
  23. if (Array.isArray(arg)) {
  24. return arg;
  25. }
  26. else if (arg != undefined) {
  27. return [arg];
  28. }
  29. else {
  30. return [];
  31. }
  32. }
  33. exports.arrayify = arrayify;
  34. /**
  35. * @deprecated (no longer used)
  36. * Enforces the invariant that the input is an object.
  37. */
  38. function objectify(arg) {
  39. if (typeof arg === "object" && arg != undefined) {
  40. return arg;
  41. }
  42. else {
  43. return {};
  44. }
  45. }
  46. exports.objectify = objectify;
  47. function hasOwnProperty(arg, key) {
  48. return Object.prototype.hasOwnProperty.call(arg, key);
  49. }
  50. exports.hasOwnProperty = hasOwnProperty;
  51. /**
  52. * Replace hyphens in a rule name by upper-casing the letter after them.
  53. * E.g. "foo-bar" -> "fooBar"
  54. */
  55. function camelize(stringWithHyphens) {
  56. return stringWithHyphens.replace(/-(.)/g, function (_, nextLetter) { return nextLetter.toUpperCase(); });
  57. }
  58. exports.camelize = camelize;
  59. function isUpperCase(str) {
  60. return str === str.toUpperCase();
  61. }
  62. exports.isUpperCase = isUpperCase;
  63. function isLowerCase(str) {
  64. return str === str.toLowerCase();
  65. }
  66. exports.isLowerCase = isLowerCase;
  67. /**
  68. * Removes leading indents from a template string without removing all leading whitespace
  69. */
  70. function dedent(strings) {
  71. var values = [];
  72. for (var _i = 1; _i < arguments.length; _i++) {
  73. values[_i - 1] = arguments[_i];
  74. }
  75. var fullString = strings.reduce(function (accumulator, str, i) { return "" + accumulator + values[i - 1] + str; });
  76. // match all leading spaces/tabs at the start of each line
  77. var match = fullString.match(/^[ \t]*(?=\S)/gm);
  78. if (match === null) {
  79. // e.g. if the string is empty or all whitespace.
  80. return fullString;
  81. }
  82. // find the smallest indent, we don't want to remove all leading whitespace
  83. var indent = Math.min.apply(Math, match.map(function (el) { return el.length; }));
  84. var regexp = new RegExp("^[ \\t]{" + indent + "}", "gm");
  85. fullString = indent > 0 ? fullString.replace(regexp, "") : fullString;
  86. return fullString;
  87. }
  88. exports.dedent = dedent;
  89. /**
  90. * Strip comments from file content.
  91. */
  92. function stripComments(content) {
  93. /**
  94. * First capturing group matches double quoted string
  95. * Second matches single quotes string
  96. * Third matches block comments
  97. * Fourth matches line comments
  98. */
  99. var regexp = /("(?:[^\\\"]*(?:\\.)?)*")|('(?:[^\\\']*(?:\\.)?)*')|(\/\*(?:\r?\n|.)*?\*\/)|(\/{2,}.*?(?:(?:\r?\n)|$))/g;
  100. var result = content.replace(regexp, function (match, _m1, _m2, m3, m4) {
  101. // Only one of m1, m2, m3, m4 matches
  102. if (m3 !== undefined) {
  103. // A block comment. Replace with nothing
  104. return "";
  105. }
  106. else if (m4 !== undefined) {
  107. // A line comment. If it ends in \r?\n then keep it.
  108. var length = m4.length;
  109. if (length > 2 && m4[length - 1] === "\n") {
  110. return m4[length - 2] === "\r" ? "\r\n" : "\n";
  111. }
  112. else {
  113. return "";
  114. }
  115. }
  116. else {
  117. // We match a string
  118. return match;
  119. }
  120. });
  121. return result;
  122. }
  123. exports.stripComments = stripComments;
  124. /**
  125. * Escapes all special characters in RegExp pattern to avoid broken regular expressions and ensure proper matches
  126. */
  127. function escapeRegExp(re) {
  128. return re.replace(/[.+*?|^$[\]{}()\\]/g, "\\$&");
  129. }
  130. exports.escapeRegExp = escapeRegExp;
  131. function arraysAreEqual(a, b, eq) {
  132. return a === b || a !== undefined && b !== undefined && a.length === b.length && a.every(function (x, idx) { return eq(x, b[idx]); });
  133. }
  134. exports.arraysAreEqual = arraysAreEqual;
  135. /** Returns the first non-`undefined` result. */
  136. function find(inputs, getResult) {
  137. for (var _i = 0, inputs_1 = inputs; _i < inputs_1.length; _i++) {
  138. var element = inputs_1[_i];
  139. var result = getResult(element);
  140. if (result !== undefined) {
  141. return result;
  142. }
  143. }
  144. return undefined;
  145. }
  146. exports.find = find;
  147. /** Returns an array that is the concatenation of all output arrays. */
  148. function flatMap(inputs, getOutputs) {
  149. var out = [];
  150. for (var i = 0; i < inputs.length; i++) {
  151. out.push.apply(out, getOutputs(inputs[i], i));
  152. }
  153. return out;
  154. }
  155. exports.flatMap = flatMap;
  156. /** Returns an array of all outputs that are not `undefined`. */
  157. function mapDefined(inputs, getOutput) {
  158. var out = [];
  159. for (var _i = 0, inputs_2 = inputs; _i < inputs_2.length; _i++) {
  160. var input = inputs_2[_i];
  161. var output = getOutput(input);
  162. if (output !== undefined) {
  163. out.push(output);
  164. }
  165. }
  166. return out;
  167. }
  168. exports.mapDefined = mapDefined;
  169. function readBufferWithDetectedEncoding(buffer) {
  170. switch (detectBufferEncoding(buffer)) {
  171. case "utf8":
  172. return buffer.toString();
  173. case "utf8-bom":
  174. return buffer.toString("utf-8", 2);
  175. case "utf16le":
  176. return buffer.toString("utf16le", 2);
  177. case "utf16be":
  178. // Round down to nearest multiple of 2.
  179. var len = buffer.length & ~1; // tslint:disable-line no-bitwise
  180. // Flip all byte pairs, then read as little-endian.
  181. for (var i = 0; i < len; i += 2) {
  182. var temp = buffer[i];
  183. buffer[i] = buffer[i + 1];
  184. buffer[i + 1] = temp;
  185. }
  186. return buffer.toString("utf16le", 2);
  187. }
  188. }
  189. exports.readBufferWithDetectedEncoding = readBufferWithDetectedEncoding;
  190. function detectBufferEncoding(buffer, length) {
  191. if (length === void 0) { length = buffer.length; }
  192. if (length < 2) {
  193. return "utf8";
  194. }
  195. switch (buffer[0]) {
  196. case 0xEF:
  197. if (buffer[1] === 0xBB && length >= 3 && buffer[2] === 0xBF) {
  198. return "utf8-bom";
  199. }
  200. break;
  201. case 0xFE:
  202. if (buffer[1] === 0xFF) {
  203. return "utf16be";
  204. }
  205. break;
  206. case 0xFF:
  207. if (buffer[1] === 0xFE) {
  208. return "utf16le";
  209. }
  210. }
  211. return "utf8";
  212. }
  213. exports.detectBufferEncoding = detectBufferEncoding;
  214. // converts Windows normalized paths (with backwards slash `\`) to paths used by TypeScript (with forward slash `/`)
  215. function denormalizeWinPath(path) {
  216. return path.replace(/\\/g, "/");
  217. }
  218. exports.denormalizeWinPath = denormalizeWinPath;