diff --git a/index.js b/index.js index 6788791..89a3577 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,16 @@ 'use strict' -var position = exports +var start = factory('start') +var end = factory('end') -position.start = factory('start') -position.end = factory('end') +module.exports = position + +position.start = start +position.end = end + +function position(node) { + return {start: start(node), end: end(node)} +} function factory(type) { point.displayName = type diff --git a/readme.md b/readme.md index 31bfc3b..f438823 100644 --- a/readme.md +++ b/readme.md @@ -26,32 +26,39 @@ var position = require('unist-util-position') var tree = remark().parse('# foo\n\n* bar\n') -position.start(tree) // => {line: 1, column: 1} -position.end(tree) // => {line: 4, column: 1} +console.log(position(tree)) +console.log(position.start(tree)) +console.log(position.end(tree)) -position.start() // => {line: null, column: null} -position.end() // => {line: null, column: null} +console.log(position()) +console.log(position.start()) +console.log(position.end()) ``` -## API - -### `position.start([node])` +Yields: -### `position.end([node])` +```js +{start: {line: 1, column: 1, offset: 0}, end: {line: 4, column: 1, offset: 13}} +{line: 1, column: 1, offset: 0} +{line: 4, column: 1, offset: 13} +{start: {line: null, column: null, offset: null}, end: {line: null, column: null, offset: null}} +{line: null, column: null, offset: null} +{line: null, column: null, offset: null} +``` -Get the start or end points in the positional info of `node`. +## API -###### Parameters +### `position(node?)` -* `node` ([`Node?`][node]) — Node to check. +Get the positional info of `node` ([`Node?`][node]). +Returns [`Position`][position]. -###### Returns +### `position.start(node?)` -[`Point`][point] — Filled with `line` (nullable `uint32 >= 1`), -`column` (nullable `uint32 >= 1`), `offset` (nullable `uint32 >= 0`). +### `position.end(node?)` -Note that in [unist][], `line` and `column` are 1-indexed integers and -`offset` is a 0-indexed integer. +Get the start or end points in the positional info of `node` ([`Node?`][node]). +Returns [`Point`][point]. ## Contribute @@ -111,4 +118,6 @@ abide by its terms. [node]: https://github.com/syntax-tree/unist#node +[position]: https://github.com/syntax-tree/unist#position + [point]: https://github.com/syntax-tree/unist#point diff --git a/test.js b/test.js index 6dfe878..7175758 100644 --- a/test.js +++ b/test.js @@ -10,44 +10,78 @@ var properties = { } } -var objects = { - position: {start: {}, end: {}} -} +var noFields = {position: {start: {}, end: {}}} -var values = {position: {}} +var noPoints = {position: {}} -var none = {} +var noPosition = {} var generated = {line: null, column: null, offset: null} test('unist-util-position', function(t) { - ;['start', 'end'].forEach(function(type) { - t.test(type, function(st) { - var fn = position[type] + var sides = ['start', 'end'] + + t.test('position', function(t) { + t.same( + position(properties), + properties.position, + 'should get the whole position' + ) + + t.same( + position(noFields), + {start: generated, end: generated}, + 'should return an empty position without fields' + ) - st.same(fn(), generated, 'should not throw without node') + t.same( + position(noPoints), + {start: generated, end: generated}, + 'should return an empty position without points' + ) - st.same(fn(properties), properties.position[type], 'should get type') + t.same( + position(noPosition), + {start: generated, end: generated}, + 'should return an empty position without position' + ) - st.same( - fn(objects), + t.same( + position(), + {start: generated, end: generated}, + 'should return an empty position without node' + ) + + t.end() + }) + + sides.forEach(function(side) { + t.test('position.' + side, function(t) { + var fn = position[side] + + t.same(fn(properties), properties.position[side], 'should get a side') + + t.same( + fn(noFields), generated, - 'should return an empty object without objects' + 'should return an empty point without fields' ) - st.same( - fn(values), + t.same( + fn(noPoints), generated, - 'should return an empty object without values' + 'should return an empty point without points' ) - st.same( - fn(none), + t.same( + fn(noPosition), generated, - 'should return an empty object without position' + 'should return an empty point without position' ) - st.end() + t.same(fn(), generated, 'should return an empty point without node') + + t.end() }) })