Urara-Blog/node_modules/.pnpm-store/v3/files/63/36642dbc44c96f4a3b6013d35daa77c62ea81e040e61f8610a566dce0c0813e13c9ff537c7f02d1a23eda9e5cf01185dd0dfe25a9ccd23d58d82096baf2af0
2022-08-14 01:14:53 +08:00

57 lines
1.4 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* @typedef {import('unist').Point} Point
* @typedef {import('../types.js').TrackFields} TrackFields
*/
/**
* Functions to track output positions.
* This info isnt used yet but suchs functionality allows line wrapping,
* and theoretically source maps (though, is there practical use in that?).
*
* @param {TrackFields} options_
*/
export function track(options_) {
// Defaults are used to prevent crashes when older utilities somehow activate
// this code.
/* c8 ignore next 5 */
const options = options_ || {}
const now = options.now || {}
let lineShift = options.lineShift || 0
let line = now.line || 1
let column = now.column || 1
return {move, current, shift}
/**
* Get the current tracked info.
*
* @returns {{now: Point, lineShift: number}}
*/
function current() {
return {now: {line, column}, lineShift}
}
/**
* Define an increased line shift (the typical indent for lines).
*
* @param {number} value
*/
function shift(value) {
lineShift += value
}
/**
* Move past a string.
*
* @param {string} value
* @returns {string}
*/
function move(value = '') {
const chunks = value.split(/\r?\n|\r/g)
const tail = chunks[chunks.length - 1]
line += chunks.length - 1
column =
chunks.length === 1 ? column + tail.length : 1 + tail.length + lineShift
return value
}
}