UNPKG

7.17 kBJavaScriptView Raw
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 */
18Object.defineProperty(exports, "__esModule", { value: true });
19/**
20 * Enforces the invariant that the input is an array.
21 */
22function 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}
33exports.arrayify = arrayify;
34/**
35 * @deprecated (no longer used)
36 * Enforces the invariant that the input is an object.
37 */
38function objectify(arg) {
39 if (typeof arg === "object" && arg != undefined) {
40 return arg;
41 }
42 else {
43 return {};
44 }
45}
46exports.objectify = objectify;
47function hasOwnProperty(arg, key) {
48 return Object.prototype.hasOwnProperty.call(arg, key);
49}
50exports.hasOwnProperty = hasOwnProperty;
51/**
52 * Replace hyphens in a rule name by upper-casing the letter after them.
53 * E.g. "foo-bar" -> "fooBar"
54 */
55function camelize(stringWithHyphens) {
56 return stringWithHyphens.replace(/-(.)/g, function (_, nextLetter) { return nextLetter.toUpperCase(); });
57}
58exports.camelize = camelize;
59function isUpperCase(str) {
60 return str === str.toUpperCase();
61}
62exports.isUpperCase = isUpperCase;
63function isLowerCase(str) {
64 return str === str.toLowerCase();
65}
66exports.isLowerCase = isLowerCase;
67/**
68 * Removes leading indents from a template string without removing all leading whitespace
69 */
70function 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}
88exports.dedent = dedent;
89/**
90 * Strip comments from file content.
91 */
92function 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}
123exports.stripComments = stripComments;
124/**
125 * Escapes all special characters in RegExp pattern to avoid broken regular expressions and ensure proper matches
126 */
127function escapeRegExp(re) {
128 return re.replace(/[.+*?|^$[\]{}()\\]/g, "\\$&");
129}
130exports.escapeRegExp = escapeRegExp;
131function 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}
134exports.arraysAreEqual = arraysAreEqual;
135/** Returns the first non-`undefined` result. */
136function 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}
146exports.find = find;
147/** Returns an array that is the concatenation of all output arrays. */
148function 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}
155exports.flatMap = flatMap;
156/** Returns an array of all outputs that are not `undefined`. */
157function 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}
168exports.mapDefined = mapDefined;
169function 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}
189exports.readBufferWithDetectedEncoding = readBufferWithDetectedEncoding;
190function 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}
213exports.detectBufferEncoding = detectBufferEncoding;
214// converts Windows normalized paths (with backwards slash `\`) to paths used by TypeScript (with forward slash `/`)
215function denormalizeWinPath(path) {
216 return path.replace(/\\/g, "/");
217}
218exports.denormalizeWinPath = denormalizeWinPath;