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

@slickcodes/object-2-html

v1.0.1

Published

This package allows you convert objects to html and can even revert html to object.

Downloads

6

Readme

Installation

You can install object-2-html using npm , yarn or pnpm

    npm install object-2-html

You can also insert the CDN directly to your project header

    <script src="https://unpkg.com/@slickcodes/[email protected]/dist/index.js"></script>

#Usage

To reader your object to html you'll need to use the reader method provided with the o2h object.

    import oth from 'object-2-html'

    const obj = {
        type: "element",
        tagName: "div",
        style: {background: "purple"},
        classes: ["container","example"],
        childNodes: [ // adding children
            {
                type: "comment",
                text: "the following code is an h1 tag"
            },
            {
                type: "element",
                tagName: "h1",
                text: "Join our news letter"
            },
            {
                type: "element",
                tagName: "input",
                attributes: [{type: "input"}, {placeholder:"email address"}]
            },
            {
                type: "text",
                text: "Submit email"
            },
            {
                type: "element",
                tagName: "button",
                text: "Submit form",
                events: { // adding event listener
                    click: (event) => console.log('submit form')
                }
            }
        ]   
    }
    // use this to render the generated array to the body element
    // o2h.render(o2h, document.querySelector('body') )

    const html = o2h.render(o2h)
    console.log(html)

Output

<div class="container example" style="background: blue;">
    <!--the following code is an h1 tag-->
    <h1 data-name="news letter text">Join our news letter</h1>
    <input type="input" placeholder="email address">
    Submit email
    <button>Submit form</button>
</div>

the event will be tied to the button element and will be triggered once the button is clicked. childNode consist of Text, Element and Comment, the rendering sequence is determined by how the nodes are stacked.

you can also render the object directly to a parent element.

    o2h.render(obj, document.querySelector('body'))

Whilst text can be rendered as a as a childNode it can also be inserted directly to the element. using the text key.

    import o2h from 'object-2-html'

    const obj = {
        type: "element",
        tagName: "h1",
        text: "Hello Word", // render text directly to h1 element
        style: {color: "blue"}
    }

Result

    <h1 style="color:blue">Hello Word</h1>

Revert html to object

you can revert html elements to object using the undoRender method.


<h1 class="header-title" title="object to html" > object to html</h1>

<script>
    o2h.undoRender(document.querySelector('h1'))
</script>

Output

    {
        type: "element",
        tagName: "h1",
        attributes: [{title: "object to html"}],
        classes: ["header-title"],
        childNodes: [
            {
                type: "text",
                text: "object to html"
            }
        ]
    }