npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ezwrap

v2.0.0

Published

Easily wrap parsed XML objects for reliable access

Downloads

2

Readme

EZWrap

Node.js CI Coverage Status

This module makes dealing with parsed XML objects easy and painless. Uses Needle's XML formatted objects.

For example, a parsed Needle XML response might look like this:

<NodeName someAttribute="someValue">
    <ChildNode>Value 1</ChildNode>
</NodeName>

Becomes:

{
  "name": "NodeName",
  "value": "",
  "attributes": {
    "someAttribute": "someValue"
  },
  "children": [
    {
      "name": "ChildNode",
      "value": "Value 1",
      "attributes": {},
      "children": []
    }
  ]
}

EZWrap makes dealing with traversing child nodes and attributes easy and reliable. If a node in the path does not exist, no problem. The fluent-like interface handles missing or empty nodes without a problem.

Installation

Install like so:

npm i --save ezwrap

Usage

const EZWrap = require('ezwrap');

const res = new EZWrap(response.body); // provide the parsed xml object to the constructor

// Get a node attribute
res.attr('someAttribute'); // => someValue

// Get a value from a child node
res.get('ChildNode').value; // => Value 1

// Safely handle missing paths in the XML
res.get('Does').get('Not').get('Exist').value; // => null

Custom inspection is included so that visualizing where you are in the tree is simple.

For example, in the node REPL:

$ node
> const EZWrap = require('ezwrap')
undefined

> const xml = {
...   "name": "NodeName",
...   "value": "",
...   "attributes": {
.....     "someAttribute": "someValue"
.....   },
...   "children": [
...     {
.....       "name": "ChildNode",
.....       "value": "Value 1",
.....       "attributes": {},
.....       "children": []
.....     }
...   ]
... }
undefined

> res = new EZWrap(xml)
<NodeName someAttribute="someValue">
  <ChildNode ... />
</NodeName>

> res.get('ChildNode')
<ChildNode>
  Value 1
</ChildNode>

Really convenient for REPL and debugging, especially with payloads with a ton of child nodes and deep trees.

Browser Usage

You can use this module in the browser too. A separate file is included, which has been run through Babel and Browserify for browser compatibility.

EZWrap API

new EZWrap(xmlObject)

Returns a new wrapped instance where:

  • xmlObject – the XML-parsed object to wrap

ezwrap.isWrapped

Property which denotes whether the object is EZWrapped (true)

ezwrap.length

Property which returns the number of children the node contains.

ezwrap.value

Property which returns the value of the node or null if not defined.

ezwrap.name

Property which returns the name of the node or null if not defined.

ezwrap.attr(name)

Gets an attribute value on the node where:

  • name – is the name of the attribute

Returns the value of the attribute or null if not defined.

ezwrap.all(name)

Filters children nodes where:

  • name – is the name of the child nodes to keep

Returns an EZWrapped object containing the filtered nodes.

ezwrap.get(name)

Gets the first node where:

  • name – is the name of the child node to get

Returns the EZWrapped child object, or a wrapped empty object if not found.

ezwrap.find(closure)

Similar to Array.find(), gets the first node where:

  • closure(node) – is a function called on each child node until the return value is truthy.
    • node – EZWrapped child node

Returns the selected EZWrapped node or an empty EZWrapped node if no nodes were matched.

ezwrap.forEach(closure)

Similar to Array.forEach(), calls the closure function for each child node.

  • closure(node) – function called on each child node
    • node – EZWrapped child node

ezwrap.filter(closure)

Similar to Array.filter(), calls the closure function for each child node and keeps the filtered results.

  • closure(node) – function called on each child node. Return truthy to keep the node in the results.
    • node – EZWrapped child node

Returns an EZWrapped object containing the filtered children.

ezwrap.map(closure)

Similar to Array.map(), calls the closure function for each child node and returns an array of mapped results.`

  • closure(node) – function called on each child node. The value returned will be included in the results.
    • node – EZWrapped child node

Returns an array of mapped result values.

Class Functions

EZWrap.getEmptyObject([options])

Class/static function that generates raw XML-like objects, where:

  • options:
    • name: Optional node name
    • value: Optional node value
    • attributes: Optional attributes object
    • children: Optional array of node objects

Returns a raw, unwrapped node object.

Contributing

There is plenty of room for this module to grow. Feel free to contribute additions!

Building

To build the browser bundle, run:

npm run build

This will update:

  • dist/ezwrap.js
  • dist/ezwrap.min.js
  • dist/ezwrap.min.js.map

Testing

To run the unit tests with code coverage and linting:

npm run report

All Package Scripts

Included scripts you can run are:

  • npm run clean – Cleans the distribution and coverage directories
  • npm run build – Builds the browser distribution
  • npm run test – Runs unit tests without coverage
  • npm run cover – Runs unit tests with coverage
  • npm run lint – Runs eslint for code quality checks
  • npm run report – Runs build, cover, and lint

Good luck, and have fun!