Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 1, 2023
1 parent 0fdf89f commit 8aabe35
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 59 deletions.
9 changes: 2 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
/**
* @typedef {import('hast').Root} Root
* @typedef {import('hast').Content} Content
*/

/**
* @typedef {Root | Content} Node
* @typedef {import('hast').Nodes} Nodes
*/

const own = {}.hasOwnProperty
Expand Down Expand Up @@ -33,7 +28,7 @@ export function hasProperty(node, field) {

/**
* @param {unknown} value
* @returns {value is Node}
* @returns {value is Nodes}
*/
function isNode(value) {
return Boolean(value && typeof value === 'object' && 'type' in value)
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
"index.d.ts",
"index.js"
],
"dependencies": {
"@types/hast": "^3.0.0"
},
"devDependencies": {
"@types/node": "^20.0.0",
"c8": "^8.0.0",
Expand Down
110 changes: 58 additions & 52 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,73 @@
import assert from 'node:assert/strict'
import test from 'node:test'
import {hasProperty} from './index.js'
import * as mod from './index.js'

test('hasProperty', () => {
assert.deepEqual(
Object.keys(mod).sort(),
['hasProperty'],
'should expose the public api'
)
test('hasProperty', async function (t) {
await t.test('should expose the public api', async function () {
assert.deepEqual(Object.keys(await import('./index.js')).sort(), [
'hasProperty'
])
})

assert.equal(
hasProperty(null, 'alpha'),
false,
'should return `false` without node'
)
await t.test('should return `false` without node', async function () {
assert.equal(hasProperty(null, 'alpha'), false)
})

assert.equal(
hasProperty({type: 'text', value: 'alpha'}, 'bravo'),
false,
'should return `false` without `element`'
)
await t.test('should return `false` without `element`', async function () {
assert.equal(hasProperty({type: 'text', value: 'alpha'}, 'bravo'), false)
})

assert.equal(
hasProperty({type: 'element'}, 'charlie'),
false,
'should return `false` without properties'
)
await t.test('should return `false` without properties', async function () {
assert.equal(hasProperty({type: 'element'}, 'charlie'), false)
})

assert.equal(
hasProperty({type: 'element', properties: {}}, 'toString'),
false,
'should return `false` for prototypal properties'
await t.test(
'should return `false` for prototypal properties',
async function () {
assert.equal(
hasProperty({type: 'element', properties: {}}, 'toString'),
false
)
}
)

assert.equal(
hasProperty(
{
type: 'element',
properties: {id: 'delta'}
},
'echo'
),
false,
'should return `false` if the property does not exist'
await t.test(
'should return `false` if the property does not exist',
async function () {
assert.equal(
hasProperty(
{
type: 'element',
properties: {id: 'delta'}
},
'echo'
),
false
)
}
)

assert.equal(
// @ts-expect-error runtime.
hasProperty({type: 'element', properties: {id: 'delta'}}),
false,
'should return `false` if without `name`'
)
await t.test('should return `false` if without `name`', async function () {
assert.equal(
// @ts-expect-error: check how a missing name is handled.
hasProperty({type: 'element', properties: {id: 'delta'}}),
false
)
})

assert.equal(
hasProperty(
{
type: 'element',
properties: {id: 'delta'}
},
'id'
),
true,
'should return `true` if the property does exist'
await t.test(
'should return `true` if the property does exist',
async function () {
assert.equal(
hasProperty(
{
type: 'element',
properties: {id: 'delta'}
},
'id'
),
true
)
}
)
})

0 comments on commit 8aabe35

Please sign in to comment.