-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Lexus Drumgold <[email protected]>
- Loading branch information
1 parent
076d69a
commit 7fe1fb7
Showing
10 changed files
with
211 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,4 +31,5 @@ unstub | |
vates | ||
vfile | ||
vitest | ||
xast | ||
yarnrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* @file Unit Tests - nodelike | ||
* @module unist-util-visit/utils/tests/unit/nodelike | ||
*/ | ||
|
||
import testSubject from '../nodelike' | ||
|
||
describe('unit:utils/nodelike', () => { | ||
it('should return false if value is not curly-braced object', () => { | ||
expect(testSubject([])).to.be.false | ||
}) | ||
|
||
it('should return false if value.type is invalid', () => { | ||
expect(testSubject({ type: '' })).to.be.false | ||
}) | ||
|
||
it('should return true if value is shaped like a node', () => { | ||
expect(testSubject({ type: 'nothing' })).to.be.true | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* @file Unit Tests - nodename | ||
* @module unist-util-visit/utils/tests/unit/nodename | ||
*/ | ||
|
||
import type * as hast from 'hast' | ||
import type * as xast from 'xast' | ||
import testSubject from '../nodename' | ||
|
||
describe('unit:utils/nodename', () => { | ||
it('should return null if node does not have display name', () => { | ||
expect(testSubject({ type: 'break' })).to.be.null | ||
}) | ||
|
||
describe('node.name', () => { | ||
let node: xast.Instruction | ||
|
||
beforeAll(() => { | ||
node = { | ||
name: 'xml', | ||
type: 'instruction', | ||
value: 'version="1.0" encoding="UTF-8"' | ||
} | ||
}) | ||
|
||
it('should return node.name if node.name is a string', () => { | ||
expect(testSubject(node)).to.equal(node.name) | ||
}) | ||
|
||
it('should return null if node.name is not a string', () => { | ||
expect(testSubject({ ...node, name: { id: node.name } })).to.be.null | ||
}) | ||
}) | ||
|
||
describe('node.tagName', () => { | ||
let node: hast.Element | ||
|
||
beforeAll(() => { | ||
node = { | ||
children: [], | ||
properties: {}, | ||
tagName: 'div', | ||
type: 'element' | ||
} | ||
}) | ||
|
||
it('should return node.tagName if node.tagName is a string', () => { | ||
expect(testSubject(node)).to.equal(node.tagName) | ||
}) | ||
|
||
it('should return null if node.tagName is not a string', () => { | ||
expect(testSubject({ ...node, tagName: { id: node.tagName } })).to.be.null | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* @file Unit Tests - parentlike | ||
* @module unist-util-visit/utils/tests/unit/parentlike | ||
*/ | ||
|
||
import type { Type } from '@flex-development/unist-util-types' | ||
import testSubject from '../parentlike' | ||
|
||
describe('unit:utils/parentlike', () => { | ||
let type: Type | ||
|
||
beforeAll(() => { | ||
type = 'root' | ||
}) | ||
|
||
it('should return false if value is not nodelike', () => { | ||
expect(testSubject('root')).to.be.false | ||
}) | ||
|
||
it('should return false if value.children is invalid', () => { | ||
expect(testSubject({ children: new Set(), type })).to.be.false | ||
}) | ||
|
||
it('should return true if value is shaped like a parent', () => { | ||
expect(testSubject({ children: [], type })).to.be.true | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* @file Entry Point - Utilities | ||
* @module unist-util-visit/utils | ||
*/ | ||
|
||
export { default as nodelike } from './nodelike' | ||
export { default as nodename } from './nodename' | ||
export { default as parentlike } from './parentlike' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* @file Utilities - nodelike | ||
* @module unist-util-visit/nodelike | ||
*/ | ||
|
||
import { isObjectCurly, isString } from '@flex-development/tutils' | ||
import type { Node } from 'unist' | ||
|
||
/** | ||
* Check if something looks like a {@linkcode Node}. | ||
* | ||
* @internal | ||
* | ||
* @this {void} | ||
* | ||
* @param {unknown} value - Thing to check | ||
* @return {value is Node} `true` if `value` looks like a node | ||
*/ | ||
function nodelike(this: void, value: unknown): value is Node { | ||
if (!isObjectCurly(value)) return false | ||
return 'type' in value && isString(value.type) && !!value.type.length | ||
} | ||
|
||
export default nodelike |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* @file Utilities - nodename | ||
* @module unist-util-visit/nodename | ||
*/ | ||
|
||
import { isString, type Nullable } from '@flex-development/tutils' | ||
import type { Node } from 'unist' | ||
|
||
/** | ||
* Get a display name for `node`. | ||
* | ||
* The following properties will be used as display names if found: | ||
* | ||
* - `tagName` | ||
* - `name` | ||
* | ||
* @internal | ||
* | ||
* @template {Node} [T=Node] - Node to check | ||
* | ||
* @this {void} | ||
* | ||
* @param {T} node - Node to check | ||
* @return {Nullable<string>} Display name or `null` | ||
*/ | ||
function nodename<T extends Node = Node>( | ||
this: void, | ||
node: T | ||
): Nullable<string> { | ||
return 'tagName' in node && isString(node.tagName) // hast | ||
? node.tagName | ||
: 'name' in node && isString(node.name) // xast | ||
? node.name | ||
: null | ||
} | ||
|
||
export default nodename |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* @file Utilities - parentlike | ||
* @module unist-util-visit/parentlike | ||
*/ | ||
|
||
import { isArray } from '@flex-development/tutils' | ||
import type { Parent } from 'unist' | ||
import nodelike from './nodelike' | ||
|
||
/** | ||
* Check if something looks like a {@linkcode Parent}. | ||
* | ||
* @internal | ||
* | ||
* @this {void} | ||
* | ||
* @param {unknown} value - Thing to check | ||
* @return {value is Parent} `true` if `value` looks like a parent | ||
*/ | ||
function parentlike(this: void, value: unknown): value is Parent { | ||
return nodelike(value) && 'children' in value && isArray(value.children) | ||
} | ||
|
||
export default parentlike |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters