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 b73aa7a commit 488b6ce
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 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
36 changes: 34 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
/**
* @typedef {Object} Polarity
* @property {number} polarity
* @property {number} positivity
* @property {number} negativity
* @property {Array.<string>} positive
* @property {Array.<string>} negative
*
* @typedef {Object.<string, number>} Inject
*/

import {afinn165} from 'afinn-165'
import {emoji} from './emoji.js'

Expand All @@ -8,14 +19,25 @@ var own = {}.hasOwnProperty
inject(afinn165)
inject(emoji)

/**
* Get a polarity result from given values, optionally with one time injections.
*
* @param {Array.<string>} values
* @param {Inject} inject
* @returns {Polarity}
*/
export function polarity(values, inject) {
var words = values || []
var index = words.length === 0 ? 1 : words.length
var positivity = 0
var negativity = 0
/** @type {Array.<string>} */
var positive = []
/** @type {Array.<string>} */
var negative = []
/** @type {string} */
var value
/** @type {number} */
var weight

while (index--) {
Expand Down Expand Up @@ -44,8 +66,13 @@ export function polarity(values, inject) {
}
}

// Inject values on the `polarities` object.
/**
* Inject values on the `polarities` object.
*
* @param {Inject} values
*/
export function inject(values) {
/** @type {string} */
var value

for (value in values) {
Expand All @@ -55,7 +82,12 @@ export function inject(values) {
}
}

// Get the polarity of a word.
/**
* Get the polarity of a word.
*
* @param {string} value
* @param {Inject} inject
*/
function getPolarity(value, inject) {
if (own.call(polarities, value)) {
return polarities[value]
Expand Down
15 changes: 14 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,39 @@
"sideEffects": false,
"type": "module",
"main": "index.js",
"types": "index.d.ts",
"files": [
"index.d.ts",
"index.js",
"emoji.js"
],
"dependencies": {
"afinn-165": "^2.0.0"
},
"devDependencies": {
"@types/tape": "^4.0.0",
"c8": "^7.0.0",
"emoji-emotion": "^3.0.0",
"gemoji": "^7.0.0",
"nyc": "^15.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",
"tinyify": "^3.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",
"generate": "node build",
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"test-api": "node test/index.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test/index.js",
"test": "npm run generate && npm run format && npm run test-coverage"
"test": "npm run generate && npm run build && npm run format && npm run test-coverage"
},
"prettier": {
"tabWidth": 2,
Expand All @@ -75,5 +83,10 @@
"plugins": [
"preset-wooorm"
]
},
"typeCoverage": {
"atLeast": 100,
"detail": true,
"strict": true
}
}
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 488b6ce

Please sign in to comment.