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

os-xml-file-handler

v1.0.6

Published

This module will build, read and manipulate an xml files. Other handy stuff is also available, like search for a specific nodes.

Downloads

14

Readme

Introduction

This module will build, read and manipulate an xml files. Other handy stuff is also available, like search for a specific nodes.

Installation

Install via npm:

npm i os-xml-file-handler

Functions and signatures:

/**
* Will create an xml file to work with.
* Notice: this method WON'T save the xml in the system. To save the xml, call saveXml(xml)
*
* @param rootNodeTag -> the tag name of the root node
*/
createXml: function (rootNodeTag) {
    return self.loadXml(null, '<?xml version="1.0" encoding="utf-8"?>\n<' + rootNodeTag + '/>')
},


/**
 * Will save any changes made in the xml
 *
 * @param xml -> the xml
 * @param filePath -> the output path of the file
 */
saveXml: function (xml, filePath) {
    let xmlStr = xml.write({'xml_declaration': true});
    streamer.writeFileSync(xmlStr, filePath)
},

/**
 * Will load an xml file
 *
 * @param fromPath -> optional path to the xml file
 * @param fromStr -> optional xml string
 */
loadXml: async function (fromPath, fromStr = null) {
    let xmlStr = fromStr;
    if (xmlStr === null) {
        let xmlInLines = await streamer.readFile(fromPath);
        xmlStr = xmlInLines.join("\n");
    }
    return et.parse(xmlStr);
},

/**
 * Will add a node to a certain (relative) location
 *
 * @param xml -> the xml file
 * @param nodeTag -> the tag for the new node ('String', for example)
 * @param attDict -> optional dict of att and values for the new node
 * @param nodeText -> optional text the new node will carry
 * @param parentNode -> (optional) the parent of the new node (for root descendent, leave blank)
 */
addNode: function (xml, nodeTag, attDict = {}, nodeText = null, parentNode = null) {

    if(parentNode === null) {
        parentNode = self.getRoot(xml)
    }

    let element = et.SubElement(parentNode, nodeTag, attDict);

    if(nodeText !== null) {
        self.setNodeText(element, nodeText)
    }

    return element
},

/**
 * Will return a list of nodes specified by an attribute key and and an attribute value.
 *
 * @param xml -> the xml object
 * @param node_tag -> the tag of the nodes to look for
 * @param node_att_name -> the att name of the nodes to look for
 * @param node_att_val -> the att value of the nodes to look for
 */
getNodes: function (xml, node_tag, node_att_name = null, node_att_val = null) {

    let selector = node_tag;

    if (node_att_name !== null) {
        if (node_att_val !== null) {
            selector = node_tag + "[@" + node_att_name + "='" + node_att_val + "']"
        } else {
            selector = node_tag + "[@" + node_att_name + "]"
        }
    }
    return xml.findall(".//" + selector)
},

/**
 * Will return the root element of the xml
 */
getRoot: function(xml) {
    return xml.getroot()
},

/**
 * Will set the text (inner html) in a given node
 */
setNodeText: function (node, text) {
    node.text = text
},

/**
 * Will return the text (inner html) from a given node
 */
getNodeText: function (node) {
    return node.text
},

/**
 * Will set attributes to an element
 *
 * @param node -> the node to which the attributes will be appended
 * @param attDict -> the dictionary of attributes to add
 */
setNodeAtt: function (node, attDict) {
    for (let key in attDict) {
        if (attDict.hasOwnProperty(key)) {
            node.attrib[key] = attDict[key];
        }
    }
    return node
},

/**
 * Will remove node attribute
 */
removeNodeAtt: function (node, attArr) {
    for (let i = 0; i < attArr.length; i++) {
        delete node.attrib[attArr[i]]
    }
},

/**
 * Will return the node's attribute value
 */
getNodeAttributeValue: function (node, att) {
    return node.attrib[att]
},

/**
 * Will return the tag of a given node
 */
getNodeTag: function (node) {
    return node.tag
},

And more...

Links -> see more tools

  • os-tools-npm -> This module contains fundamental functions to implement in an npm project
  • os-file-handler-npm -> This module contains fundamental files manipulation functions to implement in an npm project
  • os-file-stream-handler-npm -> This module contains read/write and more advanced operations on files
  • os-xml-handler-npm -> This module will build, read and manipulate an xml file. Other handy stuff is also available, like search for specific nodes

GitHub - osfunappsapps

Licence

ISC