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 23, 2021
1 parent 08f972e commit e101488
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 42 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
23 changes: 19 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
// Get the text content of a node.
// Prefer the node’s plain-text fields, otherwise serialize its children,
// and if the given value is an array, serialize the nodes in it.
/**
* Get the text content of a node.
* Prefer the node’s plain-text fields, otherwise serialize its children,
* and if the given value is an array, serialize the nodes in it.
*
* @param {unknown} node
* @returns {string}
*/
export function toString(node) {
return (
(node &&
typeof node === 'object' &&
// @ts-ignore looks like a literal.
(node.value ||
// @ts-ignore looks like an image.
node.alt ||
// @ts-ignore looks like an image/link.
node.title ||
// @ts-ignore looks like a parent.
('children' in node && all(node.children)) ||
('length' in node && all(node)))) ||
(Array.isArray(node) && all(node)))) ||
''
)
}

/**
* @param {Array.<unknown>} values
* @returns {string}
*/
function all(values) {
/** @type {Array.<string>} */
var result = []
var index = -1

Expand Down
22 changes: 15 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,30 @@
"sideEffects": false,
"type": "module",
"main": "index.js",
"types": "types/index.d.ts",
"types": "index.d.ts",
"files": [
"types/index.d.ts",
"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.39.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,
Expand All @@ -59,14 +65,16 @@
"rules": {
"no-var": "off",
"prefer-arrow-callback": "off"
},
"ignore": [
"types"
]
}
},
"remarkConfig": {
"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
}
}
8 changes: 0 additions & 8 deletions types/index.d.ts

This file was deleted.

5 changes: 0 additions & 5 deletions types/test.ts

This file was deleted.

11 changes: 0 additions & 11 deletions types/tsconfig.json

This file was deleted.

7 changes: 0 additions & 7 deletions types/tslint.json

This file was deleted.

0 comments on commit e101488

Please sign in to comment.