Skip to content

Commit

Permalink
feat: Value
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed May 29, 2024
1 parent e2c2e89 commit 9f2fce4
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
- [`fn(node[, index][, parent])`](#fnnode-index-parent)
- [`Type<[T]>`](#typet)
- [`Uint`](#uint)
- [`Value<[T]>`](#valuet)
- [Contribute](#contribute)

## What is this?
Expand Down Expand Up @@ -74,7 +75,8 @@ import type {
PositionalInfo,
Test,
TestFunction,
Type
Type,
Value
} from '@flex-development/unist-util-types'
```

Expand Down Expand Up @@ -252,6 +254,15 @@ Range: `[0, 10]`

> **source**: [`src/uint.ts`](src/uint.ts)
### `Value<[T]>`

Extract the `value` of [*tree*][tree] `T`.

- `T` ([**`Node`**][node]): tree to extract value from
- **default**: [`Literal`][node]

> **source**: [`src/value.ts`](src/value.ts)
## Contribute

See [`CONTRIBUTING.md`](CONTRIBUTING.md).
Expand Down
17 changes: 17 additions & 0 deletions src/__tests__/value.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @file Type Tests - Value
* @module unist-util-types/tests/unit-d/Value
*/

import type * as mdast from 'mdast'
import type TestSubject from '../value'

describe('unit-d:Value', () => {
it('should equal T["value"]', () => {
// Arrange
type T = mdast.Text

// Expect
expectTypeOf<TestSubject<T>>().toEqualTypeOf<T['value']>()
})
})
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ export type { default as Test } from './test'
export type { default as TestFunction } from './test-function'
export type { default as Type } from './type'
export type { default as Uint } from './uint'
export type { default as Value } from './value'
21 changes: 21 additions & 0 deletions src/value.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @file Value
* @module unist-util-types/Value
*/

import type { Literal, Node } from 'unist'

/**
* Extract the value of from [*tree*][1] `T`.
*
* [1]: https://github.com/syntax-tree/unist#tree
*
* @see {@linkcode Literal}
* @see {@linkcode Node}
* @see https://github.com/syntax-tree/unist#literal
*
* @template {Node} [T=Literal] - Tree to try extracting value from
*/
type Value<T extends Node = Literal> = T extends Literal ? T['value'] : never

export type { Value as default }

0 comments on commit 9f2fce4

Please sign in to comment.