kjelsrud.dev/node_modules/unist-util-position/lib/index.js
2023-07-19 21:31:30 +02:00

84 lines
1.8 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').Position} Position
* @typedef {import('unist').Node} Node
* @typedef {import('unist').Point} Point
*/
/**
* @typedef NodeLike
* @property {string} type
* @property {PositionLike | null | undefined} [position]
*
* @typedef PositionLike
* @property {PointLike | null | undefined} [start]
* @property {PointLike | null | undefined} [end]
*
* @typedef PointLike
* @property {number | null | undefined} [line]
* @property {number | null | undefined} [column]
* @property {number | null | undefined} [offset]
*/
/**
* Get the starting point of `node`.
*
* @param node
* Node.
* @returns
* Point.
*/
export const pointStart = point('start')
/**
* Get the ending point of `node`.
*
* @param node
* Node.
* @returns
* Point.
*/
export const pointEnd = point('end')
/**
* Get the positional info of `node`.
*
* @param {NodeLike | Node | null | undefined} [node]
* Node.
* @returns {Position}
* Position.
*/
export function position(node) {
return {start: pointStart(node), end: pointEnd(node)}
}
/**
* Get the positional info of `node`.
*
* @param {'start' | 'end'} type
* Side.
* @returns
* Getter.
*/
function point(type) {
return point
/**
* Get the point info of `node` at a bound side.
*
* @param {NodeLike | Node | null | undefined} [node]
* @returns {Point}
*/
function point(node) {
const point = (node && node.position && node.position[type]) || {}
// To do: next major: dont return points when invalid.
return {
// @ts-expect-error: in practice, null is allowed.
line: point.line || null,
// @ts-expect-error: in practice, null is allowed.
column: point.column || null,
// @ts-expect-error: in practice, null is allowed.
offset: point.offset > -1 ? point.offset : null
}
}
}