Giter Site home page Giter Site logo

ecromaneli / parasitejs Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 56 KB

Use javascript the way you want, fast as possible.

License: MIT License

Shell 2.95% TypeScript 97.05%
parasite parasitejs dom event-listener jquery mquery dom-manipulation jquery-like lightweight vanilla

parasitejs's Introduction

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$(selector: string): ParasitedList | NodeList | HTMLCollection

    // on ready callback
    p$(handler: Function): void

    // Array of HTMLElements
    p$(elemArr: HTMLElement[]): ParasitedList

    // HTML Element
    p$(elem: HTMLElement): ParasitedList

Static Helpers

    // DOM Ready
    p$.ready(fn: Function): void

    // Each lists and collections with length
    p$.each(list: ArrayLike<any>, iterator: (keyIndex: string|number, value: any) => boolean): ArrayLike<any>

Javascript Helpers

    // Load other js files with javascript
    p$.require(filePath: string): void

    // Load javascript code
    p$.globalEval(code: string): void

AJAX

    // Ajax jQuery-like
    p$.ajax(url?: string, settings: AJAXSettings): Deferred

    // GET URL
    p$.get(url: string, data?: any, success: AJAXSuccess): Deferred
    p$.get(settings: AJAXSettings): Deferred

    // POST URL
    p$.post(url: string, data: any, success: AJAXSuccess): Deferred
    p$.post(settings: AJAXSettings): 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(events: string, selector?: string, handler: Function): this

    // Attach a handler to an event for the elements.
    // The handler is executed at most once per element
    // per event type.
    obj.one(events: string, selector?: string, handler: Function): this

    // Remove an event handler.
    obj.off(events: string, selector?: string, handler: Function): this

    // Execute all handlers and behaviors attached.
    obj.trigger(events: string, data?: any): this

Document, HTMLElement, NodeList, HTMLCollection

    let obj: Document | HTMLElement | NodeList | HTMLCollection

    // Execute getElementById (and filter if needed).
    obj.findId(id: string): Element | null

    // Execute getElementsByName.
    obj.findName(name: string): NodeList

    // Execute getElementsByTag.
    obj.findTag(tag: string): HTMLCollection

    // Execute getElementsByClass.
    obj.findClass(clss: string): HTMLCollection

    // Execute querySelector.
    obj.findOne(selector: string): Element | null

    // Forced querySelectorAll.
    obj.findAll(selector: string): NodeList

    // Optimized querySelectorAll (just execute
    // querySelectorAll if needed).
    obj.find(selector: string): NodeList | HTMLCollection | ParasitedList

NodeList, HTMLCollection

    let list: NodeList | HTMLCollection

    // Reduce the set of matched elements by selector
    // or test function.
    list.filter(selector: string | 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(handler: EachIterator): this

HTMLElement

    let elem: HTMLElement

    // Set one or more attributes for the set
    // of matched elements or get first element attribute.
    elem.attr(attrs: PlainObject | string, value?: string): this | string

    // Remove an attribute from each element
    // in the set of matched elements.
    elem.removeAttr(attrNames: string): this

    // Set one or more properties for the set of
    // matched elements or get first element prop.
    elem.prop(props: PlainObject | string, value?: string): this | string

    // Remove a property for the set of matched elements.
    elem.removeProp(propNames: string): 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(data: PlainObject | string, value?: string): this | string

    // Get the computed style properties for the first
    // element in the set of matched elements.
    elem.css(styles: PlainObject | string, value?: string): this | string

    // Check the current matched set of elements for
    // selector or test function.
    elem.is(filter: string | Function): boolean

Author

License

parasitejs's People

Contributors

ecromaneli avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.