Socket
Socket
Sign inDemoInstall

vfile-location

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vfile-location - npm Package Compare versions

Comparing version 5.0.2 to 5.0.3

lib/index.d.ts.map

49

index.d.ts

@@ -1,2 +0,47 @@

export { location } from "./lib/index.js";
export type Location = import('./lib/index.js').Location;
import type {Point as UnistPoint} from 'unist'
export {location} from './lib/index.js'
/**
* Accessors for index.
*/
export interface Location {
/**
* Get the `offset` from a line/column based `Point` in the bound indices;
* returns `undefined` when given out of bounds input.
*
* @param point
* Line/column based `Point`.
* @returns
* Offset.
*/
toOffset(point?: PointLike | null | undefined): number | undefined
/**
* Get the line/column based `Point` for `offset` in the bound indices;
* returns `undefined` when given out of bounds input.
*
* @param offset
* Offset.
* @returns
* `Point`.
*/
toPoint(offset?: number | null | undefined): UnistPoint | undefined
}
/**
* Point from `unist`, allowed as input.
*/
interface PointLike {
/**
* Column.
*/
column?: number | null | undefined
/**
* Line.
*/
line?: number | null | undefined
/**
* Offset.
*/
offset?: number | null | undefined
}

5

index.js

@@ -1,5 +0,2 @@

