UNPKG

editer

Version:

A high level multiline string manipulation library

121 lines (102 loc) 3.18 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = getTargetInfo; var _locater = require('locater'); var _locater2 = _interopRequireDefault(_locater); var _lodash = require('lodash'); var _lodash2 = _interopRequireDefault(_lodash); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var lineBreak = '\n'; function getTargetInfo(target, condition) { if (condition.after) { if (condition.after.line) { var newLinePos = _locater2.default.find('\n', target, { getGlobalIndices: true }); if (!newLinePos.length) { return; } var targetNewLinePos = _lodash2.default.find(newLinePos, { line: condition.after.line }); var targetIndex = targetNewLinePos.globalIndex; return { index: targetIndex, line: targetNewLinePos.line }; } if (condition.after.regex) { var matches = _locater2.default.find(condition.after.regex, target, { getGlobalIndices: true, getMatches: true }); if (!matches.length) { return; } var targetMatchPos = undefined, targetIndex = undefined; if (condition.after.occurrence) { targetMatchPos = matches[condition.after.occurrence - 1]; } else if (condition.after.last) { targetMatchPos = matches[matches.length - 1]; } else { targetMatchPos = matches[0]; } if (targetMatchPos.match.slice(-1) === lineBreak) { targetIndex = targetMatchPos.globalIndex + targetMatchPos.match.length - 1; } else { targetIndex = targetMatchPos.globalIndex + targetMatchPos.match.length; } if (!targetIndex) { return; } else { return { index: targetIndex, line: targetMatchPos.line }; } } } if (condition.before) { if (condition.before.line) { var newLinePos = _locater2.default.find('\n', target, { getGlobalIndices: true }); if (!newLinePos.length) { return; } var targetNewLinePos = _lodash2.default.find(newLinePos, { line: condition.before.line - 1 }); var targetIndex = undefined; if (targetNewLinePos) { return { index: targetNewLinePos.globalIndex + 1, line: targetNewLinePos.line }; } else { return { index: 0, line: 0 }; } } if (condition.before.regex) { var matches = _locater2.default.find(condition.before.regex, target, { getGlobalIndices: true, getMatches: true }); if (!matches.length) { return; } var targetMatchPos = undefined, targetIndex = undefined; if (condition.before.occurrence) { targetMatchPos = matches[condition.before.occurrence - 1]; } else if (condition.before.last) { targetMatchPos = matches[matches.length - 1]; } else { targetMatchPos = matches[0]; } targetIndex = targetMatchPos.globalIndex; return { index: targetIndex, line: targetMatchPos.line }; } } }