Skip to content

Commit 8deb741

Browse files
committed
Use ESM
1 parent dc028c0 commit 8deb741

File tree

6 files changed

+74
-78
lines changed

6 files changed

+74
-78
lines changed

.gitignore

-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
.DS_Store
22
*.log
3-
.nyc_output/
43
coverage/
54
node_modules/
6-
nlcst-affix-emoticon-modifier.js
7-
nlcst-affix-emoticon-modifier.min.js
85
yarn.lock

.prettierignore

-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
11
coverage/
2-
nlcst-affix-emoticon-modifier.js
3-
nlcst-affix-emoticon-modifier.min.js
4-
*.json
52
*.md

index.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
'use strict'
1+
import {modifyChildren} from 'unist-util-modify-children'
22

3-
var modifier = require('unist-util-modify-children')
4-
5-
module.exports = modifier(mergeAffixEmoticon)
3+
export const affixEmoticonModifier = modifyChildren(mergeAffixEmoticon)
64

75
// Merge emoticons into an `EmoticonNode`.
86
function mergeAffixEmoticon(child, index, parent) {
97
var siblings = parent.children
108
var children = child.children
119
var childIndex = -1
1210

13-
if (children && children.length && index) {
11+
if (children && children.length > 0 && index) {
1412
while (++childIndex < children.length) {
1513
if (children[childIndex].type === 'EmoticonNode') {
1614
siblings[index - 1].children = [].concat(

package.json

+14-23
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,19 @@
2222
"contributors": [
2323
"Titus Wormer <[email protected]> (https://wooorm.com)"
2424
],
25+
"sideEffects": false,
26+
"type": "module",
27+
"main": "index.js",
2528
"files": [
2629
"index.js"
2730
],
2831
"dependencies": {
29-
"unist-util-modify-children": "^1.0.0"
32+
"unist-util-modify-children": "^3.0.0"
3033
},
3134
"devDependencies": {
32-
"browserify": "^17.0.0",
33-
"nlcst-emoji-modifier": "^4.0.0",
34-
"nlcst-emoticon-modifier": "^1.0.0",
35+
"c8": "^7.0.0",
36+
"nlcst-emoji-modifier": "^5.0.0",
37+
"nlcst-emoticon-modifier": "^2.0.0",
3538
"nyc": "^15.0.0",
3639
"prettier": "^2.0.0",
3740
"remark-cli": "^9.0.0",
@@ -40,23 +43,14 @@
4043
"tape": "^5.0.0",
4144
"tinyify": "^3.0.0",
4245
"unified": "^9.0.0",
43-
"unist-util-remove-position": "^3.0.0",
44-
"xo": "^0.38.0"
46+
"unist-util-remove-position": "^4.0.0",
47+
"xo": "^0.39.0"
4548
},
4649
"scripts": {
4750
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
48-
"build-bundle": "browserify . -s nlcstAffixEmoticonModifier -o nlcst-affix-emoticon-modifier.js",
49-
"build-mangle": "browserify . -s nlcstAffixEmoticonModifier -o nlcst-affix-emoticon-modifier.min.js -p tinyify",
50-
"build": "npm run build-bundle && npm run build-mangle",
51-
"test-api": "node test",
52-
"test-coverage": "nyc --reporter lcov tape test/index.js",
53-
"test": "npm run format && npm run build && npm run test-coverage"
54-
},
55-
"nyc": {
56-
"check-coverage": true,
57-
"lines": 100,
58-
"functions": 100,
59-
"branches": 100
51+
"test-api": "node test/index.js",
52+
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test/index.js",
53+
"test": "npm run format && npm run test-coverage"
6054
},
6155
"prettier": {
6256
"tabWidth": 2,
@@ -68,12 +62,9 @@
6862
},
6963
"xo": {
7064
"prettier": true,
71-
"esnext": false,
72-
"ignore": [
73-
"nlcst-affix-emoticon-modifier.js"
74-
],
7565
"rules": {
76-
"unicorn/explicit-length-check": "off"
66+
"no-var": "off",
67+
"prefer-arrow-callback": "off"
7768
}
7869
},
7970
"remarkConfig": {

readme.md

+37-29
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ the previous sentence, after a terminal marker. :unamused:
1616
1717
## Install
1818

19+
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
20+
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
21+
1922
[npm][]:
2023

2124
```sh
@@ -25,47 +28,52 @@ npm install nlcst-affix-emoticon-modifier
2528
## Use
2629

2730
```js
28-
var affixEmoticon = require('nlcst-affix-emoticon-modifier')
29-
var emoticon = require('nlcst-emoticon-modifier')
30-
var inspect = require('unist-util-inspect')
31-
var english = require('parse-english')()
31+
import {affixEmoticonModifier} from 'nlcst-affix-emoticon-modifier'
32+
import {emoticonModifier} from 'nlcst-emoticon-modifier'
33+
import {inspect} from 'unist-util-inspect'
34+
import {ParseEnglish} from 'parse-english'
35+
36+
var english = new ParseEnglish()
3237

33-
english.useFirst('tokenizeSentence', emoticon)
34-
english.useFirst('tokenizeParagraph', affixEmoticon)
38+
english.useFirst('tokenizeSentence', emoticonModifier)
39+
english.useFirst('tokenizeParagraph', affixEmoticonModifier)
3540

3641
console.log(inspect(english.parse('Hey. :) How is it going?')))
3742
```
3843

3944
Yields:
4045

4146
```txt
42-
RootNode[1]
43-
└─ ParagraphNode[3]
44-
├─ SentenceNode[4]
45-
├─ WordNode[1]
46-
│ │ └─ TextNode: 'Hey'
47-
├─ PunctuationNode: '.'
48-
├─ WhiteSpaceNode: ' '
49-
└─ EmoticonNode: ':)'
50-
├─ WhiteSpaceNode: ' '
51-
└─ SentenceNode[8]
52-
├─ WordNode[1]
53-
└─ TextNode: 'How'
54-
├─ WhiteSpaceNode: ' '
55-
├─ WordNode[1]
56-
└─ TextNode: 'is'
57-
├─ WhiteSpaceNode: ' '
58-
├─ WordNode[1]
59-
└─ TextNode: 'it'
60-
├─ WhiteSpaceNode: ' '
61-
├─ WordNode[1]
62-
└─ TextNode: 'going'
63-
└─ PunctuationNode: '?'
47+
RootNode[1] (1:1-1:25, 0-24)
48+
└─0 ParagraphNode[3] (1:1-1:25, 0-24)
49+
├─0 SentenceNode[4] (1:1-1:8, 0-7)
50+
├─0 WordNode[1] (1:1-1:4, 0-3)
51+
└─0 TextNode "Hey" (1:1-1:4, 0-3)
52+
├─1 PunctuationNode "." (1:4-1:5, 3-4)
53+
├─2 WhiteSpaceNode " " (1:5-1:6, 4-5)
54+
└─3 EmoticonNode ":)" (1:6-1:8, 5-7)
55+
├─1 WhiteSpaceNode " " (1:8-1:9, 7-8)
56+
└─2 SentenceNode[8] (1:9-1:25, 8-24)
57+
├─0 WordNode[1] (1:9-1:12, 8-11)
58+
└─0 TextNode "How" (1:9-1:12, 8-11)
59+
├─1 WhiteSpaceNode " " (1:12-1:13, 11-12)
60+
├─2 WordNode[1] (1:13-1:15, 12-14)
61+
└─0 TextNode "is" (1:13-1:15, 12-14)
62+
├─3 WhiteSpaceNode " " (1:15-1:16, 14-15)
63+
├─4 WordNode[1] (1:16-1:18, 15-17)
64+
└─0 TextNode "it" (1:16-1:18, 15-17)
65+
├─5 WhiteSpaceNode " " (1:18-1:19, 17-18)
66+
├─6 WordNode[1] (1:19-1:24, 18-23)
67+
└─0 TextNode "going" (1:19-1:24, 18-23)
68+
└─7 PunctuationNode "?" (1:24-1:25, 23-24)
6469
```
6570

6671
## API
6772

68-
### `affixEmoticon(paragraph)`
73+
This package exports the following identifiers: `affixEmoticonModifier`.
74+
There is no default export.
75+
76+
### `affixEmoticonModifier(paragraph)`
6977

7078
Merge affix emoticons (`EmoticonNode`) into the previous sentence.
7179

test/index.js

+20-15
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
1-
'use strict'
1+
import fs from 'fs'
2+
import path from 'path'
3+
import test from 'tape'
4+
import unified from 'unified'
5+
import english from 'retext-english'
6+
import {emojiModifier} from 'nlcst-emoji-modifier'
7+
import {emoticonModifier} from 'nlcst-emoticon-modifier'
8+
import {removePosition} from 'unist-util-remove-position'
9+
import {affixEmoticonModifier} from '../index.js'
210

3-
var test = require('tape')
4-
var unified = require('unified')
5-
var english = require('retext-english')
6-
var emojiModifier = require('nlcst-emoji-modifier')
7-
var emoticonModifier = require('nlcst-emoticon-modifier')
8-
var remove = require('unist-util-remove-position')
11+
var lollipop = JSON.parse(
12+
String(fs.readFileSync(path.join('test', 'fixtures', 'lollipop.json')))
13+
)
914

10-
var lollipop = require('./fixtures/lollipop')
11-
var smile = require('./fixtures/smile')
12-
var modifier = require('..')
15+
var smile = JSON.parse(
16+
String(fs.readFileSync(path.join('test', 'fixtures', 'smile.json')))
17+
)
1318

14-
test('nlcst-affix-emoticon-modifier()', function (t) {
19+
test('affixEmoticonModifier()', function (t) {
1520
t.throws(
1621
function () {
17-
modifier({})
22+
affixEmoticonModifier({})
1823
},
1924
/Missing children in `parent`/,
2025
'should throw when not given a parent'
@@ -28,7 +33,7 @@ test('nlcst-affix-emoticon-modifier()', function (t) {
2833

2934
t.deepEqual(
3035
process('Lol! :lollipop: That’s cool.', true),
31-
remove(lollipop, true),
36+
removePosition(lollipop, true),
3237
'should merge at sentence-start (1, positionless)'
3338
)
3439

@@ -40,7 +45,7 @@ test('nlcst-affix-emoticon-modifier()', function (t) {
4045

4146
t.deepEqual(
4247
process('Lol! :) That’s cool.', true),
43-
remove(smile, true),
48+
removePosition(smile, true),
4449
'should merge at sentence-start (2, positionless)'
4550
)
4651

@@ -62,5 +67,5 @@ function process(fixture, positionless) {
6267
function plugin() {
6368
this.Parser.prototype.useFirst('tokenizeSentence', emojiModifier)
6469
this.Parser.prototype.useFirst('tokenizeSentence', emoticonModifier)
65-
this.Parser.prototype.useFirst('tokenizeParagraph', modifier)
70+
this.Parser.prototype.useFirst('tokenizeParagraph', affixEmoticonModifier)
6671
}

0 commit comments

Comments
 (0)