/**
* @typedef {import('./lib/index.js').Location} Location
*/
// Note: types exposed from `index.d.ts`.
export {location} from './lib/index.js'
/**
* @import {VFile, Value} from 'vfile'
* @import {Location} from 'vfile-location'
*/
/**
* Create an index of the given document to translate between line/column and

@@ -15,48 +19,5 @@ * offset based positional info.

export function location(file: VFile | Value): Location;
export type VFile = import('vfile').VFile;
export type Value = import('vfile').Value;
export type UnistPoint = import('unist').Point;
/**
* unist point, allowed as input.
*/
export type PointLike = {
/**
* Line.
*/
line?: number | null | undefined;
/**
* Column.
*/
column?: number | null | undefined;
/**
* Offset.
*/
offset?: number | null | undefined;
};
/**
* Get the line/column based `Point` for `offset` in the bound indices.
*
* Returns `undefined` when given out of bounds input.
*
* Also implemented in Rust in [`wooorm/markdown-rs`][markdown-rs].
*
* [markdown-rs]: https://github.com/wooorm/markdown-rs/blob/main/src/util/location.rs
*/
export type ToPoint = (offset?: number | null | undefined) => UnistPoint | undefined;
/**
* Get the `offset` from a line/column based `Point` in the bound indices.
*/
export type ToOffset = (point?: PointLike | null | undefined) => number | undefined;
/**
* Accessors for index.
*/
export type Location = {
/**
* Get the line/column based `Point` for `offset` in the bound indices.
*/
toPoint: ToPoint;
/**
* Get the `offset` from a line/column based `Point` in the bound indices.
*/
toOffset: ToOffset;
};
import type { VFile } from 'vfile';
import type { Value } from 'vfile';
import type { Location } from 'vfile-location';
//# sourceMappingURL=index.d.ts.map
/**
* @typedef {import('vfile').VFile} VFile
* @typedef {import('vfile').Value} Value
* @typedef {import('unist').Point} UnistPoint
* @import {VFile, Value} from 'vfile'
* @import {Location} from 'vfile-location'
*/
/**
*
* @typedef PointLike
* unist point, allowed as input.
* @property {number | null | undefined} [line]
* Line.
* @property {number | null | undefined} [column]
* Column.
* @property {number | null | undefined} [offset]
* Offset.
*
* @callback ToPoint
* Get the line/column based `Point` for `offset` in the bound indices.
*
* Returns `undefined` when given out of bounds input.
*
* Also implemented in Rust in [`wooorm/markdown-rs`][markdown-rs].
*
* [markdown-rs]: https://github.com/wooorm/markdown-rs/blob/main/src/util/location.rs
* @param {number | null | undefined} [offset]
* Something that should be an `offset.
* @returns {UnistPoint | undefined}
* Point, if `offset` is valid and in-bounds input.
*
* @callback ToOffset
* Get the `offset` from a line/column based `Point` in the bound indices.
* @param {PointLike | null | undefined} [point]
* Something that should be a `point.
* @returns {number | undefined}
* Offset (`number`) or `undefined` for invalid or out of bounds input.
*
* @typedef Location
* Accessors for index.
* @property {ToPoint} toPoint
* Get the line/column based `Point` for `offset` in the bound indices.
* @property {ToOffset} toOffset
* Get the `offset` from a line/column based `Point` in the bound indices.
*/
const search = /\r?\n|\r/g
/**
* Create an index of the given document to translate between line/column and

@@ -71,23 +29,19 @@ * offset based positional info.

search.lastIndex = 0
return {toOffset, toPoint}
while (search.test(value)) {
indices.push(search.lastIndex)
}
/** @type {Location['toPoint']} */
function toPoint(offset) {
if (typeof offset === 'number' && offset > -1 && offset <= value.length) {
let index = 0
indices.push(value.length + 1)
while (true) {
let end = indices[index]
return {toPoint, toOffset}
if (end === undefined) {
const eol = next(value, indices[index - 1])
end = eol === -1 ? value.length + 1 : eol + 1
indices[index] = end
}
/** @type {ToPoint} */
function toPoint(offset) {
let index = -1
if (
typeof offset === 'number' &&
offset > -1 &&
offset < indices[indices.length - 1]
) {
while (++index < indices.length) {
if (indices[index] > offset) {
if (end > offset) {
return {

@@ -99,2 +53,4 @@ line: index + 1,

}
index++
}

@@ -104,21 +60,37 @@ }

/** @type {ToOffset} */
/** @type {Location['toOffset']} */
function toOffset(point) {
const line = point && point.line
const column = point && point.column
if (
typeof line === 'number' &&
typeof column === 'number' &&
!Number.isNaN(line) &&
!Number.isNaN(column) &&
line - 1 in indices
point &&
typeof point.line === 'number' &&
typeof point.column === 'number' &&
!Number.isNaN(point.line) &&
!Number.isNaN(point.column)
) {
const offset = (indices[line - 2] || 0) + column - 1 || 0
while (indices.length < point.line) {
const from = indices[indices.length - 1]
const eol = next(value, from)
const end = eol === -1 ? value.length + 1 : eol + 1
if (from === end) break
indices.push(end)
}
if (offset > -1 && offset < indices[indices.length - 1]) {
return offset
}
const offset =
(point.line > 1 ? indices[point.line - 2] : 0) + point.column - 1
// The given `column` could not exist on this line.
if (offset < indices[point.line - 1]) return offset
}
}
}
/**
* @param {string} value
* @param {number} from
*/
function next(value, from) {
const cr = value.indexOf('\r', from)
const lf = value.indexOf('\n', from)
if (lf === -1) return cr
if (cr === -1 || cr + 1 === lf) return lf
return cr < lf ? cr : lf
}
{
"name": "vfile-location",
"version": "5.0.2",
"version": "5.0.3",
"description": "vfile utility to convert between positional (line and column-based) and offset (range-based) locations",

@@ -46,9 +46,10 @@ "license": "MIT",

"@types/node": "^20.0.0",
"c8": "^8.0.0",
"c8": "^10.0.0",
"prettier": "^3.0.0",
"remark-cli": "^11.0.0",
"remark-preset-wooorm": "^9.0.0",
"remark-api": "^1.0.0",
"remark-cli": "^12.0.0",
"remark-preset-wooorm": "^10.0.0",
"type-coverage": "^2.0.0",
"typescript": "^5.0.0",
"xo": "^0.56.0"
"xo": "^0.58.0"
},

@@ -73,3 +74,4 @@ "scripts": {

"plugins": [
"remark-preset-wooorm"
"remark-preset-wooorm",
"remark-api"
]

@@ -84,4 +86,30 @@ },

"xo": {
"overrides": [
{
"files": [
"**/*.d.ts"
],
"rules": {
"@typescript-eslint/array-type": [
"error",
{
"default": "generic"
}
],
"@typescript-eslint/ban-types": [
"error",
{
"extendDefaults": true
}
],
"@typescript-eslint/consistent-type-definitions": [
"error",
"interface"
]
}
}
],
"prettier": true,
"rules": {
"no-constant-condition": "off",
"unicorn/prefer-at": "off"

@@ -88,0 +116,0 @@ }

@@ -16,13 +16,13 @@ # vfile-location

* [What is this?](#what-is-this)
* [When should I use this?](#when-should-i-use-this)
* [Install](#install)
* [Use](#use)
* [API](#api)
* [`location(file)`](#locationfile)
* [`Location`](#location)
* [Types](#types)
* [Compatibility](#compatibility)
* [Contribute](#contribute)
* [License](#license)
* [What is this?](#what-is-this)
* [When should I use this?](#when-should-i-use-this)
* [Install](#install)
* [Use](#use)
* [API](#api)
* [`Location`](#location)
* [`location(file)`](#locationfile)
* [Types](#types)
* [Compatibility](#compatibility)
* [Contribute](#contribute)
* [License](#license)

@@ -78,5 +78,15 @@ ## What is this?

This package exports the identifier [`location`][api-location].
There is no default export.
### `Location`
Accessors for index.
###### Fields
* `toOffset` (`(point?: PointLike | null | undefined) => number | undefined`)
— get the `offset` from a line/column based `Point` in the bound indices;
returns `undefined` when given out of bounds input
* `toPoint` (`(offset?: number | null | undefined) => UnistPoint | undefined`)
— get the line/column based `Point` for `offset` in the bound indices;
returns `undefined` when given out of bounds input
### `location(file)`

@@ -87,24 +97,15 @@

Also implemented in Rust in [`wooorm/markdown-rs`][markdown-rs].
[markdown-rs]: https://github.com/wooorm/markdown-rs/blob/main/src/util/location.rs
###### Parameters
* `file` ([`VFile`][vfile], `Buffer`, or `string`)
— file to index
* `file` (`VFile | Value`)
— file to index
###### Returns
Accessors for index ([`Location`][api-location-map]).
Accessors for index (`Location`).
### `Location`
Accessors for index (TypeScript type).
###### Fields
* `toPoint` (`(offset: number) => Point | undefined`)
— get the line/column based [`Point`][point] for `offset` in the bound
indices
* `toOffset` (`(point: Point) => number | undefined`)
— get the `offset` from a line/column based [`Point`][point] in the bound
indices
## Types

@@ -189,6 +190,2 @@

[point]: https://github.com/syntax-tree/unist#point
[api-location]: #locationfile
[api-location-map]: #location
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc