Files
zhibo/node_modules/unist-util-stringify-position/index.js
T
xiaoyu ac8f91d4d3 init
2023-05-22 14:23:20 +08:00

63 lines
1.3 KiB
JavaScript
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('unist').Node} Node
* @typedef {import('unist').Position} Position
* @typedef {object & {type: string, position?: Position|undefined}} NodeLike
*/
/**
* Stringify one point, a position (start and end points), or a nodes
* positional information.
*
* @param {Node|NodeLike|Position|Point|null} [value]
* @returns {string}
*/
export function stringifyPosition(value) {
// Nothing.
if (!value || typeof value !== 'object') {
return ''
}
// Node.
if ('position' in value || 'type' in value) {
return position(value.position)
}
// Position.
if ('start' in value || 'end' in value) {
return position(value)
}
// Point.
if ('line' in value || 'column' in value) {
return point(value)
}
// ?
return ''
}
/**
* @param {Point|undefined} point
* @returns {string}
*/
function point(point) {
return index(point && point.line) + ':' + index(point && point.column)
}
/**
* @param {Position|undefined} pos
* @returns {string}
*/
function position(pos) {
return point(pos && pos.start) + '-' + point(pos && pos.end)
}
/**
* @param {number|undefined} value
* @returns {number}
*/
function index(value) {
return value && typeof value === 'number' ? value : 1
}