Skip to content

Commit

Permalink
Use ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Apr 6, 2021
1 parent b9ab459 commit c77a646
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 45 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
.DS_Store
*.log
.nyc_output/
coverage/
node_modules/
double-metaphone.js
double-metaphone.min.js
yarn.lock
2 changes: 0 additions & 2 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
coverage/
double-metaphone.js
double-metaphone.min.js
*.md
13 changes: 8 additions & 5 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
#!/usr/bin/env node
'use strict'
import {URL} from 'url'
import fs from 'fs'
import {doubleMetaphone} from './index.js'

var pack = require('./package.json')
var doubleMetaphone = require('.')
var pack = JSON.parse(
String(fs.readFileSync(new URL('./package.json', import.meta.url)))
)

var argv = process.argv.slice(2)

if (argv.indexOf('--help') !== -1 || argv.indexOf('-h') !== -1) {
if (argv.includes('--help') || argv.includes('-h')) {
console.log(help())
} else if (argv.indexOf('--version') !== -1 || argv.indexOf('-v') !== -1) {
} else if (argv.includes('--version') || argv.includes('-v')) {
console.log(pack.version)
} else if (argv.length === 0) {
process.stdin.resume()
Expand Down
6 changes: 1 addition & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
'use strict'

module.exports = doubleMetaphone

// Match vowels (including `Y`).
var vowels = /[AEIOUY]/

Expand Down Expand Up @@ -50,7 +46,7 @@ var dutchSch = /E[DMNR]|UY|OO/

// Get the phonetics according to the Double Metaphone algorithm from a value.
// eslint-disable-next-line complexity
function doubleMetaphone(value) {
export function doubleMetaphone(value) {
var primary = ''
var secondary = ''
var index = 0
Expand Down
28 changes: 11 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,27 @@
"contributors": [
"Titus Wormer <[email protected]> (https://wooorm.com)"
],
"sideEffects": false,
"type": "module",
"bin": "cli.js",
"main": "index.js",
"files": [
"index.js",
"cli.js"
],
"bin": "cli.js",
"main": "index.js",
"devDependencies": {
"browserify": "^17.0.0",
"nyc": "^15.0.0",
"c8": "^7.0.0",
"prettier": "^2.0.0",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"tape": "^5.0.0",
"tinyify": "^3.0.0",
"xo": "^0.38.0"
},
"scripts": {
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"build-bundle": "browserify . -s doubleMetaphone -o double-metaphone.js",
"build-mangle": "browserify . -s doubleMetaphone -p tinyify -o double-metaphone.min.js",
"build": "npm run build-bundle && npm run build-mangle",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test.js",
"test": "npm run format && npm run build && npm run test-coverage"
"test-api": "node test.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
"test": "npm run format && npm run test-coverage"
},
"nyc": {
"check-coverage": true,
Expand All @@ -63,13 +60,10 @@
},
"xo": {
"prettier": true,
"esnext": false,
"rules": {
"unicorn/prefer-includes": "off"
},
"ignores": [
"double-metaphone.js"
]
"no-var": "off",
"prefer-arrow-callback": "off"
}
},
"remarkConfig": {
"plugins": [
Expand Down
12 changes: 9 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

## Install

This package is ESM only: Node 12+ is needed to use it and it must be `import`ed
instead of `require`d.

[npm][]:

```sh
Expand All @@ -17,8 +20,11 @@ npm install double-metaphone

## API

This package exports the following identifiers: `doubleMetaphone`.
There is no default export.

```js
var doubleMetaphone = require('double-metaphone')
import {doubleMetaphone} from 'double-metaphone'

doubleMetaphone('michael') // => ['MKL', 'MXL']
doubleMetaphone('crevalle') // => ['KRFL', 'KRF']
Expand All @@ -32,8 +38,8 @@ doubleMetaphone('allegrettos') // => ['ALKRTS', 'AKRTS']
With [stemmer][]:

```js
var doubleMetaphone = require('double-metaphone')
var stemmer = require('stemmer')
import {doubleMetaphone} from 'double-metaphone'
import {stemmer} from 'stemmer'

doubleMetaphone(stemmer('acceptingness')) // => [ 'AKSPTNK', 'AKSPTNK' ]
doubleMetaphone(stemmer('allegrettos')) // => [ 'ALKRT', 'AKRT' ]
Expand Down
23 changes: 13 additions & 10 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
'use strict'

var exec = require('child_process').exec
var PassThrough = require('stream').PassThrough
var assert = require('assert')
var test = require('tape')
var version = require('./package.json').version
var m = require('.')
import assert from 'assert'
import {exec} from 'child_process'
import fs from 'fs'
import {URL} from 'url'
import {PassThrough} from 'stream'
import {doubleMetaphone as m} from './index.js'
import test from 'tape'

var pack = JSON.parse(
String(fs.readFileSync(new URL('./package.json', import.meta.url)))
)

test('api', function (t) {
t.equal(typeof m, 'function', 'should be a `function`')
Expand Down Expand Up @@ -825,12 +828,12 @@ test('cli', function (t) {
})

exec('./cli.js -v', function (error, stdout, stderr) {
t.deepEqual([error, stdout, stderr], [null, version + '\n', ''], '-v')
t.deepEqual([error, stdout, stderr], [null, pack.version + '\n', ''], '-v')
})
exec('./cli.js --version', function (error, stdout, stderr) {
t.deepEqual(
[error, stdout, stderr],
[null, version + '\n', ''],
[null, pack.version + '\n', ''],
'--version'
)
})
Expand Down

0 comments on commit c77a646

Please sign in to comment.