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/index.js b/index.js index 95eb4bc..73bbe72 100644 --- a/index.js +++ b/index.js @@ -1,10 +1,19 @@ +/** + * @typedef {Object} SmogFormulaCounts + * @property {number} sentence + * @property {number} [polysillabicWord] + */ + var sentenceSize = 30 var weight = 1.043 var base = 3.1291 -// Get the grade level of a given value according to the SMOG formula. -// More information is available at WikiPedia: -// +/** + * Get the grade level of a given value according to the SMOG formula. More information is available at WikiPedia: . + * + * @param {SmogFormulaCounts} counts + * @returns {number} + */ export function smogFormula(counts) { if (!counts || !counts.sentence) { return Number.NaN diff --git a/package.json b/package.json index 479fad5..31ee13c 100644 --- a/package.json +++ b/package.json @@ -25,22 +25,30 @@ "sideEffects": false, "type": "module", "main": "index.js", + "types": "index.d.ts", "files": [ + "index.d.ts", "index.js" ], "devDependencies": { + "@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" }, "prettier": { "tabWidth": 2, @@ -61,5 +69,10 @@ "plugins": [ "preset-wooorm" ] + }, + "typeCoverage": { + "atLeast": 100, + "detail": true, + "strict": true } } diff --git a/test.js b/test.js index 4f67b7f..f7e815b 100644 --- a/test.js +++ b/test.js @@ -2,12 +2,17 @@ import test from 'tape' import {smogFormula} from './index.js' test('smogFormula', function (t) { + // @ts-ignore runtime. t.ok(Number.isNaN(smogFormula()), 'NaN when an invalid value is given') t.equal(round(smogFormula({sentence: 1})), 3.1291) t.equal(round(smogFormula({sentence: 1, polysillabicWord: 4})), 14.554593) t.end() }) +/** + * @param {number} value + * @returns {number} + */ function round(value) { return Math.round(value * 1e6) / 1e6 } 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 + } +}