a zip code crypto-currency system good for red ONLY

lines.js 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. var assert = require("assert");
  2. var sourceMap = require("source-map");
  3. var normalizeOptions = require("./options").normalize;
  4. var secretKey = require("private").makeUniqueKey();
  5. var types = require("./types");
  6. var isString = types.builtInTypes.string;
  7. var comparePos = require("./util").comparePos;
  8. var Mapping = require("./mapping");
  9. // Goals:
  10. // 1. Minimize new string creation.
  11. // 2. Keep (de)identation O(lines) time.
  12. // 3. Permit negative indentations.
  13. // 4. Enforce immutability.
  14. // 5. No newline characters.
  15. function getSecret(lines) {
  16. return lines[secretKey];
  17. }
  18. function Lines(infos, sourceFileName) {
  19. assert.ok(this instanceof Lines);
  20. assert.ok(infos.length > 0);
  21. if (sourceFileName) {
  22. isString.assert(sourceFileName);
  23. } else {
  24. sourceFileName = null;
  25. }
  26. Object.defineProperty(this, secretKey, {
  27. value: {
  28. infos: infos,
  29. mappings: [],
  30. name: sourceFileName,
  31. cachedSourceMap: null
  32. }
  33. });
  34. if (sourceFileName) {
  35. getSecret(this).mappings.push(new Mapping(this, {
  36. start: this.firstPos(),
  37. end: this.lastPos()
  38. }));
  39. }
  40. }
  41. // Exposed for instanceof checks. The fromString function should be used
  42. // to create new Lines objects.
  43. exports.Lines = Lines;
  44. var Lp = Lines.prototype;
  45. // These properties used to be assigned to each new object in the Lines
  46. // constructor, but we can more efficiently stuff them into the secret and
  47. // let these lazy accessors compute their values on-the-fly.
  48. Object.defineProperties(Lp, {
  49. length: {
  50. get: function() {
  51. return getSecret(this).infos.length;
  52. }
  53. },
  54. name: {
  55. get: function() {
  56. return getSecret(this).name;
  57. }
  58. }
  59. });
  60. function copyLineInfo(info) {
  61. return {
  62. line: info.line,
  63. indent: info.indent,
  64. locked: info.locked,
  65. sliceStart: info.sliceStart,
  66. sliceEnd: info.sliceEnd
  67. };
  68. }
  69. var fromStringCache = {};
  70. var hasOwn = fromStringCache.hasOwnProperty;
  71. var maxCacheKeyLen = 10;
  72. function countSpaces(spaces, tabWidth) {
  73. var count = 0;
  74. var len = spaces.length;
  75. for (var i = 0; i < len; ++i) {
  76. switch (spaces.charCodeAt(i)) {
  77. case 9: // '\t'
  78. assert.strictEqual(typeof tabWidth, "number");
  79. assert.ok(tabWidth > 0);
  80. var next = Math.ceil(count / tabWidth) * tabWidth;
  81. if (next === count) {
  82. count += tabWidth;
  83. } else {
  84. count = next;
  85. }
  86. break;
  87. case 11: // '\v'
  88. case 12: // '\f'
  89. case 13: // '\r'
  90. case 0xfeff: // zero-width non-breaking space
  91. // These characters contribute nothing to indentation.
  92. break;
  93. case 32: // ' '
  94. default: // Treat all other whitespace like ' '.
  95. count += 1;
  96. break;
  97. }
  98. }
  99. return count;
  100. }
  101. exports.countSpaces = countSpaces;
  102. var leadingSpaceExp = /^\s*/;
  103. // As specified here: http://www.ecma-international.org/ecma-262/6.0/#sec-line-terminators
  104. var lineTerminatorSeqExp =
  105. /\u000D\u000A|\u000D(?!\u000A)|\u000A|\u2028|\u2029/;
  106. /**
  107. * @param {Object} options - Options object that configures printing.
  108. */
  109. function fromString(string, options) {
  110. if (string instanceof Lines)
  111. return string;
  112. string += "";
  113. var tabWidth = options && options.tabWidth;
  114. var tabless = string.indexOf("\t") < 0;
  115. var locked = !! (options && options.locked);
  116. var cacheable = !options && tabless && (string.length <= maxCacheKeyLen);
  117. assert.ok(tabWidth || tabless, "No tab width specified but encountered tabs in string\n" + string);
  118. if (cacheable && hasOwn.call(fromStringCache, string))
  119. return fromStringCache[string];
  120. var lines = new Lines(string.split(lineTerminatorSeqExp).map(function(line) {
  121. var spaces = leadingSpaceExp.exec(line)[0];
  122. return {
  123. line: line,
  124. indent: countSpaces(spaces, tabWidth),
  125. // Boolean indicating whether this line can be reindented.
  126. locked: locked,
  127. sliceStart: spaces.length,
  128. sliceEnd: line.length
  129. };
  130. }), normalizeOptions(options).sourceFileName);
  131. if (cacheable)
  132. fromStringCache[string] = lines;
  133. return lines;
  134. }
  135. exports.fromString = fromString;
  136. function isOnlyWhitespace(string) {
  137. return !/\S/.test(string);
  138. }
  139. Lp.toString = function(options) {
  140. return this.sliceString(this.firstPos(), this.lastPos(), options);
  141. };
  142. Lp.getSourceMap = function(sourceMapName, sourceRoot) {
  143. if (!sourceMapName) {
  144. // Although we could make up a name or generate an anonymous
  145. // source map, instead we assume that any consumer who does not
  146. // provide a name does not actually want a source map.
  147. return null;
  148. }
  149. var targetLines = this;
  150. function updateJSON(json) {
  151. json = json || {};
  152. isString.assert(sourceMapName);
  153. json.file = sourceMapName;
  154. if (sourceRoot) {
  155. isString.assert(sourceRoot);
  156. json.sourceRoot = sourceRoot;
  157. }
  158. return json;
  159. }
  160. var secret = getSecret(targetLines);
  161. if (secret.cachedSourceMap) {
  162. // Since Lines objects are immutable, we can reuse any source map
  163. // that was previously generated. Nevertheless, we return a new
  164. // JSON object here to protect the cached source map from outside
  165. // modification.
  166. return updateJSON(secret.cachedSourceMap.toJSON());
  167. }
  168. var smg = new sourceMap.SourceMapGenerator(updateJSON());
  169. var sourcesToContents = {};
  170. secret.mappings.forEach(function(mapping) {
  171. var sourceCursor = mapping.sourceLines.skipSpaces(
  172. mapping.sourceLoc.start
  173. ) || mapping.sourceLines.lastPos();
  174. var targetCursor = targetLines.skipSpaces(
  175. mapping.targetLoc.start
  176. ) || targetLines.lastPos();
  177. while (comparePos(sourceCursor, mapping.sourceLoc.end) < 0 &&
  178. comparePos(targetCursor, mapping.targetLoc.end) < 0) {
  179. var sourceChar = mapping.sourceLines.charAt(sourceCursor);
  180. var targetChar = targetLines.charAt(targetCursor);
  181. assert.strictEqual(sourceChar, targetChar);
  182. var sourceName = mapping.sourceLines.name;
  183. // Add mappings one character at a time for maximum resolution.
  184. smg.addMapping({
  185. source: sourceName,
  186. original: { line: sourceCursor.line,
  187. column: sourceCursor.column },
  188. generated: { line: targetCursor.line,
  189. column: targetCursor.column }
  190. });
  191. if (!hasOwn.call(sourcesToContents, sourceName)) {
  192. var sourceContent = mapping.sourceLines.toString();
  193. smg.setSourceContent(sourceName, sourceContent);
  194. sourcesToContents[sourceName] = sourceContent;
  195. }
  196. targetLines.nextPos(targetCursor, true);
  197. mapping.sourceLines.nextPos(sourceCursor, true);
  198. }
  199. });
  200. secret.cachedSourceMap = smg;
  201. return smg.toJSON();
  202. };
  203. Lp.bootstrapCharAt = function(pos) {
  204. assert.strictEqual(typeof pos, "object");
  205. assert.strictEqual(typeof pos.line, "number");
  206. assert.strictEqual(typeof pos.column, "number");
  207. var line = pos.line,
  208. column = pos.column,
  209. strings = this.toString().split(lineTerminatorSeqExp),
  210. string = strings[line - 1];
  211. if (typeof string === "undefined")
  212. return "";
  213. if (column === string.length &&
  214. line < strings.length)
  215. return "\n";
  216. if (column >= string.length)
  217. return "";
  218. return string.charAt(column);
  219. };
  220. Lp.charAt = function(pos) {
  221. assert.strictEqual(typeof pos, "object");
  222. assert.strictEqual(typeof pos.line, "number");
  223. assert.strictEqual(typeof pos.column, "number");
  224. var line = pos.line,
  225. column = pos.column,
  226. secret = getSecret(this),
  227. infos = secret.infos,
  228. info = infos[line - 1],
  229. c = column;
  230. if (typeof info === "undefined" || c < 0)
  231. return "";
  232. var indent = this.getIndentAt(line);
  233. if (c < indent)
  234. return " ";
  235. c += info.sliceStart - indent;
  236. if (c === info.sliceEnd &&
  237. line < this.length)
  238. return "\n";
  239. if (c >= info.sliceEnd)
  240. return "";
  241. return info.line.charAt(c);
  242. };
  243. Lp.stripMargin = function(width, skipFirstLine) {
  244. if (width === 0)
  245. return this;
  246. assert.ok(width > 0, "negative margin: " + width);
  247. if (skipFirstLine && this.length === 1)
  248. return this;
  249. var secret = getSecret(this);
  250. var lines = new Lines(secret.infos.map(function(info, i) {
  251. if (info.line && (i > 0 || !skipFirstLine)) {
  252. info = copyLineInfo(info);
  253. info.indent = Math.max(0, info.indent - width);
  254. }
  255. return info;
  256. }));
  257. if (secret.mappings.length > 0) {
  258. var newMappings = getSecret(lines).mappings;
  259. assert.strictEqual(newMappings.length, 0);
  260. secret.mappings.forEach(function(mapping) {
  261. newMappings.push(mapping.indent(width, skipFirstLine, true));
  262. });
  263. }
  264. return lines;
  265. };
  266. Lp.indent = function(by) {
  267. if (by === 0)
  268. return this;
  269. var secret = getSecret(this);
  270. var lines = new Lines(secret.infos.map(function(info) {
  271. if (info.line && ! info.locked) {
  272. info = copyLineInfo(info);
  273. info.indent += by;
  274. }
  275. return info
  276. }));
  277. if (secret.mappings.length > 0) {
  278. var newMappings = getSecret(lines).mappings;
  279. assert.strictEqual(newMappings.length, 0);
  280. secret.mappings.forEach(function(mapping) {
  281. newMappings.push(mapping.indent(by));
  282. });
  283. }
  284. return lines;
  285. };
  286. Lp.indentTail = function(by) {
  287. if (by === 0)
  288. return this;
  289. if (this.length < 2)
  290. return this;
  291. var secret = getSecret(this);
  292. var lines = new Lines(secret.infos.map(function(info, i) {
  293. if (i > 0 && info.line && ! info.locked) {
  294. info = copyLineInfo(info);
  295. info.indent += by;
  296. }
  297. return info;
  298. }));
  299. if (secret.mappings.length > 0) {
  300. var newMappings = getSecret(lines).mappings;
  301. assert.strictEqual(newMappings.length, 0);
  302. secret.mappings.forEach(function(mapping) {
  303. newMappings.push(mapping.indent(by, true));
  304. });
  305. }
  306. return lines;
  307. };
  308. Lp.lockIndentTail = function () {
  309. if (this.length < 2) {
  310. return this;
  311. }
  312. var infos = getSecret(this).infos;
  313. return new Lines(infos.map(function (info, i) {
  314. info = copyLineInfo(info);
  315. info.locked = i > 0;
  316. return info;
  317. }));
  318. };
  319. Lp.getIndentAt = function(line) {
  320. assert.ok(line >= 1, "no line " + line + " (line numbers start from 1)");
  321. var secret = getSecret(this),
  322. info = secret.infos[line - 1];
  323. return Math.max(info.indent, 0);
  324. };
  325. Lp.guessTabWidth = function() {
  326. var secret = getSecret(this);
  327. if (hasOwn.call(secret, "cachedTabWidth")) {
  328. return secret.cachedTabWidth;
  329. }
  330. var counts = []; // Sparse array.
  331. var lastIndent = 0;
  332. for (var line = 1, last = this.length; line <= last; ++line) {
  333. var info = secret.infos[line - 1];
  334. var sliced = info.line.slice(info.sliceStart, info.sliceEnd);
  335. // Whitespace-only lines don't tell us much about the likely tab
  336. // width of this code.
  337. if (isOnlyWhitespace(sliced)) {
  338. continue;
  339. }
  340. var diff = Math.abs(info.indent - lastIndent);
  341. counts[diff] = ~~counts[diff] + 1;
  342. lastIndent = info.indent;
  343. }
  344. var maxCount = -1;
  345. var result = 2;
  346. for (var tabWidth = 1;
  347. tabWidth < counts.length;
  348. tabWidth += 1) {
  349. if (hasOwn.call(counts, tabWidth) &&
  350. counts[tabWidth] > maxCount) {
  351. maxCount = counts[tabWidth];
  352. result = tabWidth;
  353. }
  354. }
  355. return secret.cachedTabWidth = result;
  356. };
  357. Lp.isOnlyWhitespace = function() {
  358. return isOnlyWhitespace(this.toString());
  359. };
  360. Lp.isPrecededOnlyByWhitespace = function(pos) {
  361. var secret = getSecret(this);
  362. var info = secret.infos[pos.line - 1];
  363. var indent = Math.max(info.indent, 0);
  364. var diff = pos.column - indent;
  365. if (diff <= 0) {
  366. // If pos.column does not exceed the indentation amount, then
  367. // there must be only whitespace before it.
  368. return true;
  369. }
  370. var start = info.sliceStart;
  371. var end = Math.min(start + diff, info.sliceEnd);
  372. var prefix = info.line.slice(start, end);
  373. return isOnlyWhitespace(prefix);
  374. };
  375. Lp.getLineLength = function(line) {
  376. var secret = getSecret(this),
  377. info = secret.infos[line - 1];
  378. return this.getIndentAt(line) + info.sliceEnd - info.sliceStart;
  379. };
  380. Lp.nextPos = function(pos, skipSpaces) {
  381. var l = Math.max(pos.line, 0),
  382. c = Math.max(pos.column, 0);
  383. if (c < this.getLineLength(l)) {
  384. pos.column += 1;
  385. return skipSpaces
  386. ? !!this.skipSpaces(pos, false, true)
  387. : true;
  388. }
  389. if (l < this.length) {
  390. pos.line += 1;
  391. pos.column = 0;
  392. return skipSpaces
  393. ? !!this.skipSpaces(pos, false, true)
  394. : true;
  395. }
  396. return false;
  397. };
  398. Lp.prevPos = function(pos, skipSpaces) {
  399. var l = pos.line,
  400. c = pos.column;
  401. if (c < 1) {
  402. l -= 1;
  403. if (l < 1)
  404. return false;
  405. c = this.getLineLength(l);
  406. } else {
  407. c = Math.min(c - 1, this.getLineLength(l));
  408. }
  409. pos.line = l;
  410. pos.column = c;
  411. return skipSpaces
  412. ? !!this.skipSpaces(pos, true, true)
  413. : true;
  414. };
  415. Lp.firstPos = function() {
  416. // Trivial, but provided for completeness.
  417. return { line: 1, column: 0 };
  418. };
  419. Lp.lastPos = function() {
  420. return {
  421. line: this.length,
  422. column: this.getLineLength(this.length)
  423. };
  424. };
  425. Lp.skipSpaces = function(pos, backward, modifyInPlace) {
  426. if (pos) {
  427. pos = modifyInPlace ? pos : {
  428. line: pos.line,
  429. column: pos.column
  430. };
  431. } else if (backward) {
  432. pos = this.lastPos();
  433. } else {
  434. pos = this.firstPos();
  435. }
  436. if (backward) {
  437. while (this.prevPos(pos)) {
  438. if (!isOnlyWhitespace(this.charAt(pos)) &&
  439. this.nextPos(pos)) {
  440. return pos;
  441. }
  442. }
  443. return null;
  444. } else {
  445. while (isOnlyWhitespace(this.charAt(pos))) {
  446. if (!this.nextPos(pos)) {
  447. return null;
  448. }
  449. }
  450. return pos;
  451. }
  452. };
  453. Lp.trimLeft = function() {
  454. var pos = this.skipSpaces(this.firstPos(), false, true);
  455. return pos ? this.slice(pos) : emptyLines;
  456. };
  457. Lp.trimRight = function() {
  458. var pos = this.skipSpaces(this.lastPos(), true, true);
  459. return pos ? this.slice(this.firstPos(), pos) : emptyLines;
  460. };
  461. Lp.trim = function() {
  462. var start = this.skipSpaces(this.firstPos(), false, true);
  463. if (start === null)
  464. return emptyLines;
  465. var end = this.skipSpaces(this.lastPos(), true, true);
  466. assert.notStrictEqual(end, null);
  467. return this.slice(start, end);
  468. };
  469. Lp.eachPos = function(callback, startPos, skipSpaces) {
  470. var pos = this.firstPos();
  471. if (startPos) {
  472. pos.line = startPos.line,
  473. pos.column = startPos.column
  474. }
  475. if (skipSpaces && !this.skipSpaces(pos, false, true)) {
  476. return; // Encountered nothing but spaces.
  477. }
  478. do callback.call(this, pos);
  479. while (this.nextPos(pos, skipSpaces));
  480. };
  481. Lp.bootstrapSlice = function(start, end) {
  482. var strings = this.toString().split(
  483. lineTerminatorSeqExp
  484. ).slice(
  485. start.line - 1,
  486. end.line
  487. );
  488. strings.push(strings.pop().slice(0, end.column));
  489. strings[0] = strings[0].slice(start.column);
  490. return fromString(strings.join("\n"));
  491. };
  492. Lp.slice = function(start, end) {
  493. if (!end) {
  494. if (!start) {
  495. // The client seems to want a copy of this Lines object, but
  496. // Lines objects are immutable, so it's perfectly adequate to
  497. // return the same object.
  498. return this;
  499. }
  500. // Slice to the end if no end position was provided.
  501. end = this.lastPos();
  502. }
  503. var secret = getSecret(this);
  504. var sliced = secret.infos.slice(start.line - 1, end.line);
  505. if (start.line === end.line) {
  506. sliced[0] = sliceInfo(sliced[0], start.column, end.column);
  507. } else {
  508. assert.ok(start.line < end.line);
  509. sliced[0] = sliceInfo(sliced[0], start.column);
  510. sliced.push(sliceInfo(sliced.pop(), 0, end.column));
  511. }
  512. var lines = new Lines(sliced);
  513. if (secret.mappings.length > 0) {
  514. var newMappings = getSecret(lines).mappings;
  515. assert.strictEqual(newMappings.length, 0);
  516. secret.mappings.forEach(function(mapping) {
  517. var sliced = mapping.slice(this, start, end);
  518. if (sliced) {
  519. newMappings.push(sliced);
  520. }
  521. }, this);
  522. }
  523. return lines;
  524. };
  525. function sliceInfo(info, startCol, endCol) {
  526. var sliceStart = info.sliceStart;
  527. var sliceEnd = info.sliceEnd;
  528. var indent = Math.max(info.indent, 0);
  529. var lineLength = indent + sliceEnd - sliceStart;
  530. if (typeof endCol === "undefined") {
  531. endCol = lineLength;
  532. }
  533. startCol = Math.max(startCol, 0);
  534. endCol = Math.min(endCol, lineLength);
  535. endCol = Math.max(endCol, startCol);
  536. if (endCol < indent) {
  537. indent = endCol;
  538. sliceEnd = sliceStart;
  539. } else {
  540. sliceEnd -= lineLength - endCol;
  541. }
  542. lineLength = endCol;
  543. lineLength -= startCol;
  544. if (startCol < indent) {
  545. indent -= startCol;
  546. } else {
  547. startCol -= indent;
  548. indent = 0;
  549. sliceStart += startCol;
  550. }
  551. assert.ok(indent >= 0);
  552. assert.ok(sliceStart <= sliceEnd);
  553. assert.strictEqual(lineLength, indent + sliceEnd - sliceStart);
  554. if (info.indent === indent &&
  555. info.sliceStart === sliceStart &&
  556. info.sliceEnd === sliceEnd) {
  557. return info;
  558. }
  559. return {
  560. line: info.line,
  561. indent: indent,
  562. // A destructive slice always unlocks indentation.
  563. locked: false,
  564. sliceStart: sliceStart,
  565. sliceEnd: sliceEnd
  566. };
  567. }
  568. Lp.bootstrapSliceString = function(start, end, options) {
  569. return this.slice(start, end).toString(options);
  570. };
  571. Lp.sliceString = function(start, end, options) {
  572. if (!end) {
  573. if (!start) {
  574. // The client seems to want a copy of this Lines object, but
  575. // Lines objects are immutable, so it's perfectly adequate to
  576. // return the same object.
  577. return this;
  578. }
  579. // Slice to the end if no end position was provided.
  580. end = this.lastPos();
  581. }
  582. options = normalizeOptions(options);
  583. var infos = getSecret(this).infos;
  584. var parts = [];
  585. var tabWidth = options.tabWidth;
  586. for (var line = start.line; line <= end.line; ++line) {
  587. var info = infos[line - 1];
  588. if (line === start.line) {
  589. if (line === end.line) {
  590. info = sliceInfo(info, start.column, end.column);
  591. } else {
  592. info = sliceInfo(info, start.column);
  593. }
  594. } else if (line === end.line) {
  595. info = sliceInfo(info, 0, end.column);
  596. }
  597. var indent = Math.max(info.indent, 0);
  598. var before = info.line.slice(0, info.sliceStart);
  599. if (options.reuseWhitespace &&
  600. isOnlyWhitespace(before) &&
  601. countSpaces(before, options.tabWidth) === indent) {
  602. // Reuse original spaces if the indentation is correct.
  603. parts.push(info.line.slice(0, info.sliceEnd));
  604. continue;
  605. }
  606. var tabs = 0;
  607. var spaces = indent;
  608. if (options.useTabs) {
  609. tabs = Math.floor(indent / tabWidth);
  610. spaces -= tabs * tabWidth;
  611. }
  612. var result = "";
  613. if (tabs > 0) {
  614. result += new Array(tabs + 1).join("\t");
  615. }
  616. if (spaces > 0) {
  617. result += new Array(spaces + 1).join(" ");
  618. }
  619. result += info.line.slice(info.sliceStart, info.sliceEnd);
  620. parts.push(result);
  621. }
  622. return parts.join(options.lineTerminator);
  623. };
  624. Lp.isEmpty = function() {
  625. return this.length < 2 && this.getLineLength(1) < 1;
  626. };
  627. Lp.join = function(elements) {
  628. var separator = this;
  629. var separatorSecret = getSecret(separator);
  630. var infos = [];
  631. var mappings = [];
  632. var prevInfo;
  633. function appendSecret(secret) {
  634. if (secret === null)
  635. return;
  636. if (prevInfo) {
  637. var info = secret.infos[0];
  638. var indent = new Array(info.indent + 1).join(" ");
  639. var prevLine = infos.length;
  640. var prevColumn = Math.max(prevInfo.indent, 0) +
  641. prevInfo.sliceEnd - prevInfo.sliceStart;
  642. prevInfo.line = prevInfo.line.slice(
  643. 0, prevInfo.sliceEnd) + indent + info.line.slice(
  644. info.sliceStart, info.sliceEnd);
  645. // If any part of a line is indentation-locked, the whole line
  646. // will be indentation-locked.
  647. prevInfo.locked = prevInfo.locked || info.locked;
  648. prevInfo.sliceEnd = prevInfo.line.length;
  649. if (secret.mappings.length > 0) {
  650. secret.mappings.forEach(function(mapping) {
  651. mappings.push(mapping.add(prevLine, prevColumn));
  652. });
  653. }
  654. } else if (secret.mappings.length > 0) {
  655. mappings.push.apply(mappings, secret.mappings);
  656. }
  657. secret.infos.forEach(function(info, i) {
  658. if (!prevInfo || i > 0) {
  659. prevInfo = copyLineInfo(info);
  660. infos.push(prevInfo);
  661. }
  662. });
  663. }
  664. function appendWithSeparator(secret, i) {
  665. if (i > 0)
  666. appendSecret(separatorSecret);
  667. appendSecret(secret);
  668. }
  669. elements.map(function(elem) {
  670. var lines = fromString(elem);
  671. if (lines.isEmpty())
  672. return null;
  673. return getSecret(lines);
  674. }).forEach(separator.isEmpty()
  675. ? appendSecret
  676. : appendWithSeparator);
  677. if (infos.length < 1)
  678. return emptyLines;
  679. var lines = new Lines(infos);
  680. getSecret(lines).mappings = mappings;
  681. return lines;
  682. };
  683. exports.concat = function(elements) {
  684. return emptyLines.join(elements);
  685. };
  686. Lp.concat = function(other) {
  687. var args = arguments,
  688. list = [this];
  689. list.push.apply(list, args);
  690. assert.strictEqual(list.length, args.length + 1);
  691. return emptyLines.join(list);
  692. };
  693. // The emptyLines object needs to be created all the way down here so that
  694. // Lines.prototype will be fully populated.
  695. var emptyLines = fromString("");