Skip to content

Commit

Permalink
Add JSDoc based types
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Apr 6, 2021
1 parent c77a646 commit 43d215c
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
*.d.ts
*.log
coverage/
node_modules/
Expand Down
4 changes: 3 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {URL} from 'url'
import fs from 'fs'
import {doubleMetaphone} from './index.js'

/** @type {Object.<string, unknown>} */
var pack = JSON.parse(
String(fs.readFileSync(new URL('./package.json', import.meta.url)))
)
Expand All @@ -17,12 +18,13 @@ if (argv.includes('--help') || argv.includes('-h')) {
process.stdin.resume()
process.stdin.setEncoding('utf8')
process.stdin.on('data', function (data) {
console.log(phonetics(data))
console.log(phonetics(String(data)))
})
} else {
console.log(phonetics(argv.join(' ')))
}

/** @param {string} values */
function phonetics(values) {
return values
.split(/\s+/g)
Expand Down
18 changes: 14 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,31 @@ var hForS = /EIM|OEK|OLM|OLZ/
// or `SK`.
var dutchSch = /E[DMNR]|UY|OO/

// Get the phonetics according to the Double Metaphone algorithm from a value.
// eslint-disable-next-line complexity
/**
* Get the phonetics according to the Double Metaphone algorithm from a value.
*
* @param {string} value
* @returns {[string, string]}
*/
export function doubleMetaphone(value) {
var primary = ''
var secondary = ''
var index = 0
var length = value.length
var last = length - 1
/** @type {boolean} */
var isSlavoGermanic
/** @type {boolean} */
var isGermanic
/** @type {string} */
var subvalue
/** @type {string} */
var next
/** @type {string} */
var previous
/** @type {string} */
var nextnext
/** @type {Array.<string>} */
var characters

value = String(value).toUpperCase() + ' '
Expand Down Expand Up @@ -126,8 +137,7 @@ export function doubleMetaphone(value) {
nextnext !== 'I' &&
!vowels.test(characters[index - 2]) &&
(nextnext !== 'E' ||
(subvalue =
value.slice(index - 2, index + 4) &&
((subvalue = value.slice(index - 2, index + 4)) &&
(subvalue === 'BACHER' || subvalue === 'MACHER')))
) {
primary += 'K'
Expand Down
17 changes: 16 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,32 @@
"type": "module",
"bin": "cli.js",
"main": "index.js",
"types": "index.d.ts",
"files": [
"index.d.ts",
"index.js",
"cli.js"
],
"devDependencies": {
"@types/node": "^14.0.0",
"@types/tape": "^4.0.0",
"c8": "^7.0.0",
"prettier": "^2.0.0",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"rimraf": "^3.0.0",
"tape": "^5.0.0",
"type-coverage": "^2.0.0",
"typescript": "^4.0.0",
"xo": "^0.38.0"
},
"scripts": {
"prepack": "npm run build && npm run format",
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"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"
"test": "npm run build && npm run format && npm run test-coverage"
},
"nyc": {
"check-coverage": true,
Expand All @@ -61,6 +70,7 @@
"xo": {
"prettier": true,
"rules": {
"complexity": "off",
"no-var": "off",
"prefer-arrow-callback": "off"
}
Expand All @@ -69,5 +79,10 @@
"plugins": [
"preset-wooorm"
]
},
"typeCoverage": {
"atLeast": 100,
"detail": true,
"strict": true
}
}
1 change: 1 addition & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {PassThrough} from 'stream'
import {doubleMetaphone as m} from './index.js'
import test from 'tape'

/** @type {Object.<string, unknown>} */
var pack = JSON.parse(
String(fs.readFileSync(new URL('./package.json', import.meta.url)))
)
Expand Down
15 changes: 15 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"include": ["*.js"],
"compilerOptions": {
"target": "ES2020",
"lib": ["ES2020"],
"module": "ES2020",
"moduleResolution": "node",
"allowJs": true,
"checkJs": true,
"declaration": true,
"emitDeclarationOnly": true,
"allowSyntheticDefaultImports": true,
"skipLibCheck": true
}
}

0 comments on commit 43d215c

Please sign in to comment.