Skip to content

Commit 1a59cc6

Browse files
committed
Refactor to move implementation to lib/
1 parent 30a6ace commit 1a59cc6

File tree

3 files changed

+54
-52
lines changed

3 files changed

+54
-52
lines changed

index.js

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1 @@
1-
/**
2-
* @typedef {import('unist').Node} Node
3-
* @typedef {import('unist').Position} Position
4-
* @typedef {import('vfile').VFile} VFile
5-
* @typedef {import('vfile').VFileValue} VFileValue
6-
*/
7-
8-
import {location} from 'vfile-location'
9-
10-
const search = /\r?\n|\r/g
11-
12-
/**
13-
* Get the source of a node or at a position.
14-
*
15-
* @param {Node|Position} value
16-
* Value to get.
17-
* @param {VFile|VFileValue} file
18-
* File in which `value` exists.
19-
* @returns {string|null}
20-
* Source of `value` in `doc`, if available.
21-
*/
22-
export function source(value, file) {
23-
const doc = String(file)
24-
const loc = location(file)
25-
/** @type {import('unist').Position} */
26-
// @ts-expect-error Looks like a node.
27-
const position = (value && value.position) || value || {}
28-
const endOffset = loc.toOffset(position.end)
29-
let startOffset = loc.toOffset(position.start)
30-
31-
if (endOffset === -1 || startOffset === -1) {
32-
return null
33-
}
34-
35-
/** @type {Array.<string>} */
36-
const results = []
37-
38-
while (startOffset < endOffset) {
39-
search.lastIndex = startOffset
40-
const match = search.exec(doc)
41-
const end = match && match.index < endOffset ? match.index : endOffset
42-
results.push(doc.slice(startOffset, end))
43-
startOffset = end
44-
45-
if (match && match.index < endOffset) {
46-
startOffset += match[0].length
47-
results.push(match[0])
48-
}
49-
}
50-
51-
return results.join('')
52-
}
1+
export {source} from './lib/index.js'

lib/index.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @typedef {import('unist').Node} Node
3+
* @typedef {import('unist').Position} Position
4+
* @typedef {import('vfile').VFile} VFile
5+
* @typedef {import('vfile').VFileValue} VFileValue
6+
*/
7+
8+
import {location} from 'vfile-location'
9+
10+
const search = /\r?\n|\r/g
11+
12+
/**
13+
* Get the source of a node or at a position.
14+
*
15+
* @param {Node|Position} value
16+
* Value to get.
17+
* @param {VFile|VFileValue} file
18+
* File in which `value` exists.
19+
* @returns {string|null}
20+
* Source of `value` in `doc`, if available.
21+
*/
22+
export function source(value, file) {
23+
const doc = String(file)
24+
const loc = location(file)
25+
/** @type {import('unist').Position} */
26+
// @ts-expect-error Looks like a node.
27+
const position = (value && value.position) || value || {}
28+
const endOffset = loc.toOffset(position.end)
29+
let startOffset = loc.toOffset(position.start)
30+
31+
if (endOffset === -1 || startOffset === -1) {
32+
return null
33+
}
34+
35+
/** @type {Array.<string>} */
36+
const results = []
37+
38+
while (startOffset < endOffset) {
39+
search.lastIndex = startOffset
40+
const match = search.exec(doc)
41+
const end = match && match.index < endOffset ? match.index : endOffset
42+
results.push(doc.slice(startOffset, end))
43+
startOffset = end
44+
45+
if (match && match.index < endOffset) {
46+
startOffset += match[0].length
47+
results.push(match[0])
48+
}
49+
}
50+
51+
return results.join('')
52+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"main": "index.js",
2828
"types": "index.d.ts",
2929
"files": [
30+
"lib/",
3031
"index.d.ts",
3132
"index.js"
3233
],

0 commit comments

Comments
 (0)