Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

js-select

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

js-select

Traverse and modify objects with JSONSelect selectors

  • 0.5.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
6.5K
decreased by-11.19%
Maintainers
1
Weekly downloads
 
Created
Source

js-select

js-select uses js-traverse to traverse and modify JavaScript object nodes that match JSONSelect selectors.

var people = {
   george: {
      age : 35,
      movie: "Repo Man"
   },
   mary: {
      age: 15,
      movie: "Twilight"
   }
};

.forEach(fn)

Iterates over all matching nodes in the object. The callback gets a special this context. See js-traverse for all the things you can do to modify and inspect the node with this context. In addition, js-select adds a this.matches() which will test if the node matches a selector:

select(people).forEach(function(node) {
   if (this.matches(".mary > .movie")) {
      this.remove();
   }
});

.nodes()

Returns all matching nodes from the object.

select(people, ".age").nodes(); // [35, 15]

.remove()

Removes matching elements from the original object.

select(people, ".age").remove();

.update(fn)

Updates all matching nodes using the given callback.

select(people, ".age").update(function(age) {
   return age - 5;
});

.condense()

Reduces the original object down to only the matching elements (the hierarchy is maintained).

select(people, ".age").condense();
{
    george: { age: 35 },
    mary: { age: 15 }
}

Selectors

js-select supports the following JSONSelect selectors:

*
type
.key
ancestor selector
parent > selector
sibling ~ selector
selector1, selector2
:root
:nth-child(n)
:nth-child(even)
:nth-child(odd)
:nth-last-child(n)
:first-child
:last-child
:only-child
:has(selector)
:val("string")
:contains("substring")

See details on each selector, and try them out on the JSONSelect website.

Install

For node, install with npm:

npm install js-select

For the browser, download the select.js file or fetch the latest version from npm and build a browser file using browserify:

npm install browserify -g
npm install js-select

browserify --require js-select --outfile select.js

this will build a browser file with require('js-select') available.

Propers

Huge thanks to @substack for the ingenious js-traverse and @lloyd for the ingenious JSONSelect spec and selector parser.

Keywords

FAQs

Package last updated on 24 Sep 2012

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc