|
Last change
on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago |
|
Fix frontend appearance
|
-
Property mode
set to
100644
|
|
File size:
728 bytes
|
| Rev | Line | |
|---|
| [9af201e] | 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | /**
|
|---|
| 9 | * @param {string} str string
|
|---|
| 10 | * @returns {string[]} array of string separated by lines
|
|---|
| 11 | */
|
|---|
| 12 | const splitIntoLines = (str) => {
|
|---|
| 13 | const results = [];
|
|---|
| 14 | const len = str.length;
|
|---|
| 15 | let i = 0;
|
|---|
| 16 | while (i < len) {
|
|---|
| 17 | // indexOf is implemented natively and is significantly faster than
|
|---|
| 18 | // scanning char-by-char with charCodeAt for long lines.
|
|---|
| 19 | const n = str.indexOf("\n", i);
|
|---|
| 20 | if (n === -1) {
|
|---|
| 21 | results.push(i === 0 ? str : str.slice(i));
|
|---|
| 22 | break;
|
|---|
| 23 | }
|
|---|
| 24 | if (n === i) {
|
|---|
| 25 | results.push("\n");
|
|---|
| 26 | } else {
|
|---|
| 27 | results.push(str.slice(i, n + 1));
|
|---|
| 28 | }
|
|---|
| 29 | i = n + 1;
|
|---|
| 30 | }
|
|---|
| 31 | return results;
|
|---|
| 32 | };
|
|---|
| 33 |
|
|---|
| 34 | module.exports = splitIntoLines;
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.