Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: syntax-tree/unist-util-find-before
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 3.0.0
Choose a base ref
...
head repository: syntax-tree/unist-util-find-before
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: main
Choose a head ref

Commits on Jun 1, 2021

  1. Copy the full SHA
    50555e0 View commit details

Commits on Jul 24, 2021

  1. Copy the full SHA
    b8f403e View commit details
  2. Refactor code-style

    wooorm committed Jul 24, 2021
    Copy the full SHA
    51eff15 View commit details
  3. Add strict to tsconfig.json

    wooorm committed Jul 24, 2021
    Copy the full SHA
    d10307e View commit details
  4. Update xo

    wooorm committed Jul 24, 2021
    Copy the full SHA
    a7bcc1a View commit details

Commits on Mar 3, 2022

  1. Copy the full SHA
    0780842 View commit details

Commits on Jun 3, 2022

  1. Update dev-dependencies

    wooorm committed Jun 3, 2022
    Copy the full SHA
    3913540 View commit details
  2. Refactor code-style

    wooorm committed Jun 3, 2022
    Copy the full SHA
    48a6c9e View commit details
  3. Replace dev-dependency

    wooorm committed Jun 3, 2022
    Copy the full SHA
    5dd9f26 View commit details
  4. Add improved docs

    wooorm committed Jun 3, 2022
    Copy the full SHA
    d262197 View commit details
  5. Add improved jsdoc

    wooorm committed Jun 3, 2022
    Copy the full SHA
    cc14892 View commit details

Commits on Jan 24, 2023

  1. Update dev-dependencies

    wooorm committed Jan 24, 2023
    Copy the full SHA
    889f4fd View commit details
  2. Update Actions

    wooorm committed Jan 24, 2023
    Copy the full SHA
    bb82931 View commit details
  3. Update tsconfig.json

    wooorm committed Jan 24, 2023
    Copy the full SHA
    5194684 View commit details
  4. Copy the full SHA
    337a25e View commit details
  5. Refactor code-style

    *   Add more docs to JSDoc
    *   Add support for `null` in input of API types
    wooorm committed Jan 24, 2023
    Copy the full SHA
    8372200 View commit details
  6. Use Node test runner

    wooorm committed Jan 24, 2023
    Copy the full SHA
    2fae894 View commit details
  7. Add improved docs

    wooorm committed Jan 24, 2023
    Copy the full SHA
    322e6d1 View commit details
  8. 3.0.1

    wooorm committed Jan 24, 2023
    Copy the full SHA
    cd0cd7f View commit details

Commits on Jul 3, 2023

  1. Update dev-dependencies

    wooorm committed Jul 3, 2023
    Copy the full SHA
    9ade2a5 View commit details
  2. Copy the full SHA
    8771d9b View commit details
  3. Refactor code-style

    wooorm committed Jul 3, 2023
    Copy the full SHA
    7993029 View commit details
  4. Refactor .npmrc

    wooorm committed Jul 3, 2023
    Copy the full SHA
    be23fdf View commit details
  5. Refactor docs

    wooorm committed Jul 3, 2023
    Copy the full SHA
    90ed9db View commit details
  6. Change to require Node.js 16

    wooorm committed Jul 3, 2023
    Copy the full SHA
    b3d27ae View commit details
  7. Change to use export map

    wooorm committed Jul 3, 2023
    Copy the full SHA
    824f157 View commit details
  8. Change to return undefined

    wooorm committed Jul 3, 2023
    Copy the full SHA
    b9ba870 View commit details

Commits on Jul 7, 2023

  1. Update @types/unist

    wooorm committed Jul 7, 2023
    Copy the full SHA
    9039274 View commit details
  2. Add smarter types

    wooorm committed Jul 7, 2023
    Copy the full SHA
    21144ef View commit details
  3. 4.0.0

    wooorm committed Jul 7, 2023
    Copy the full SHA
    785e58c View commit details

