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

xm-el

v0.1.2

Published

Simple xml creation using d3.js-inspired syntax

Downloads

4

Readme

xm-el

Simple XML creation using d3.js-inspired syntax.

Install

npm install xm-el

Examples

examples will reference the library as variable 'xml':

var xml = require('xm-el');

Create Elements:

Create basic elements with xml.element(<tagname>)

var kerouac = xml.element('author');

Add a text node with .text()

kerouac.text('Jack Kerouac');

Add attributes with .attr(<name>, <value>)

kerouac.attr('literary-movement', 'Beat');

Get the markup as a string using .stringify()

kerouac.stringify();
// '<author literary-movement="Beat">Jack Kerouac</author>'

Create self-closing elements with xml.element(<tagname>, true)

// create a self-closing element, set 2nd argument to 'true'
var pagebreak = xml.element('br', true);
pagebreak.stringify();
// '<br />'

Multiple attributes can be assigned in a single call by passing a plain object to Element.attr()

var svg = xml.element('svg')
  .attr({
    'width': 400,
    'height': 300,
    'viewBox': '0 0 400 300',
    'preserveAspectRatio': 'xMidYMid meet'
  });

Chaining:

Creating an element or setting an attribute returns the element, which allows you to chain methods together:

xml.element('composer')
  .attr('last-name', 'Reich')
  .attr('genre', 'minimalism')
  .text('Steve Reich')
  .stringify();
// '<composer last-name="Reich" genre="minimalism">Steve Reich</composer>'

Child Elements:

Create child elements using .append(<tagname>)

// some familiar names
var members = {
  'John Lennon': 'guitar',
  'Paul McCartney': 'bass',
  'George Harrison': 'guitar',
  'Ringo Starr': 'drums'
};

// lets say they're in a band
var beatles = xml.element('band')
  .attr('name', 'The Beatles');

for (var name in members) {
  // add each member using append()
  beatles.append('member')
    .attr('instrument', members[name])
    .text(name);
}

// gotta have a manager
beatles.append('manager')
  .text('Brian Epstein');

Output to file:

Create an xml declaration with xml.declaration(<options>).

If no options are given, the default output is <?xml version="1.0"?>

the options argument is a plain object with 3 optional properties: version, encoding and standalone.

// lets create a standalone file out of the beatles example
var declaration = xml.declaration({standalone: 'yes'});

// giving an argument to stringify() tidies output with the specified indent
var output = declaration + beatles.stringify(2);
// write to file
require('fs').writeFileSync('/path/to/beatles.xml', output)

beatles.xml

<?xml version="1.0" standalone="yes"?>
<band name="The Beatles">
  <member instrument="guitar">John Lennon</member>
  <member instrument="bass">Paul McCartney></member>
  <member instrument="guitar">George Harrison</member>
  <member instrument="drums">Ringo Starr</member>
  <manager>Brian Epstein</manager>
</band>

Create a doctype with xml.doctype(<name>)

// create svg1.1 doctype
var doctype = xml.doctype('svg');
var declaration = xml.declaration();

// now lets write a simple svg file
var svg = xml.element('svg')
  .attr('xmlns', 'http://www.w3.org/2000/svg')
  .attr('width', 300)
  .attr('height', 200)
  .attr('viewBox', '0 0 300 200');

var g = svg.append('g')
  .attr('class', 'circle-group');

// create 10 random circles  
var circles = 10;
while (circles > 0) {
  g.append('circle')
    .attr('cx', Math.floor(Math.random() * 300))
    .attr('cy', Math.floor(Math.random() * 200))
    .attr('r', 4 + Math.floor(Math.random() * 10))
    .attr('stroke-width', 1 + Math.floor(Math.random() * 3))
    .attr('stroke', '#333');
  circles--;
}

// output a file
var output = declaration + doctype + svg.stringify(2);
require('fs').writeFileSync('/path/to/circles.svg', output);

circles.svg

<?xml version="1.0"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" width="300" height="200" viewBox="0 0 300 200">
  <g class="circle-group">
    <circle cx="87" cy="194" r="9" stroke-width="3" stroke="#333"></circle>
    <circle cx="279" cy="97" r="7" stroke-width="2" stroke="#333"></circle>
    <circle cx="264" cy="24" r="5" stroke-width="2" stroke="#333"></circle>
    <circle cx="16" cy="43" r="11" stroke-width="3" stroke="#333"></circle>
    <circle cx="52" cy="32" r="7" stroke-width="3" stroke="#333"></circle>
    <circle cx="298" cy="83" r="4" stroke-width="1" stroke="#333"></circle>
    <circle cx="91" cy="107" r="10" stroke-width="3" stroke="#333"></circle>
    <circle cx="281" cy="169" r="10" stroke-width="2" stroke="#333"></circle>
    <circle cx="107" cy="51" r="13" stroke-width="2" stroke="#333"></circle>
    <circle cx="225" cy="154" r="8" stroke-width="1" stroke="#333"></circle>
  </g>
</svg>