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

pigeonmark-utils

v2.1.2

Published

Utilities for working with PigeonMark data structures

Downloads

76

Readme

PigeonMark Utilities

pigeonmark-utils is a set of light weight utilties useful for interacting with JsonML and PigeonMark representations of html and xml documents. It provides some getters and setters to more easily manipulate JsonML documents, and a minimalist but fairly strict test function isPigeonMark() which tells you if a structure looks like valid PigeonMark/JsonML.

Updates:

  • 1.0.0 - Initial Release with just utils.get functions
  • 1.0.1 - Bugfix for utils.get.name()
  • 1.0.2 - Added utils.set functions
  • 1.0.3 - Added utils.isPigeonMark()
  • 1.0.4 - Fixes to pigeonmark-utils/library/tree-selector-adapter
  • 1.0.5 - Updates to dev dependancies mocha and chai to silence npm audit fails
  • 2.0.1 - Rewrite in typescript, switch to esm modules, all set functions return a new node mutated in the requested way instead of modifying the input node, create functions now exist
  • 2.0.2 - make all the isType functions available through utils.get.isWhatever

API

import utils from 'pigeonmark-utils'

Help adapting this to support ES6 style imports would be very appreciated!

utils.get.type(node)

Given a node, returns one of the strings: tag, text, attributes, document, pi, cdata, comment, or fragment. If the input node is not valid PigeonMark, it may return undefined. For JsonML inputs, the result will always be either tag, text, or attributes

You can also find more specific isTag, isText, isAttributes, isComment, etc functions on this import if you import pigeonmark-utils/get/type directly. These can be helpful in typescript as they work as type guards.

utils.get.name(node) or utils.set.name(node, stringValue)

Gets the string tag name of a tag node or xml processing instruction node

utils.get.name(['tag', { attr: 'val' }, 'child text']) //=> 'tag'
utils.get.name(['?xml', { version: '1.0' }]) //=> 'xml'
utils.set.name(['?xml', { version: '1.0' }], 'xml-stylesheet') //=> ['?xml-stylesheet', { version: '1.0' }]

utils.get.id(tag) or utils.set.id(tag, stringValue)

Shortcut to read or write the ID attribute of a tag, returns a string or undefined.

utils.get.id(['tag', { id: 'foo' }, 'child text']) //=> 'foo'
utils.get.id(['tag', 'child text']) //=> undefined
utils.set.id(['tag', 'child text'], 'foo') //=> ['tag', { id: 'foo' }, 'child text']

utils.get.classList(tag) or utils.set.classList(tag, arrayOfStrings)

Returns array of strings from tag's 'class' attribute, or an empty array if it is empty or unset.

utils.get.classList(['tag', { class: 'foo bar' }]) //=> ['foo', 'bar']
utils.get.classList(['tag']) //=> []

utils.get.attribute(tag, attrName) or utils.set.attribute(tag, attrName, attrStringValue)

Returns an attribute value from a tag or xml processing instruction as a string, or undefined if it isn't set. The set varient sets an attribute's value to a string.

utils.get.attributes(tag) or utils.set.attributes(tag, attributesObject)

Gets a tag's attributes as an object, or replaces a tag's attributes with an object.

utils.get.children(node) or utils.set.children(node, childList)

Gets the child tags of a node as an array, excluding direct descendants of other types (for example, skipping text nodes and comments)

The set varient replaces all the childNodes of the node with a child list.

utils.get.childNodes(node) or utils.set.childNodes(node, childList)

Gets all the child nodes of a node, including text nodes, comments, cdata, as well as tags, as an ordered array, or in the set varient, replaces those items

utils.get.text(node) or utils.set.text(node, string)

Like WebAPI's Element.textContent property, returns all the child text nodes, concatinated together in to one string, including descendents inside of tags within this one. The set varient returns a new identical node with this node's child nodes replaced with a single text element containing the string.

utils.create.tag('name', { attr: value }, childNodes[])

Creates a tag, using the supplied parameters

utils.create.tag('label', { for: 'foo' }, ['hello world']) //=> ['label', { for: 'foo' }, 'hello world']

utils.isPigeonMark(node)

given an object, tests if that object could possibly be the root node of a PigeonMark document, which being a slightly extended superset of JsonML, is also good at detecting JsonML documents.

Note that any string is technically a valid JsonML document, as it's a text root node, so lookout for that.

This function tries to do some light validation, so it will return false for most structures that would fail to serialize to XML in really obvious ways, like spaces in tag names, or tag names that aren't strings.