parasitejs
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

ParasiteJS

Use javascript the way you want, fast as possible.

module version  GitHub license  contributions welcome

Objective

Now, more than ever, you do not need jQuery to manipulate HTML elements and their events. The ParasiteJS library consumes HTML elements, nodelists, and collections by adding key query and iteration functions, focusing on performance and agility in development.

Performance

All added functions aim to approach vanilla performance.

Agility in Development

When talking about agility, vanilla is more inefficient than all query libraries (for this, they exist :P).

The javascript does not have functions to manipulate the lists returned by the search of elements (querySelectorAll, getElements ...) forcing you to create functions to do this.

The ParasiteJS functions are available in collections and elements making these functions always available into the elements. The functions are based on jQuery but are NOT compatible, so check out the documentation below.

Warning

This project does not objective the module creation. This is a lightweight and faster alternative than jQuery for "Do not use jQuery" projects where jQuery is not a dependency.

Install

    npm i parasitejs

How to use

Module

Require parasitejs and he goes modify lists, collections and elements:

    const { p$ } = require('parasitejs')
 
    p$(() => {
 
        // Same as document.find
        let $el = p$('tag.class#id[attr="value"]')
 
        $el = $el.find('selector')
 
        $el .findTag('tag')     // Return collection
            .findId('id')       // Return element
            .findClass('class') // Return collection 
            .find('selector')   // Return list
            .css({ style: 'value' });
 
    })

Web

Download script into /dist/web/ folder and import normally:

    <script type="text/javascript" src="parasite.min.js"/>
 
    <script>
        p$(() => { 
 
            // Same as document.find
            let $el = p$('tag.class#id[attr="value"]')
 
            $el = $el.find('selector')
 
            $el .findTag('tag')     // Return collection
                .findId('id')       // Return element
                .findClass('class') // Return collection 
                .find('selector')   // Return list
                .css({ style: 'value' });
 
        });
    </script> 

Pseudo jQuery-like Function

The ParasiteJS create a variable called p$. That provides static functions and an constructor.

jQuery-like Function

    // Find selector and return collection
    p$(selectorstring)ParasitedList | NodeList | HTMLCollection
 
    // on ready callback
    p$(handlerFunction)void
 
    // Array of HTMLElements
    p$(elemArrHTMLElement[])ParasitedList
 
    // HTML Element
    p$(elemHTMLElement)ParasitedList

Static Helpers

    // DOM Ready
    p$.ready(fnFunction)void
 
    // Each lists and collections with length
    p$.each(listArrayLike<any>, iterator: (keyIndex: string|number, value: any) => boolean)ArrayLike<any>

Javascript Helpers

    // Load other js files with javascript
    p$.require(filePathstring)void
 
    // Load javascript code
    p$.globalEval(codestring)void

AJAX

    // Ajax jQuery-like
    p$.ajax(url?: string, settingsAJAXSettings)Deferred
 
    // GET URL
    p$.get(urlstring, data?: any, successAJAXSuccess)Deferred
    p$.get(settingsAJAXSettings)Deferred
 
    // POST URL
    p$.post(urlstring, dataany, successAJAXSuccess)Deferred
    p$.post(settingsAJAXSettings)Deferred

Promise

    // Deferred jQuery-like
    p$.Deferred(beforeStart?: Function)Deferred

Functions

All

    let obj: Document | HTMLElement | Window | NodeList | HTMLCollection
 
    // Attach an event handler function for one or more events.
    obj.on(eventsstring, selector?: string, handlerFunction)this
 
    // Attach a handler to an event for the elements.
    // The handler is executed at most once per element
    // per event type.
    obj.one(eventsstring, selector?: string, handlerFunction)this
 
    // Remove an event handler.
    obj.off(eventsstring, selector?: string, handlerFunction)this
 
    // Execute all handlers and behaviors attached.
    obj.trigger(eventsstring, data?: any)this

Document, HTMLElement, NodeList, HTMLCollection

    let obj: Document | HTMLElement | NodeList | HTMLCollection
 
    // Execute getElementById (and filter if needed).
    obj.findId(idstring)Element | null
 
    // Execute getElementsByName.
    obj.findName(namestring)NodeList
 
    // Execute getElementsByTag.
    obj.findTag(tagstring)HTMLCollection
 
    // Execute getElementsByClass.
    obj.findClass(clssstring)HTMLCollection
 
    // Execute querySelector.
    obj.findOne(selectorstring)Element | null
 
    // Forced querySelectorAll.
    obj.findAll(selectorstring)NodeList
 
    // Optimized querySelectorAll (just execute
    // querySelectorAll if needed).
    obj.find(selectorstring)NodeList | HTMLCollection | ParasitedList

NodeList, HTMLCollection

    let list: NodeList | HTMLCollection
 
    // Reduce the set of matched elements by selector
    // or test function.
    list.filter(selectorstring | Function)ParasitedList
 
    // Retrieve one of the elements matched.
    list.get(index?: number)HTMLElement | void
 
    // Iterate over the list, executing a function
    // for each matched element.
    list.each(handlerEachIterator)this

HTMLElement

    let elem: HTMLElement
 
    // Set one or more attributes for the set
    // of matched elements or get first element attribute.
    elem.attr(attrsPlainObject | string, value?: string)this | string
 
    // Remove an attribute from each element
    // in the set of matched elements.
    elem.removeAttr(attrNamesstring)this
 
    // Set one or more properties for the set of
    // matched elements or get first element prop.
    elem.prop(propsPlainObject | string, value?: string)this | string
 
    // Remove a property for the set of matched elements.
    elem.removeProp(propNamesstring)this
 
    // Set the value of each element in the set of
    // matched elements or get first element value.
    elem.val(value?: string)this | string
 
    // Store arbitrary data associated with the matched
    // elements or get first element data.
    elem.data(dataPlainObject | string, value?: string)this | string
 
    // Get the computed style properties for the first
    // element in the set of matched elements.
    elem.css(stylesPlainObject | string, value?: string)this | string
 
    // Check the current matched set of elements for
    // selector or test function.
    elem.is(filterstring | Function)boolean

Author

License

Package Sidebar

Install

npm i parasitejs

Weekly Downloads

0

Version

1.0.3

License

MIT

Unpacked Size

41.4 kB

Total Files

5

Last publish

Collaborators

  • ecromaneli