Skip to content

Commit d892083

Browse files
committed
Refactor to improve bundle size
1 parent 4271155 commit d892083

File tree

2 files changed

+19
-26
lines changed

2 files changed

+19
-26
lines changed

index.js

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,12 @@
22

33
var visit = require('unist-util-visit')
44

5-
module.exports = getDefinitionFactory
5+
module.exports = createGetDefinition
66

77
var own = {}.hasOwnProperty
88

99
// Get a definition in `node` by `identifier`.
10-
function getDefinitionFactory(node, options) {
11-
return getterFactory(gather(node, options))
12-
}
13-
14-
// Gather all definitions in `node`
15-
function gather(node) {
10+
function createGetDefinition(node) {
1611
var cache = {}
1712

1813
if (!node || !node.type) {
@@ -21,27 +16,22 @@ function gather(node) {
2116

2217
visit(node, 'definition', ondefinition)
2318

24-
return cache
19+
return getDefinition
2520

2621
function ondefinition(definition) {
27-
var id = normalise(definition.identifier)
28-
if (!own.call(cache, id)) {
22+
var id = clean(definition.identifier)
23+
if (id && !own.call(cache, id)) {
2924
cache[id] = definition
3025
}
3126
}
32-
}
33-
34-
// Factory to get a node from the given definition-cache.
35-
function getterFactory(cache) {
36-
return getter
3727

3828
// Get a node from the bound definition-cache.
39-
function getter(identifier) {
40-
var id = identifier && normalise(identifier)
29+
function getDefinition(identifier) {
30+
var id = clean(identifier)
4131
return id && own.call(cache, id) ? cache[id] : null
4232
}
4333
}
4434

45-
function normalise(identifier) {
46-
return identifier.toUpperCase()
35+
function clean(value) {
36+
return String(value || '').toUpperCase()
4737
}

test.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ var remark = require('remark')
55
var definitions = require('.')
66

77
test('mdast-util-definitions', function (t) {
8-
var getDefinition
98
var tree
109

1110
t.throws(
@@ -17,10 +16,9 @@ test('mdast-util-definitions', function (t) {
1716
)
1817

1918
tree = remark().parse('[example]: https://example.com "Example"')
20-
getDefinition = definitions(tree)
2119

2220
t.deepLooseEqual(
23-
getDefinition('example'),
21+
definitions(tree)('example'),
2422
{
2523
type: 'definition',
2624
identifier: 'example',
@@ -35,13 +33,12 @@ test('mdast-util-definitions', function (t) {
3533
'should return a definition'
3634
)
3735

38-
t.equal(getDefinition('foo'), null, 'should return null when not found')
36+
t.equal(definitions(tree)('foo'), null, 'should return null when not found')
3937

4038
tree = remark().parse('[__proto__]: https://proto.com "Proto"')
41-
getDefinition = definitions(tree)
4239

4340
t.deepLooseEqual(
44-
getDefinition('__proto__'),
41+
definitions(tree)('__proto__'),
4542
{
4643
type: 'definition',
4744
identifier: '__proto__',
@@ -60,7 +57,7 @@ test('mdast-util-definitions', function (t) {
6057
t.equal({}.type, undefined, 'should not polute the prototype')
6158

6259
t.deepEqual(
63-
getDefinition('toString'),
60+
definitions(tree)('toString'),
6461
null,
6562
'should work on weird identifiers when not found'
6663
)
@@ -75,5 +72,11 @@ test('mdast-util-definitions', function (t) {
7572
'should prefer the first of duplicate definitions'
7673
)
7774

75+
t.deepEqual(
76+
definitions(tree)(''),
77+
null,
78+
'should not return something for a missing identifier'
79+
)
80+
7881
t.end()
7982
})

0 commit comments

Comments
 (0)