textlint-util-to-string
Advanced tools
Comparing version 2.0.2 to 2.1.0
@@ -43,3 +43,3 @@ // LICENSE : MIT | ||
intermediate: [start, end] | ||
// generaged value = "Str" | ||
// generated value = "Str" | ||
// e.g.) [0, 3] | ||
@@ -71,3 +71,4 @@ generated : [start, end] | ||
* @param generatedPosition | ||
* @returns {Object} | ||
* @param {boolean} isEnd - is the position end of the node? | ||
* @returns {Object} | ||
*/ | ||
@@ -77,4 +78,4 @@ | ||
key: "originalPositionFor", | ||
value: function originalPositionFor(generatedPosition) { | ||
return this.originalPositionFromPosition(generatedPosition); | ||
value: function originalPositionFor(generatedPosition, isEnd) { | ||
return this.originalPositionFromPosition(generatedPosition, isEnd); | ||
} | ||
@@ -85,2 +86,3 @@ | ||
* @param {number} generatedIndex - position is a index value. | ||
* @param {boolean} isEnd - is the position end of the node? | ||
* @returns {number|undefined} original | ||
@@ -94,2 +96,4 @@ */ | ||
var isEnd = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; | ||
var hitTokenMaps = this.tokenMaps.filter(function (tokenMap, index) { | ||
@@ -100,3 +104,3 @@ var generated = tokenMap.generated; | ||
if (nextGenerated) { | ||
if (generated[0] <= generatedIndex && generatedIndex < nextGenerated[0]) { | ||
if (generated[0] <= generatedIndex && generatedIndex <= nextGenerated[0]) { | ||
return true; | ||
@@ -113,15 +117,25 @@ } | ||
} | ||
// a bcd | ||
// b = index 1 | ||
// original `a` bcd | ||
// originalRange [3, 7] | ||
// adjustedStart = 1 | ||
// b's index = 3 + 1 | ||
var hitTokenMap = hitTokenMaps[0]; | ||
// <----------->\[<------------->|text] | ||
/** | ||
* **Str**ABC | ||
* | | ||
* | | ||
* generatedIndex | ||
* | ||
* If isEnd is true, generatedIndex is end of **Str** node. | ||
* If isEnd is false, generatedIndex is index of ABC node. | ||
*/ | ||
var hitTokenMap = isEnd ? hitTokenMaps[0] : hitTokenMaps[hitTokenMaps.length - 1]; | ||
// <----------->[<------------->|text] | ||
// ^ ^ | ||
// position-generated intermediate-origin | ||
var outAdjust = generatedIndex - hitTokenMap.generated[0]; | ||
var inAdjust = hitTokenMap.intermediate[0] - hitTokenMap.original[0]; | ||
return outAdjust + inAdjust + hitTokenMap.original[0]; | ||
// <-------------->[<------------->|text] | ||
// | | | ||
// outer adjust _ | ||
// inner adjust = 1 | ||
var outerAdjust = generatedIndex - hitTokenMap.generated[0]; | ||
var innerAdjust = hitTokenMap.intermediate[0] - hitTokenMap.original[0]; | ||
return outerAdjust + innerAdjust + hitTokenMap.original[0]; | ||
} | ||
@@ -132,2 +146,3 @@ | ||
* @param {object} position | ||
* @param {boolean} isEnd - is the position end of the node? | ||
* @returns {object} original position | ||
@@ -139,2 +154,4 @@ */ | ||
value: function originalPositionFromPosition(position) { | ||
var isEnd = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; | ||
if (typeof position.line === "undefined" || typeof position.column === "undefined") { | ||
@@ -148,4 +165,4 @@ throw new Error("position.{line, column} should not undefined: " + JSON.stringify(position)); | ||
} | ||
var originalIndex = this.originalIndexFromIndex(generatedIndex); | ||
return this.originalSource.indexToPosition(originalIndex); | ||
var originalIndex = this.originalIndexFromIndex(generatedIndex, isEnd); | ||
return this.originalSource.indexToPosition(originalIndex, isEnd); | ||
} | ||
@@ -156,2 +173,3 @@ | ||
* @param {object} generatedPosition | ||
* @param {boolean} isEnd - is the position end of the node? | ||
* @returns {number} original index | ||
@@ -163,4 +181,6 @@ */ | ||
value: function originalIndexFromPosition(generatedPosition) { | ||
var isEnd = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; | ||
var originalPosition = this.originalPositionFromPosition(generatedPosition); | ||
return this.originalSource.positionToIndex(originalPosition); | ||
return this.originalSource.positionToIndex(originalPosition, isEnd); | ||
} | ||
@@ -171,2 +191,3 @@ | ||
* @param {number} generatedIndex | ||
* @param {boolean} isEnd - is the position end of the node? | ||
* @return {object} original position | ||
@@ -178,4 +199,6 @@ */ | ||
value: function originalPositionFromIndex(generatedIndex) { | ||
var isEnd = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; | ||
var originalIndex = this.originalIndexFromIndex(generatedIndex); | ||
return this.originalSource.indexToPosition(originalIndex); | ||
return this.originalSource.indexToPosition(originalIndex, isEnd); | ||
} | ||
@@ -182,0 +205,0 @@ }, { |
@@ -14,3 +14,3 @@ { | ||
}, | ||
"version": "2.0.2", | ||
"version": "2.1.0", | ||
"description": "textlint util convert Paragraph Node to text with SourceMap.", | ||
@@ -26,3 +26,3 @@ "main": "lib/index.js", | ||
"scripts": { | ||
"build": "babel src --out-dir lib --source-maps", | ||
"build": "NODE_ENV=production babel src --out-dir lib --source-maps", | ||
"watch": "babel src --out-dir lib --watch --source-maps", | ||
@@ -35,6 +35,7 @@ "prepublish": "npm run --if-present build", | ||
"babel-preset-es2015": "^6.1.18", | ||
"espower-babel": "^4.0.0", | ||
"markdown-to-ast": "^3.1.1", | ||
"babel-preset-power-assert": "^1.0.0", | ||
"babel-register": "^6.24.1", | ||
"markdown-to-ast": "^4.0.0", | ||
"mocha": "^3.0.2", | ||
"power-assert": "^1.2.0", | ||
"power-assert": "^1.4.2", | ||
"sentence-splitter": "^2.0.0" | ||
@@ -41,0 +42,0 @@ }, |
@@ -17,4 +17,3 @@ # textlint-util-to-string | ||
Please pay extra attention to the `column` property of `position` as it is 0-based index | ||
and defferent from that of `textlint` which is 1-based index. | ||
**Note**: the `column` property of `position` as it is **0-based** index. | ||
@@ -21,0 +20,0 @@ ## Usage |
@@ -5,2 +5,3 @@ // LICENSE : MIT | ||
import StructuredSource from "structured-source"; | ||
export default class StringSource { | ||
@@ -25,3 +26,3 @@ constructor(node) { | ||
intermediate: [start, end] | ||
// generaged value = "Str" | ||
// generated value = "Str" | ||
// e.g.) [0, 3] | ||
@@ -48,6 +49,8 @@ generated : [start, end] | ||
* @param generatedPosition | ||
* @param {boolean} isEnd - is the position end of the node? | ||
* @returns {Object} | ||
*/ | ||
originalPositionFor(generatedPosition) { | ||
return this.originalPositionFromPosition(generatedPosition); | ||
originalPositionFor(generatedPosition, isEnd) { | ||
return this.originalPositionFromPosition(generatedPosition, isEnd); | ||
} | ||
@@ -58,5 +61,6 @@ | ||
* @param {number} generatedIndex - position is a index value. | ||
* @param {boolean} isEnd - is the position end of the node? | ||
* @returns {number|undefined} original | ||
*/ | ||
originalIndexFromIndex(generatedIndex) { | ||
originalIndexFromIndex(generatedIndex, isEnd = false) { | ||
let hitTokenMaps = this.tokenMaps.filter((tokenMap, index) => { | ||
@@ -67,3 +71,3 @@ const generated = tokenMap.generated; | ||
if (nextGenerated) { | ||
if (generated[0] <= generatedIndex && generatedIndex < nextGenerated[0]) { | ||
if (generated[0] <= generatedIndex && generatedIndex <= nextGenerated[0]) { | ||
return true; | ||
@@ -80,15 +84,25 @@ } | ||
} | ||
// a bcd | ||
// b = index 1 | ||
// original `a` bcd | ||
// originalRange [3, 7] | ||
// adjustedStart = 1 | ||
// b's index = 3 + 1 | ||
let hitTokenMap = hitTokenMaps[0]; | ||
// <----------->\[<------------->|text] | ||
/** | ||
* **Str**ABC | ||
* | | ||
* | | ||
* generatedIndex | ||
* | ||
* If isEnd is true, generatedIndex is end of **Str** node. | ||
* If isEnd is false, generatedIndex is index of ABC node. | ||
*/ | ||
const hitTokenMap = isEnd ? hitTokenMaps[0] : hitTokenMaps[hitTokenMaps.length - 1]; | ||
// <----------->[<------------->|text] | ||
// ^ ^ | ||
// position-generated intermediate-origin | ||
let outAdjust = generatedIndex - hitTokenMap.generated[0]; | ||
let inAdjust = hitTokenMap.intermediate[0] - hitTokenMap.original[0]; | ||
return outAdjust + inAdjust + hitTokenMap.original[0]; | ||
// <-------------->[<------------->|text] | ||
// | | | ||
// outer adjust _ | ||
// inner adjust = 1 | ||
const outerAdjust = generatedIndex - hitTokenMap.generated[0]; | ||
const innerAdjust = hitTokenMap.intermediate[0] - hitTokenMap.original[0]; | ||
return outerAdjust + innerAdjust + hitTokenMap.original[0]; | ||
} | ||
@@ -99,9 +113,10 @@ | ||
* @param {object} position | ||
* @param {boolean} isEnd - is the position end of the node? | ||
* @returns {object} original position | ||
*/ | ||
originalPositionFromPosition(position) { | ||
originalPositionFromPosition(position, isEnd = false) { | ||
if (typeof position.line === "undefined" || typeof position.column === "undefined") { | ||
throw new Error("position.{line, column} should not undefined: " + JSON.stringify(position)); | ||
} | ||
var generatedIndex = this.generatedSource.positionToIndex(position); | ||
const generatedIndex = this.generatedSource.positionToIndex(position); | ||
if (isNaN(generatedIndex)) { | ||
@@ -111,4 +126,4 @@ // Not Found | ||
} | ||
let originalIndex = this.originalIndexFromIndex(generatedIndex); | ||
return this.originalSource.indexToPosition(originalIndex); | ||
const originalIndex = this.originalIndexFromIndex(generatedIndex, isEnd); | ||
return this.originalSource.indexToPosition(originalIndex, isEnd); | ||
} | ||
@@ -119,7 +134,8 @@ | ||
* @param {object} generatedPosition | ||
* @param {boolean} isEnd - is the position end of the node? | ||
* @returns {number} original index | ||
*/ | ||
originalIndexFromPosition(generatedPosition) { | ||
originalIndexFromPosition(generatedPosition, isEnd = false) { | ||
const originalPosition = this.originalPositionFromPosition(generatedPosition); | ||
return this.originalSource.positionToIndex(originalPosition); | ||
return this.originalSource.positionToIndex(originalPosition, isEnd); | ||
} | ||
@@ -130,7 +146,8 @@ | ||
* @param {number} generatedIndex | ||
* @param {boolean} isEnd - is the position end of the node? | ||
* @return {object} original position | ||
*/ | ||
originalPositionFromIndex(generatedIndex) { | ||
originalPositionFromIndex(generatedIndex, isEnd = false) { | ||
let originalIndex = this.originalIndexFromIndex(generatedIndex); | ||
return this.originalSource.indexToPosition(originalIndex); | ||
return this.originalSource.indexToPosition(originalIndex, isEnd); | ||
} | ||
@@ -137,0 +154,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
40409
522
8
106