Skip to content

Commit 2e0420d

Browse files
committed
Change parameter order
1 parent 6eeed6c commit 2e0420d

File tree

3 files changed

+26
-27
lines changed

3 files changed

+26
-27
lines changed

lib/index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,17 @@ import {location} from 'vfile-location'
2424

2525
const search = /\r?\n|\r/g
2626

27-
// To do: change parameter order, to allow `value` to be missing.
2827
/**
2928
* Get the source of a node or at a position.
3029
*
31-
* @param {Node | NodeLike | Position | PositionLike | null | undefined} value
32-
* Value to get.
3330
* @param {VFile | VFileValue} file
3431
* File in which `value` exists.
32+
* @param {Node | NodeLike | Position | PositionLike | null | undefined} value
33+
* Value to get.
3534
* @returns {string | undefined}
3635
* Source of `value` in `doc`, if available.
3736
*/
38-
export function source(value, file) {
37+
export function source(file, value) {
3938
const doc = String(file)
4039
const loc = location(file)
4140
const position =

readme.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* [Install](#install)
1818
* [Use](#use)
1919
* [API](#api)
20-
* [`source(value, file)`](#sourcevalue-file)
20+
* [`source(file[, value])`](#sourcefile-value)
2121
* [Types](#types)
2222
* [Compatibility](#compatibility)
2323
* [Contribute](#contribute)
@@ -67,15 +67,15 @@ Say our document `example.md` contains:
6767
…and our module `example.js` looks as follows:
6868

6969
```js
70-
import {read} from 'to-vfile'
7170
import {fromMarkdown} from 'mdast-util-from-markdown'
71+
import {read} from 'to-vfile'
7272
import {source} from 'unist-util-source'
7373

7474
const file = await read('example.md')
7575
const tree = fromMarkdown(String(file))
7676

7777
const strong = tree.children[0].children[0].children[0].children[0].children[0]
78-
console.log(source(strong, file))
78+
console.log(source(file, strong))
7979
```
8080

8181
…now running `node example.js` yields:
@@ -89,16 +89,16 @@ console.log(source(strong, file))
8989
This package exports the identifier [`source`][source].
9090
There is no default export.
9191

92-
### `source(value, file)`
92+
### `source(file[, value])`
9393

9494
Get the source of a node or at a position.
9595

9696
###### Parameters
9797

98-
* `value` ([`Node`][node] or [`Position`][position])
99-
— value to get
10098
* `file` ([`VFile`][vfile] or `string`)
10199
— file in which `value` exists
100+
* `value` ([`Node`][node], [`Position`][position], optional)
101+
— value to get
102102

103103
###### Returns
104104

@@ -189,4 +189,4 @@ abide by its terms.
189189

190190
[vfile]: https://github.com/vfile/vfile
191191

192-
[source]: #sourcevalue-file
192+
[source]: #sourcefile-value

test.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,42 +26,42 @@ test('source', async function (t) {
2626
assert(text.type === 'text')
2727

2828
await t.test('should support a root', async function () {
29-
assert.equal(source(tree, file), '> + **[Hello](./example)**\n> world.')
29+
assert.equal(source(file, tree), '> + **[Hello](./example)**\n> world.')
3030
})
3131

3232
await t.test('should support a block quote', async function () {
3333
assert.equal(
34-
source(blockquote, file),
34+
source(file, blockquote),
3535
'> + **[Hello](./example)**\n> world.'
3636
)
3737
})
3838

3939
await t.test('should support a list', async function () {
40-
assert.equal(source(list, file), '+ **[Hello](./example)**\n> world.')
40+
assert.equal(source(file, list), '+ **[Hello](./example)**\n> world.')
4141
})
4242

4343
await t.test('should support a list item', async function () {
44-
assert.equal(source(listItem, file), '+ **[Hello](./example)**\n> world.')
44+
assert.equal(source(file, listItem), '+ **[Hello](./example)**\n> world.')
4545
})
4646

4747
await t.test('should support a paragraph', async function () {
48-
assert.equal(source(paragraph, file), '**[Hello](./example)**\n> world.')
48+
assert.equal(source(file, paragraph), '**[Hello](./example)**\n> world.')
4949
})
5050

5151
await t.test('should support strong', async function () {
52-
assert.equal(source(strong, file), '**[Hello](./example)**')
52+
assert.equal(source(file, strong), '**[Hello](./example)**')
5353
})
5454

5555
await t.test('should support a link', async function () {
56-
assert.equal(source(link, file), '[Hello](./example)')
56+
assert.equal(source(file, link), '[Hello](./example)')
5757
})
5858

5959
await t.test('should support a text', async function () {
60-
assert.equal(source(text, file), 'Hello')
60+
assert.equal(source(file, text), 'Hello')
6161
})
6262

6363
await t.test('should support a position', async function () {
64-
assert.equal(source(text.position, file), 'Hello')
64+
assert.equal(source(file, text.position), 'Hello')
6565
})
6666

6767
await t.test('should support out of bounds data', async function () {
@@ -70,44 +70,44 @@ test('source', async function (t) {
7070
value: 'qwe',
7171
position: {start: {line: 0, column: 0}, end: {line: 0, column: 0}}
7272
}
73-
assert.equal(source(text, file), undefined)
73+
assert.equal(source(file, text), undefined)
7474
})
7575

7676
await t.test('should support a generated node', async function () {
7777
const text = {type: 'text', value: 'qwe'}
78-
assert.equal(source(text, file), undefined)
78+
assert.equal(source(file, text), undefined)
7979
})
8080

8181
await t.test('should support a nullish node', async function () {
82-
assert.equal(source(null, file), undefined)
82+
assert.equal(source(file, null), undefined)
8383
})
8484

8585
await t.test('should support cr + lf', async function () {
8686
const file = new VFile('a\r\nb')
8787
const node = fromMarkdown(String(file))
8888
const paragraph = node.children[0]
89-
assert.equal(source(paragraph, file), 'a\r\nb')
89+
assert.equal(source(file, paragraph), 'a\r\nb')
9090
})
9191

9292
await t.test('should support cr', async function () {
9393
const file = new VFile('a\rb')
9494
const node = fromMarkdown(String(file))
9595
const paragraph = node.children[0]
9696

97-
assert.equal(source(paragraph, file), 'a\rb')
97+
assert.equal(source(file, paragraph), 'a\rb')
9898
})
9999

100100
await t.test('should support an eof eol', async function () {
101101
const file = new VFile('a\n')
102102
const node = fromMarkdown(String(file))
103103

104-
assert.equal(source(node, file), 'a\n')
104+
assert.equal(source(file, node), 'a\n')
105105
})
106106

107107
await t.test('should support an lf + cr (a blank line)', async function () {
108108
const file = new VFile('a\n\rb')
109109
const node = fromMarkdown(String(file))
110110

111-
assert.equal(source(node, file), 'a\n\rb')
111+
assert.equal(source(file, node), 'a\n\rb')
112112
})
113113
})

0 commit comments

Comments
 (0)