vfile-location
Advanced tools
Comparing version 3.1.0 to 3.2.0
68
index.js
@@ -6,32 +6,30 @@ 'use strict' | ||
function factory(file) { | ||
var contents = indices(String(file)) | ||
var toPoint = offsetToPointFactory(contents) | ||
var value = String(file) | ||
var indices = [] | ||
var search = /\r?\n|\r/g | ||
while (search.exec(value)) { | ||
indices.push(search.lastIndex) | ||
} | ||
indices.push(value.length + 1) | ||
return { | ||
toPoint: toPoint, | ||
toPosition: toPoint, | ||
toOffset: pointToOffsetFactory(contents) | ||
toPoint: offsetToPoint, | ||
toPosition: offsetToPoint, | ||
toOffset: pointToOffset | ||
} | ||
} | ||
// Factory to get the line and column-based `point` for `offset` in the bound | ||
// indices. | ||
function offsetToPointFactory(indices) { | ||
return offsetToPoint | ||
// Get the line and column-based `point` for `offset` in the bound indices. | ||
function offsetToPoint(offset) { | ||
var index = -1 | ||
var length = indices.length | ||
if (offset < 0) { | ||
return {} | ||
} | ||
while (++index < length) { | ||
if (indices[index] > offset) { | ||
return { | ||
line: index + 1, | ||
column: offset - (indices[index - 1] || 0) + 1, | ||
offset: offset | ||
if (offset > -1 && offset < indices[indices.length - 1]) { | ||
while (++index < indices.length) { | ||
if (indices[index] > offset) { | ||
return { | ||
line: index + 1, | ||
column: offset - (indices[index - 1] || 0) + 1, | ||
offset: offset | ||
} | ||
} | ||
@@ -43,9 +41,3 @@ } | ||
} | ||
} | ||
// Factory to get the `offset` for a line and column-based `point` in the | ||
// bound indices. | ||
function pointToOffsetFactory(indices) { | ||
return pointToOffset | ||
// Get the `offset` for a line and column-based `point` in the bound | ||
@@ -56,24 +48,10 @@ // indices. | ||
var column = point && point.column | ||
var offset | ||
if (!isNaN(line) && !isNaN(column) && line - 1 in indices) { | ||
return (indices[line - 2] || 0) + column - 1 || 0 | ||
offset = (indices[line - 2] || 0) + column - 1 || 0 | ||
} | ||
return -1 | ||
return offset > -1 && offset < indices[indices.length - 1] ? offset : -1 | ||
} | ||
} | ||
// Get indices of line-breaks in `value`. | ||
function indices(value) { | ||
var result = [] | ||
var index = value.indexOf('\n') | ||
while (index !== -1) { | ||
result.push(index + 1) | ||
index = value.indexOf('\n', index + 1) | ||
} | ||
result.push(value.length + 1) | ||
return result | ||
} |
{ | ||
"name": "vfile-location", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"description": "vfile utility to convert between positional (line and column-based) and offset (range-based) locations", | ||
@@ -37,15 +37,15 @@ "license": "MIT", | ||
"@types/unist": "^2.0.0", | ||
"browserify": "^16.0.0", | ||
"dtslint": "^3.0.0", | ||
"browserify": "^17.0.0", | ||
"nyc": "^15.0.0", | ||
"prettier": "^2.0.0", | ||
"remark-cli": "^8.0.0", | ||
"remark-preset-wooorm": "^7.0.0", | ||
"remark-cli": "^9.0.0", | ||
"remark-preset-wooorm": "^8.0.0", | ||
"tape": "^5.0.0", | ||
"tinyify": "^3.0.0", | ||
"tsd": "^0.13.1", | ||
"vfile": "^4.0.0", | ||
"xo": "^0.33.0" | ||
"xo": "^0.34.0" | ||
}, | ||
"scripts": { | ||
"format": "remark . -qfo && prettier . --write && xo --fix", | ||
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix", | ||
"build-bundle": "browserify . -s vfileLocation > vfile-location.js", | ||
@@ -56,3 +56,3 @@ "build-mangle": "browserify . -s vfileLocation -p tinyify > vfile-location.min.js", | ||
"test-coverage": "nyc --reporter lcov tape test.js", | ||
"test-types": "dtslint types", | ||
"test-types": "tsd", | ||
"test": "npm run format && npm run build && npm run test-coverage && npm run test-types" | ||
@@ -59,0 +59,0 @@ }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8436
71