Coder Social home page Coder Social logo

christianmurphy / unist-util-filter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from syntax-tree/unist-util-filter

0.0 2.0 0.0 26 KB

unist utility to create a new tree with nodes that pass a filter

Home Page: https://unifiedjs.com

License: MIT License

JavaScript 100.00%

unist-util-filter's Introduction

unist-util-filter

Build Coverage Downloads Size Sponsors Backers Chat

unist utility to create a new tree with all nodes that pass the given test.

Install

npm:

npm install unist-util-filter

Usage

var u = require('unist-builder')
var filter = require('unist-util-filter')

var tree = u('root', [
  u('leaf', '1'),
  u('node', [u('leaf', '2'), u('node', [u('leaf', '3')])]),
  u('leaf', '4')
])

var newTree = filter(tree, node => node.type !== 'leaf' || node.value % 2 === 0)

console.dir(newTree, {depth: null})

Yields:

{
  type: 'root',
  children: [
    { type: 'node', children: [ { type: 'leaf', value: '2' } ] },
    { type: 'leaf', value: '4' }
  ]
}

API

filter(tree[, options][, test])

Create a new tree consisting of copies of all nodes that pass test. The tree is walked in preorder (NLR), visiting the node itself, then its head, etc.

Parameters
  • tree (Node?) — Tree to filter
  • options.cascade (boolean, default: true) — Whether to drop parent nodes if they had children, but all their children were filtered out
  • test (Test, optional) — is-compatible test (such as a type)
Returns

Node? — New filtered tree. null is returned if tree itself didn’t pass the test, or is cascaded away.

Contribute

See contributing.md in syntax-tree/.github for ways to get started. See support.md for ways to get help.

This project has a Code of Conduct. By interacting with this repository, organisation, or community you agree to abide by its terms.

License

MIT © Eugene Sharygin

unist-util-filter's People

Contributors

eush77 avatar wooorm avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.