Commits on Jan 2, 2025

  1. Update dev-dependencies

    wooorm committed Jan 2, 2025
    Copy the full SHA
    db24986 View commit details
  2. Copy the full SHA
    6d56095 View commit details
  3. Update Actions

    wooorm committed Jan 2, 2025
    Copy the full SHA
    b742a71 View commit details
  4. Refactor to use @imports

    wooorm committed Jan 2, 2025
    Copy the full SHA
    44b2970 View commit details
  5. Refactor .editorconfig

    wooorm committed Jan 2, 2025
    Copy the full SHA
    a0cebdd View commit details
  6. Remove license year

    wooorm committed Jan 2, 2025
    Copy the full SHA
    d1c98fb View commit details
  7. Refactor package.json

    wooorm committed Jan 2, 2025
    Copy the full SHA
    3719ee3 View commit details
  8. Add declaration maps

    wooorm committed Jan 2, 2025
    Copy the full SHA
    41629ac View commit details
  9. 4.0.1

    wooorm committed Jan 2, 2025
    Copy the full SHA
    c3e5455 View commit details
Showing with 594 additions and 370 deletions.
  1. +4 −4 .editorconfig
  2. +6 −6 .github/workflows/bb.yml
  3. +8 −8 .github/workflows/main.yml
  4. +3 −1 .gitignore
  5. +1 −0 .npmrc
  6. +1 −58 index.js
  7. +42 −0 index.test-d.ts
  8. +132 −0 lib/index.js
  9. +1 −1 license
  10. +58 −60 package.json
  11. +116 −57 readme.md
  12. +212 −166 test.js
  13. +10 −9 tsconfig.json
8 changes: 4 additions & 4 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
12 changes: 6 additions & 6 deletions .github/workflows/bb.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
name: bb
on:
issues:
types: [opened, reopened, edited, closed, labeled, unlabeled]
pull_request:
types: [opened, reopened, edited, closed, labeled, unlabeled]
jobs:
main:
runs-on: ubuntu-latest
steps:
- uses: unifiedjs/beep-boop-beta@main
with:
repo-token: ${{secrets.GITHUB_TOKEN}}
name: bb
on:
issues:
types: [closed, edited, labeled, opened, reopened, unlabeled]
pull_request_target:
types: [closed, edited, labeled, opened, reopened, unlabeled]
16 changes: 8 additions & 8 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
name: main
on:
- pull_request
- push
jobs:
main:
name: ${{matrix.node}}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: dcodeIO/setup-node-nvm@master
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{matrix.node}}
- run: npm install
- run: npm test
- uses: codecov/codecov-action@v1
- uses: codecov/codecov-action@v5
strategy:
matrix:
node:
- lts/erbium
- lts/hydrogen
- node
name: main
on:
- pull_request
- push
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
.DS_Store
*.d.ts
*.log
*.map
*.tsbuildinfo
.DS_Store
coverage/
node_modules/
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
ignore-scripts=true
package-lock=false
59 changes: 1 addition & 58 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,58 +1 @@
/**
* @typedef {import('unist').Node} Node
* @typedef {import('unist').Parent} Parent
*
* @typedef {import('unist-util-is').Type} Type
* @typedef {import('unist-util-is').Props} Props
* @typedef {import('unist-util-is').TestFunctionAnything} TestFunctionAnything
*/

import {convert} from 'unist-util-is'

export var findBefore =
/**
* @type {(
* (<T extends Node>(node: Parent, index: Node|number, test: T['type']|Partial<T>|import('unist-util-is').TestFunctionPredicate<T>|Array.<T['type']|Partial<T>|import('unist-util-is').TestFunctionPredicate<T>>) => T|null) &
* ((node: Parent, index: Node|number, test?: null|undefined|Type|Props|TestFunctionAnything|Array<Type|Props|TestFunctionAnything>) => Node|null)
* )}
*/
(
/**
* @param {Parent} parent Parent node
* @param {Node|number} index Child of `parent`, or it’s index
* @param {null|undefined|Type|Props|TestFunctionAnything|Array<Type|Props|TestFunctionAnything>} [test] is-compatible test (such as a type)
* @returns {Node|null}
*/
function (parent, index, test) {
var is = convert(test)

if (!parent || !parent.type || !parent.children) {
throw new Error('Expected parent node')
}

if (typeof index === 'number') {
if (index < 0 || index === Number.POSITIVE_INFINITY) {
throw new Error('Expected positive finite number as index')
}
} else {
index = parent.children.indexOf(index)

if (index < 0) {
throw new Error('Expected child node or index')
}
}

// Performance.
if (index > parent.children.length) {
index = parent.children.length
}

while (index--) {
if (is(parent.children[index], index, parent)) {
return parent.children[index]
}
}

return null
}
)
export {findBefore} from './lib/index.js'
42 changes: 42 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {expectType} from 'tsd'
import type {
Heading,
PhrasingContent,
Root,
RootContent,
RowContent,
TableCell,
TableRow,
Text
} from 'mdast'
import {findBefore} from './index.js'

