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 Mar 16, 2021
1 parent 8c99b54 commit 3011eff
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 10 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
5 changes: 3 additions & 2 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import fs from 'fs'

import {direction} from './index.js'

var pack = JSON.parse(fs.readFileSync('package.json'))
/** @type {{[key: string]: unknown, version: string}} */
var pack = JSON.parse(String(fs.readFileSync('package.json')))

var argv = process.argv.slice(2)

Expand All @@ -15,7 +16,7 @@ if (argv.includes('--help') || argv.includes('-h')) {
process.stdin.resume()
process.stdin.setEncoding('utf8')
process.stdin.on('data', function (data) {
console.log(direction(data))
console.log(direction(String(data)))
})
} else {
console.log(direction(argv.join(' ')))
Expand Down
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ var ltrRange =
var rtl = new RegExp('^[^' + ltrRange + ']*[' + rtlRange + ']')
var ltr = new RegExp('^[^' + rtlRange + ']*[' + ltrRange + ']')

/**
* Detect direction.
*
* @param {string} value
* @returns {'rtl' | 'ltr' | 'neutral'}
*/
export function direction(value) {
var source = String(value || '')
return rtl.test(source) ? 'rtl' : ltr.test(source) ? 'ltr' : 'neutral'
Expand Down
21 changes: 14 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,31 @@
"type": "module",
"main": "index.js",
"bin": "cli.js",
"types": "index.d.ts",
"files": [
"index.d.ts",
"index.js",
"cli.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"
},
"nyc": {
"check-coverage": true,
"lines": 100,
"functions": 100,
"branches": 100
"test": "npm run build && npm run format && npm run test-coverage"
},
"prettier": {
"tabWidth": 2,
Expand All @@ -71,5 +73,10 @@
"plugins": [
"preset-wooorm"
]
},
"typeCoverage": {
"atLeast": 100,
"detail": true,
"strict": true
}
}
4 changes: 3 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {PassThrough} from 'stream'
import test from 'tape'
import {direction} from './index.js'

var pkg = JSON.parse(fs.readFileSync('package.json'))
/** @type {{[key: string]: unknown, version: string}} */
var pkg = JSON.parse(String(fs.readFileSync('package.json')))

var fixtures = [
{input: '0', output: 'neutral'},
Expand Down Expand Up @@ -33,6 +34,7 @@ var fixtures = [
test('api', function (t) {
var index = -1

// @ts-ignore
t.equal(direction(), 'neutral', 'should classify nullish as `neutral`')

while (++index < fixtures.length) {
Expand Down
16 changes: 16 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"files": ["index.js"],
"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 3011eff

Please sign in to comment.