From 43d215c5c80b04cb8addc3b3083a96dc7006d06d Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Tue, 6 Apr 2021 14:25:07 +0200 Subject: [PATCH] Add JSDoc based types --- .gitignore | 1 + cli.js | 4 +++- index.js | 18 ++++++++++++++---- package.json | 17 ++++++++++++++++- test.js | 1 + tsconfig.json | 15 +++++++++++++++ 6 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore index 735f4af..c977c85 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .DS_Store +*.d.ts *.log coverage/ node_modules/ diff --git a/cli.js b/cli.js index 2dea1d4..ee0570e 100755 --- a/cli.js +++ b/cli.js @@ -3,6 +3,7 @@ import {URL} from 'url' import fs from 'fs' import {doubleMetaphone} from './index.js' +/** @type {Object.} */ var pack = JSON.parse( String(fs.readFileSync(new URL('./package.json', import.meta.url))) ) @@ -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) diff --git a/index.js b/index.js index 4cb3aab..165e83f 100644 --- a/index.js +++ b/index.js @@ -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.} */ var characters value = String(value).toUpperCase() + ' ' @@ -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' diff --git a/package.json b/package.json index bda4729..84b4e6b 100644 --- a/package.json +++ b/package.json @@ -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, @@ -61,6 +70,7 @@ "xo": { "prettier": true, "rules": { + "complexity": "off", "no-var": "off", "prefer-arrow-callback": "off" } @@ -69,5 +79,10 @@ "plugins": [ "preset-wooorm" ] + }, + "typeCoverage": { + "atLeast": 100, + "detail": true, + "strict": true } } diff --git a/test.js b/test.js index 3a45bcc..c3745fb 100644 --- a/test.js +++ b/test.js @@ -6,6 +6,7 @@ import {PassThrough} from 'stream' import {doubleMetaphone as m} from './index.js' import test from 'tape' +/** @type {Object.} */ var pack = JSON.parse( String(fs.readFileSync(new URL('./package.json', import.meta.url))) ) diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..be08abe --- /dev/null +++ b/tsconfig.json @@ -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 + } +}