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

jsxn

v0.0.13

Published

JSON representation of XML

Downloads

29

Readme

JSXN - JSON representation of XML by simple mapping rules

Currently experimental, expect updates to break things

Example usage

const parser = require('slimdom-sax-parser')
import {XMLDocument, Element} from "slimdom"
import {jsxn, Rule} from "./index"

const sampleXml = `
<root xmlns:y="http://localhost/yellow" xmlns:g="http://localhost/green">
    <tree>
        <branch>br1</branch>
        <branch>br2</branch>
    </tree>
    <plant type="shrub"/>
    <plant type="bush"/>
    <option value="true">
        <value>false</value>
    </option>
    <y:yellow a="b">
        text
    </y:yellow>
    <g:yellow>green</g:yellow>
    <simpleText ab="123">text of element</simpleText>
    <simpleText2 xmlns="http://abc">text of element</simpleText2>
    <dash-element dash-attribute="123" dash-dash="slash"/>
    <items>
        <item-part>1</item-part>
        <item-part>2</item-part>
    </items>
</root>
`
const document:XMLDocument = parser.sync(sampleXml)

/**
 * Default behaviors:
 * - when no rule specified, first use key to match by element localName and then by attribute name
 * - if element has no children or attributes, then automaticaly resolve as the textContent
 * - elements are assumed to be single instance unless a rule is added to indicate more than 1 by specifying type = 'multiple'
 * 
 * Rules can be used to customize the behavior of key resolution
 */
const rules:Rule[] = [
    {element:'plant', type: 'multiple'},
    {element:'value', asKey:'v'},
    {element:'yellow', asKey: 'green', namespace: 'http://localhost/green'},
    {element:'simpleText', type: 'text'},
    {element:'item-part', type: 'multiple'},
    {attribute: 'dash-dash', asKey:'dashy'}
]

const xml = jsxn(document.documentElement as Element, rules, {convertKeysToCamelCase:true})

console.log(xml.tree.branch === 'br1')
console.log(xml.plant[1].type === 'bush')
console.log(xml.option.value === 'true')
console.log(xml.option.v === 'false')
console.log(xml.yellow.a === 'b')
console.log(xml.green === 'green')
console.log(xml.simpleText === 'text of element')
console.log(xml.simpleText2 === 'text of element')
console.log(xml.dashElement.dashAttribute === '123')
console.log(xml.dashElement.dashy === 'slash')