Skip to content

Commit

Permalink
Add support for getting a whole, valid, position
Browse files Browse the repository at this point in the history
Closes GH-6.
  • Loading branch information
wooorm committed Feb 16, 2020
1 parent 16ff667 commit 2c3407e
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 39 deletions.
13 changes: 10 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
41 changes: 25 additions & 16 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
74 changes: 54 additions & 20 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})

Expand Down

0 comments on commit 2c3407e

Please sign in to comment.