mdast-range
Advanced tools
Comparing version 0.2.1 to 0.3.0
@@ -6,2 +6,11 @@ --- | ||
0.3.0 / 2015-05-25 | ||
================== | ||
* Add `offsetToPosition()` to reverse offset into position ([935de29](https://github.com/wooorm/mdast-range/commit/935de29)) | ||
* Fix coverage ([a350066](https://github.com/wooorm/mdast-range/commit/a350066)) | ||
* Update mdast-github ([1773b85](https://github.com/wooorm/mdast-range/commit/1773b85)) | ||
* Update eslint ([81ae48e](https://github.com/wooorm/mdast-range/commit/81ae48e)) | ||
* Update mdast ([dc43c62](https://github.com/wooorm/mdast-range/commit/dc43c62)) | ||
0.2.1 / 2015-05-06 | ||
@@ -8,0 +17,0 @@ ================== |
45
index.js
@@ -58,2 +58,41 @@ 'use strict'; | ||
/** | ||
* Factory to reverse an offset into a line--column | ||
* tuple. | ||
* | ||
* @param {Array.<number>} offsets - Offsets, as returned | ||
* by `toOffsets()`. | ||
* @return {Function} - Bound method. | ||
*/ | ||
function offsetToPositionFactory(offsets) { | ||
/** | ||
* Calculate offsets for `lines`. | ||
* | ||
* @param {number} offset - Offset. | ||
* @return {Object} - Object with `line` and `colymn` | ||
* properties based on the bound `offsets`. | ||
*/ | ||
function offsetToPosition(offset) { | ||
var index = -1; | ||
var length = offsets.length; | ||
if (offset < 0) { | ||
return {}; | ||
} | ||
while (++index < length) { | ||
if (offsets[index] > offset) { | ||
return { | ||
'line': index + 1, | ||
'column': (offset - offsets[index - 1] || 0) + 1 | ||
}; | ||
} | ||
} | ||
return {}; | ||
} | ||
return offsetToPosition; | ||
} | ||
/** | ||
* Add ranges for `doc` to `ast`. | ||
@@ -82,2 +121,8 @@ * | ||
/* | ||
* Expose `offsetToPosition`. | ||
*/ | ||
file.offsetToPosition = offsetToPositionFactory(contents); | ||
/* | ||
* Add `offset` on both `start` and `end`. | ||
@@ -84,0 +129,0 @@ */ |
{ | ||
"name": "mdast-range", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "Range information to mdast", | ||
@@ -23,7 +23,7 @@ "license": "MIT", | ||
"peerDependencies": { | ||
"mdast": ">=0.17.0" | ||
"mdast": ">=0.21.0" | ||
}, | ||
"devDependencies": { | ||
"browserify": "^10.0.0", | ||
"eslint": "^0.20.0", | ||
"eslint": "^0.21.0", | ||
"esmangle": "^1.0.0", | ||
@@ -33,4 +33,4 @@ "istanbul": "^0.3.0", | ||
"jscs-jsdoc": "^1.0.0", | ||
"mdast": "^0.19.0", | ||
"mdast-github": "^0.2.0", | ||
"mdast": "^0.21.0", | ||
"mdast-github": "^0.3.0", | ||
"mdast-toc": "^0.3.0", | ||
@@ -43,4 +43,5 @@ "mdast-usage": "^0.2.0", | ||
"test-api": "mocha --check-leaks test.js", | ||
"test-coverage": "istanbul cover _mocha --report lcovonly -- --check-leaks test.js", | ||
"test-travis": "npm run test-coverage", | ||
"test-coverage": "istanbul cover _mocha -- --check-leaks test.js", | ||
"test-coveralls": "istanbul cover _mocha --report lcovonly -- --check-leaks test.js", | ||
"test-travis": "npm run test-coveralls", | ||
"test": "npm run test-api", | ||
@@ -47,0 +48,0 @@ "lint-api": "eslint .", |
@@ -86,3 +86,4 @@ # mdast-range [![Build Status](https://img.shields.io/travis/wooorm/mdast-range.svg?style=flat)](https://travis-ci.org/wooorm/mdast-range) [![Coverage Status](https://img.shields.io/coveralls/wooorm/mdast-range.svg?style=flat)](https://coveralls.io/r/wooorm/mdast-range?branch=master) | ||
"offset": 5 | ||
} | ||
}, | ||
"indent": [] | ||
} | ||
@@ -106,3 +107,4 @@ }, | ||
"offset": 14 | ||
} | ||
}, | ||
"indent": [] | ||
} | ||
@@ -121,3 +123,4 @@ } | ||
"offset": 15 | ||
} | ||
}, | ||
"indent": [] | ||
} | ||
@@ -138,3 +141,6 @@ }, | ||
"offset": 17 | ||
} | ||
}, | ||
"indent": [ | ||
1 | ||
] | ||
} | ||
@@ -158,3 +164,4 @@ }, | ||
"offset": 29 | ||
} | ||
}, | ||
"indent": [] | ||
} | ||
@@ -173,3 +180,4 @@ } | ||
"offset": 31 | ||
} | ||
}, | ||
"indent": [] | ||
} | ||
@@ -190,3 +198,6 @@ }, | ||
"offset": 37 | ||
} | ||
}, | ||
"indent": [ | ||
1 | ||
] | ||
} | ||
@@ -207,3 +218,4 @@ }, | ||
"offset": 43 | ||
} | ||
}, | ||
"indent": [] | ||
} | ||
@@ -224,3 +236,4 @@ }, | ||
"offset": 44 | ||
} | ||
}, | ||
"indent": [] | ||
} | ||
@@ -239,3 +252,7 @@ } | ||
"offset": 44 | ||
} | ||
}, | ||
"indent": [ | ||
1, | ||
1 | ||
] | ||
} | ||
@@ -310,4 +327,13 @@ } | ||
To reverse an `offset` into a position, pass it into `file.offsetToPosition()`: | ||
```javascript | ||
mdast.use(range).process('foo', function (err, doc, file) { | ||
file.offsetToPosition(0); | ||
// Yields: `{line: 1, column: 1}` | ||
}); | ||
``` | ||
## License | ||
[MIT](LICENSE) © [Titus Wormer](http://wooorm.com) |
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
14087
128
329