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

51 lines
1.1 KiB
JavaScript

/**
* @typedef {import('unist').Position} Position
* @typedef {import('unist').Node} Node
* @typedef {Record<string, unknown> & {type: string, position?: PositionLike|undefined}} NodeLike
* @typedef {import('unist').Point} Point
*
* @typedef {Partial<Point>} PointLike
*
* @typedef PositionLike
* @property {PointLike} [start]
* @property {PointLike} [end]
*/
export const pointStart = point('start')
export const pointEnd = point('end')
/**
* Get the positional info of `node`.
*
* @param {NodeLike|Node} [node]
* @returns {Position}
*/
export function position(node) {
return {start: pointStart(node), end: pointEnd(node)}
}
/**
* Get the positional info of `node`.
*
* @param {'start'|'end'} type
*/
function point(type) {
return point
/**
* Get the positional info of `node`.
*
* @param {NodeLike|Node} [node]
* @returns {Point}
*/
function point(node) {
const point = (node && node.position && node.position[type]) || {}
return {
line: point.line || null,
column: point.column || null,
offset: point.offset > -1 ? point.offset : null
}
}
}