const text: Text = {type: 'text', value: 'alpha'}
const heading: Heading = {type: 'heading', depth: 1, children: [text]}
const root: Root = {type: 'root', children: [heading]}
const cell: TableCell = {type: 'tableCell', children: [text]}
const row: TableRow = {type: 'tableRow', children: [cell]}

// @ts-expect-error: parent needed.
findBefore()

// @ts-expect-error: child or index needed.
findBefore(heading)

findBefore(
// @ts-expect-error: parent needed.
text,
0
)

expectType<PhrasingContent | undefined>(findBefore(heading, text))

expectType<Text | undefined>(findBefore(heading, text, 'text'))

expectType<Text | undefined>(findBefore(heading, 0, 'text'))

expectType<RootContent | undefined>(findBefore(root, 0))

expectType<Text | undefined>(findBefore(root, 0, 'text'))

expectType<RowContent | undefined>(findBefore(row, 0))
132 changes: 132 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/**
* @import {Test} from 'unist-util-is'
* @import {Node as UnistNode, Parent as UnistParent} from 'unist'
*/

/**
* @typedef {(
* Fn extends (value: any) => value is infer Thing
* ? Thing
* : Fallback
* )} Predicate
* Get the value of a type guard `Fn`.
* @template Fn
* Value; typically function that is a type guard (such as `(x): x is Y`).
* @template Fallback
* Value to yield if `Fn` is not a type guard.
*/

/**
* @typedef {(
* Check extends null | undefined // No test.
* ? Value
* : Value extends {type: Check} // String (type) test.
* ? Value
* : Value extends Check // Partial test.
* ? Value
* : Check extends Function // Function test.
* ? Predicate<Check, Value> extends Value
* ? Predicate<Check, Value>
* : never
* : never // Some other test?
* )} MatchesOne
* Check whether a node matches a primitive check in the type system.
* @template Value
* Value; typically unist `Node`.
* @template Check
* Value; typically `unist-util-is`-compatible test, but not arrays.
*/

/**
* @typedef {(
* Check extends Array<any>
* ? MatchesOne<Value, Check[keyof Check]>
* : MatchesOne<Value, Check>
* )} Matches
* Check whether a node matches a check in the type system.
* @template Value
* Value; typically unist `Node`.
* @template Check
* Value; typically `unist-util-is`-compatible test.
*/

/**
* @typedef {(
* Kind extends {children: Array<infer Child>}
* ? Child
* : never
* )} Child
* Collect nodes that can be parents of `Child`.
* @template {UnistNode} Kind
* All node types.
*/

import {convert} from 'unist-util-is'

/**
* Find the first node in `parent` before another `node` or before an index,
* that passes `test`.
*
* @param parent
* Parent node.
* @param index
* Child node or index.
* @param [test=undefined]
* Test for child to look for (optional).
* @returns
* A child (matching `test`, if given) or `undefined`.
*/
export const findBefore =
// Note: overloads like this are needed to support optional generics.
/**
* @type {(
* (<Kind extends UnistParent, Check extends Test>(parent: Kind, index: Child<Kind> | number, test: Check) => Matches<Child<Kind>, Check> | undefined) &
* (<Kind extends UnistParent>(parent: Kind, index: Child<Kind> | number, test?: null | undefined) => Child<Kind> | undefined)
* )}
*/
(
/**
* @param {UnistParent} parent
* Parent node.
* @param {UnistNode | number} index
* Child node or index.
* @param {Test} [test=undefined]
* Test for child to look for.
* @returns {UnistNode | undefined}
* A child (matching `test`, if given) or `undefined`.
*/
function (parent, index, test) {
const is = convert(test)

if (!parent || !parent.type || !parent.children) {
throw new Error('Expected parent node')
}

if (typeof index === 'number') {
if (index < 0 || index === Number.POSITIVE_INFINITY) {
throw new Error('Expected positive finite number as index')
}
} else {
index = parent.children.indexOf(index)

if (index < 0) {
throw new Error('Expected child node or index')
}
}

// Performance.
if (index > parent.children.length) {
index = parent.children.length
}

while (index--) {
const child = parent.children[index]

if (is(child, index, parent)) {
return child
}
}

return undefined
}
)
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(The MIT License)

Copyright (c) 2015 Titus Wormer <tituswormer@gmail.com>
Copyright (c) Titus Wormer <tituswormer@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Loading