freeCodeCamp/utils/get-lines.js
Oliver Eyton-Williams 9df098953d Feat: editable dom element (#39341)
* feat: __testEditable allows editable region tests

It's not entirely isolated, but it makes it possible to select only the
element with id 'editable-only' which is built solely from code inside
the editable region.

* fix(client): missing editableContents -> ''

Previously it was added as the string 'undefined'

* fix: more informative error messages

* fix: DRY, correct and test getLines
2020-09-16 11:54:11 +05:30

17 lines
375 B
JavaScript

const { isEmpty } = require('lodash');
// TODO: secure with tests
function getLines(contents, range) {
if (isEmpty(range)) {
return '';
}
const lines = contents.split('\n');
const editableLines =
isEmpty(lines) || range[1] <= range[0]
? []
: lines.slice(range[0], range[1] - 1);
return editableLines.join('\n');
}
exports.getLines = getLines;