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 26, 2021
1 parent e00c379 commit afe81e3
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 8 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
15 changes: 14 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,33 @@
"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",
"gemoji": "^7.0.0",
"markdown-table": "^3.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",
"generate": "node script/build-data && node script/build-support",
"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 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 Down Expand Up @@ -75,5 +83,10 @@
false
]
]
},
"typeCoverage": {
"atLeast": 100,
"detail": true,
"strict": true
}
}
29 changes: 22 additions & 7 deletions script/build-data.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import fs from 'fs'
import {gemoji} from 'gemoji'

var schema = JSON.parse(fs.readFileSync('schema.json'))
var alias = JSON.parse(fs.readFileSync('alias.json'))
/** @type {Object.<string, string[]>} */
var schema = JSON.parse(String(fs.readFileSync('schema.json')))
/** @type {Object.<string, string | string[]>} */
var alias = JSON.parse(String(fs.readFileSync('alias.json')))

var own = {}.hasOwnProperty

Expand All @@ -16,7 +18,9 @@ var data = Object.keys(schema)
}
})
.map(function (ctx) {
/** @type {string[]|string[][]} */
var structure = ctx.structure
/** @type {string[]} */
var result

structure = structure.map(function (key) {
Expand Down Expand Up @@ -59,7 +63,9 @@ var data = Object.keys(schema)

// Detect if emoticons are classified multiple times.
var known = {}
/** @type {{name: string, emoji: string, tags: string[], description: string, emoticons: string[]}} */
var info
/** @type {string} */
var emoticon

for (info of data) {
Expand All @@ -83,9 +89,15 @@ fs.writeFileSync(
'export var emoticon = ' + JSON.stringify(data, null, 2) + '\n'
)

/**
* @param {string[]|string[][]} value
*/
function unpack(value) {
/** @type {string[]} */
var result = []
/** @type {string} */
var first
/** @type {string} */
var second

for (first of value[0]) {
Expand All @@ -97,17 +109,20 @@ function unpack(value) {
return result
}

// Flatten facial parts.
/**
* Flatten facial parts.
* @param {string|string[]} keys
*/
function flatten(keys) {
/** @type {string[]} */
var result = []
var index = -1
var length = keys.length

while (++index < length) {
while (++index < keys.length) {
if (own.call(alias, keys[index])) {
result = result.concat(flatten(alias[keys[index]]))
result.push(...flatten(alias[keys[index]]))
} else if (Array.isArray(keys[index])) {
result = result.concat(keys[index])
result.push(...keys[index])
} else {
result.push(keys[index])
}
Expand Down
4 changes: 4 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ test('emoticon', function (t) {
t.ok(Array.isArray(emoticon), 'should be an array')

t.doesNotThrow(function () {
/** @type {{name: string, emoji: string, tags: string[], description: string, emoticons: string[]}} */
var info

for (info of emoticon) {
Expand All @@ -14,6 +15,7 @@ test('emoticon', function (t) {
}, 'each entry should have an `emoji` string field')

t.doesNotThrow(function () {
/** @type {{name: string, emoji: string, tags: string[], description: string, emoticons: string[]}} */
var info

for (info of emoticon) {
Expand All @@ -26,6 +28,7 @@ test('emoticon', function (t) {
}, 'each entry should have an `description` string field')

t.doesNotThrow(function () {
/** @type {{name: string, emoji: string, tags: string[], description: string, emoticons: string[]}} */
var info

for (info of emoticon) {
Expand All @@ -34,6 +37,7 @@ test('emoticon', function (t) {
}, 'each entry should have an `tags` array field')

t.doesNotThrow(function () {
/** @type {{name: string, emoji: string, tags: string[], description: string, emoticons: string[]}} */
var info

for (info of emoticon) {
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", "script/build-data.js", "script/build-support.js"],
"include": ["{script/,}*.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 afe81e3

Please sign in to comment.