persona-community-5/.pnpm-store/v3/files/93/83fe0d15b8690a2cfc53623a3cc3aa8a58e28e5047d65bc5e61a8dc953445c70b90f38346c3ab217ba3ec7940a546570a1d7f0a39523aaa48d04b5ee0854aa
rdev-worker a1d0d1bf1c
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
build: /implement-feature community-ui --requirements 'Build the React commu...
2026-02-24 08:22:30 +00:00

34 lines
1.0 KiB
Plaintext

const getOffsets = ({
oneBased,
oneBasedLine = oneBased,
oneBasedColumn = oneBased,
} = {}) => [oneBasedLine ? 1 : 0, oneBasedColumn ? 1 : 0];
// Performance https://github.com/sindresorhus/index-to-position/pull/9
function getPosition(text, textIndex, options) {
const lineBreakBefore = textIndex === 0 ? -1 : text.lastIndexOf('\n', textIndex - 1);
const [lineOffset, columnOffset] = getOffsets(options);
return {
line: lineBreakBefore === -1
? lineOffset
: text.slice(0, lineBreakBefore + 1).match(/\n/g).length + lineOffset,
column: textIndex - lineBreakBefore - 1 + columnOffset,
};
}
export default function indexToPosition(text, textIndex, options) {
if (typeof text !== 'string') {
throw new TypeError('Text parameter should be a string');
}
if (!Number.isInteger(textIndex)) {
throw new TypeError('Index parameter should be an integer');
}
if (textIndex < 0 || textIndex > text.length) {
throw new RangeError('Index out of bounds');
}
return getPosition(text, textIndex, options);
}