Launch Week Day 5: Introducing Reachability for PHP.Learn More
Socket
Book a DemoSign in
Socket

unist-util-find

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

unist-util-find - npm Package Compare versions

Comparing version
1.0.2
to
1.0.3
+30
index.d.ts
export default find;
export type Node = import('unist').Node;
/**
* Finds first node with a truthy property matching string.
*/
export type TestStr = string;
/**
* Finds first node that has matching values for all properties of object.
*/
export type TestObj = {
[x: string]: unknown;
};
/**
* Finds first node for which function returns true when passed node as argument.
*/
export type TestFn = <V extends import("unist").Node<import("unist").Data>>(node: V) => boolean;
/**
* Unist node finder utility.
*
* @param tree
* Node to search.
* @param condition
* Condition used to test each node.
* @returns
* The first node that matches condition, or undefined if no node matches.
* @type {<V extends Node>(tree: Node, condition: TestStr | TestObj | TestFn) => V | undefined}
*/
declare function find<V extends import("unist").Node<import("unist").Data>>(tree: import("unist").Node<import("unist").Data>, condition: string | {
[x: string]: unknown;
} | TestFn): V | undefined;
+24
-15

@@ -7,21 +7,33 @@ /**

* @fileoverview Unist node finder
*
* @typedef {import('unist').Node} Node
*
* @typedef {string} TestStr
* Finds first node with a truthy property matching string.
* @typedef {Object.<string, unknown>} TestObj
* Finds first node that has matching values for all properties of object.
* @typedef {<V extends Node>(node: V) => boolean} TestFn
* Finds first node for which function returns true when passed node as argument.
*/
'use strict'
import { visit } from 'unist-util-visit'
import iteratee from 'lodash.iteratee'
var visit = require('unist-util-visit')
var iteratee = require('lodash.iteratee')
/**
* Find
* Unist node finder utility.
*
* @param {Node} tree - Root node
* @param {string|object|function} [condition] - Condition to match node.
* @param tree
* Node to search.
* @param condition
* Condition used to test each node.
* @returns
* The first node that matches condition, or undefined if no node matches.
* @type {<V extends Node>(tree: Node, condition: TestStr | TestObj | TestFn) => V | undefined}
*/
function find (tree, condition) {
if (!tree) throw new Error('unist-find requires a tree to search')
if (!condition) throw new Error('unist-find requires a condition')
if (!tree) throw new Error('unist-util-find requires a tree to search')
if (!condition) throw new Error('unist-util-find requires a condition')
var predicate = iteratee(condition)
var result
const predicate = iteratee(condition)
let result

@@ -38,5 +50,2 @@ visit(tree, function (node) {

/*
* Expose.
*/
module.exports = find
export default find
{
"name": "unist-util-find",
"version": "1.0.2",
"version": "1.0.3",
"description": "Unist node finder utility. Useful for working with remark, rehype and retext.",
"repository": "https://github.com/blahah/unist-util-find",
"main": "index.js",
"types": "index.d.ts",
"files": [
"index.d.ts",
"index.js"
],
"sideEffects": false,
"scripts": {
"test": "standard && node test.js"
"test": "standard && node test.js",
"build": "tsc",
"prepare": "npm run build",
"release": "release-it"
},
"engines": {
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"type": "module",
"keywords": [

@@ -24,10 +37,14 @@ "unist",

"devDependencies": {
"remark": "^7.0.0",
"standard": "^8.0.0",
"tape": "^4.6.0"
"@types/lodash.iteratee": "^4.7.7",
"@types/unist": "^2.0.6",
"release-it": "^15.11.0",
"remark": "^14.0.1",
"standard": "^16.0.4",
"tape": "^5.3.1",
"typescript": "^4.6.4"
},
"dependencies": {
"lodash.iteratee": "^4.5.0",
"unist-util-visit": "^1.1.0"
"lodash.iteratee": "^4.7.0",
"unist-util-visit": "^4.1.0"
}
}
language: node_js
node_js:
- '0.11'
- '0.12'
- '4.0'
- '5.0'
- '6.0'
var remark = require('remark')
var find = require('./index.js')
remark()
.use(function () {
return function (tree) {
// string condition
console.log(find(tree, 'value'))
// object condition
console.log(find(tree, { value: 'emphasis' }))
// function condition
console.log(find(tree, function (node) {
return node.type === 'inlineCode'
}))
}
})
.processSync('Some _emphasis_, **strongness**, and `code`.')
var test = require('tape')
var remark = require('remark')
var find = require('./index.js')
test('unist-find', function (t) {
var tree = remark().parse('Some _emphasis_, **strongness**, and `code`.')
t.throws(function () {
find()
}, 'should fail without tree')
t.throws(function () {
find(tree)
}, 'should fail without condition')
t.test('should find with string condition', function (st) {
var result = find(tree, 'value')
st.equal(result, tree.children[0].children[0])
st.end()
})
t.test('should find with object condition', function (st) {
var result = find(tree, { type: 'emphasis' })
st.equal(result, tree.children[0].children[1])
st.end()
})
t.test('should find with function condition', function (st) {
var result = find(tree, function (node) {
return node.type === 'inlineCode'
})
st.equal(result, tree.children[0].children[5])
st.end()
})
t.test('should return undefined if no matches', function (st) {
var result = find(tree, 'nope, nope, nope')
st.equal(result, undefined)
st.end()
})
})