Comparing version
@@ -47,3 +47,3 @@ /** | ||
*/ | ||
lineAt: function (index, options) { | ||
'lineAt': function (index, options) { | ||
// Fallback options | ||
@@ -77,3 +77,3 @@ options = options || {}; | ||
*/ | ||
columnAt: function (index) { | ||
'columnAt': function (index) { | ||
// Start at the index - 1 | ||
@@ -100,2 +100,37 @@ var input = this.input, | ||
return col; | ||
}, | ||
/** | ||
* Get the index of the character at a line and column | ||
* @param {Object} params Object containing line and column | ||
* @param {Number} params.line Line of character | ||
* @param {Number} params.column Column of character | ||
* @returns {Number} Index of character | ||
*/ | ||
'indexAt': function (params) { | ||
// Grab the parameters and lineMap | ||
var line = params.line, | ||
column = params.column, | ||
lineMap = this.lineMap; | ||
// Go to the nth line and get the start | ||
var retLine = lineMap[line], | ||
lineStart = retLine.start; | ||
// Add on the column to the line start and return | ||
var retVal = lineStart + column; | ||
return retVal; | ||
}, | ||
/** | ||
* Get the character at a line and column | ||
* @param {Object} params Object containing line and column | ||
* @param {Number} params.line Line of character | ||
* @param {Number} params.column Column of character | ||
* @returns {String} Character at specified location | ||
*/ | ||
'charAt': function (params) { | ||
// Get the index of the character, look it up, and return | ||
var index = this.indexAt(params), | ||
input = this.input, | ||
retVal = input.charAt(index); | ||
return retVal; | ||
} | ||
@@ -102,0 +137,0 @@ }; |
{ | ||
"name": "char-props", | ||
"description": "Retrieve line and column of a character at a given index.", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"homepage": "https://github.com/twolfson/char-props", | ||
@@ -29,3 +29,3 @@ "author": { | ||
"scripts": { | ||
"test": "vows" | ||
"test": "vows test/* --spec" | ||
}, | ||
@@ -32,0 +32,0 @@ "devDependencies": { |
@@ -37,2 +37,20 @@ # char-props | ||
*/ | ||
// Indexer.indexAt JSDoc | ||
/** | ||
* Get the index of the character at a line and column | ||
* @param {Object} params Object containing line and column | ||
* @param {Number} params.line Line of character | ||
* @param {Number} params.column Column of character | ||
* @returns {Number} Index of character | ||
*/ | ||
// Indexer.charAt JSDoc | ||
/** | ||
* Get the character at a line and column | ||
* @param {Object} params Object containing line and column | ||
* @param {Number} params.line Line of character | ||
* @param {Number} params.column Column of character | ||
* @returns {String} Character at specified location | ||
*/ | ||
``` | ||
@@ -46,3 +64,3 @@ | ||
## Contributing | ||
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using vows. | ||
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint your code via [grunt](http://gruntjs.com/) and test via [vows](http://vowsjs.org/). | ||
@@ -49,0 +67,0 @@ ## License |
@@ -45,2 +45,13 @@ var vows = require('vows'), | ||
assert.strictEqual(col, 8, 'The character at index 35 is in the ninth column'); | ||
}, | ||
'can find the index of a character at a given column and line': function (indexer) { | ||
var location = { | ||
'line': 2, | ||
'column': 8 | ||
}; | ||
// Grab the index of our locaiton | ||
var index = indexer.indexAt(location); | ||
assert.strictEqual(index, 35, 'The character at line 2 and column 8 is index 35'); | ||
} | ||
@@ -68,2 +79,13 @@ } | ||
assert.strictEqual(line, 3, 'The character at index 42 is on the fourth line'); | ||
}, | ||
'can get the character at a given column and line': function (indexer) { | ||
var location = { | ||
'line': 3, | ||
'column': 6 | ||
}; | ||
// Grab the character at our locaiton | ||
var char = indexer.charAt(location); | ||
assert.strictEqual(char, 'o', 'The character at line 3 and column 6 is "o"'); | ||
} | ||
@@ -70,0 +92,0 @@ } |
12643
24.89%257
24.76%68
36%