GithubHelp home page GithubHelp logo

vladocar / femtojs Goto Github PK

View Code? Open in Web Editor NEW
142.0 8.0 4.0 77 KB

femtoJS - Really small JavaScript (ES6) library for DOM manipulation.

Home Page: https://vladocar.github.io/femtoJS/

License: MIT License

HTML 25.17% JavaScript 70.10% CSS 4.74%
javascript ecmascript6 es6 es6-javascript dom jquery

femtojs's Introduction

femtoJS

License MIT Gzip Size npm Join the chat at https://gitter.im/femtoJS/community

Small JS (ES6) library for DOM manipulation.

So, what is femtoJS? femtoJS is a JavaScript library for basic DOM manipulation. It has a jQuery-inspired syntax and supports chaining.

femtoJS is about 100 lines of code in size, and the entire library weighs in at just under 0.9kB compressed and gzipped.

It works in all the browsers that support ES6. If you need to support older browsers take a look into my previous project nanoJS.

You can use direct download or:

npm i femtojs
yarn add femtojs
<script src="https://unpkg.com/[email protected]/src/femtoJS.min.js"></script>

OR

<script src="https://cdn.jsdelivr.net/npm/[email protected]/src/femtoJS.min.js"></script>

Utils:

addClass
attr
css
empty
getAttr
html
insertAfter, insertBefore, insertFirst insertLast
offset
on
parent
removeAttr
removeClass
text
toggleClass

You can find it here some basic examples.

Simple demo

Project femtoJS

License

This project is licensed under the MIT License

femtojs's People

Contributors

gitter-badger avatar logandark avatar vladocar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

femtojs's Issues

Don't pollute the global namespace

Currently this library pollutes the window object with global variables. A few suggestions:

  • Wrap in an agnostic module wrapper to support commonjs, es6, and umd/amd modules.
  • Attach the methods to the $ instance instead of window, and return $

Idea not real issue. Feel free to close.

Saw your project, very nice, and played with the code for a bit, I shuffled some things around and made it smaller and so there's no use of "this" (for users' convenience and preventing binding issues). Instead of checking for arrays, you can just assume a value is array like and go with it, this makes the code a bit smaller/simpler and could make it a little faster since you're not checking and converting (e.g. [...selectAll()]). Just some thoughts Good job with femto, all the best!

const func = (() => {
	const argToElements = src => {
        if (src != null) {
            if (src.isFemtoJS) return src.sel()
            if (typeof src === 'string') {
                const tagName = /^<(\w+)>$/.exec(src)
                return tagName != null
                    ? [document.createElement(tagName[1])]
                    : document.querySelectorAll(src)
            }
            if (src instanceof Element) return [src]
            if (src.length) {
                const elems = []
                for (let i = 0; i < src.length; i++) elems.push(...argToElements(src[i]))
                return elems
            }
        }

             throw new TypeError('Expected string | HTMLElement | Array | femtoJS, instead got ' + typeof src)
	}

	const $ = function () {
	const sel = argToElements(arguments)

	const iter = fn => {
            for (let i = 0; i < sel.length; i++) fn(sel[i], i)
            return O
        }

        const insertToAdjacent = s => e => iter((j, i) => i === 0 ?
            e instanceof Element ?
            e.insertAdjacentElement(s, j) :
            e.sel()[0].insertAdjacentElement(s, j) :
            sel[0].insertAdjacentElement('afterend', j))

        const insertAdjacent = s => sOrE => {
            if (typeof sOrE !== 'string') {
                if (sOrE instanceof Element) {
                    sel[0].insertAdjacentElement(s, sOrE)
                } else if (sOrE.isFemtoJS) {
                    const osel = sOrE.sel()

                    sel[0].insertAdjacentElement(s, osel[0])

                    for (let i = 1; i < osel.length; i++) {
                        osel[0].insertAdjacentElement('afterend', osel[i])
                    }
                }
            } else {
                sel[0].insertAdjacentHTML(s, sOrE)
            }

            return O
        }
    
        const O = {
			on:           (type, fn) => iter(i => i.addEventListener(type, fn)),
			off:          (type, fn) => iter(i => i.removeEventListener(type, fn)),
			css:          s => iter(i => i.style.cssText += s),
			html:         h => iter(i => i.innerHTML = h),
			text:         t => iter(i => i.innerText = t),
			addClass:     t => iter(i => i.classList.add(t)),
			toggleClass:  t => iter(i => i.classList.toggle(t)),
			removeClass:  t => iter(i => i.classList.remove(t)),
			empty:        () => iter(i => i.innerHTML = ''),
			attr:         (k, v) => iter(i => i.setAttribute(k, v)),
			removeAttr:   k => iter(i => i.removeAttribute(k)),
			parent:       () => iter((e, i) => { sel[i] = e.parentNode }),
			remove:       () => iter(i => i.remove()),

			before:       insertAdjacent('beforebegin'),
			after:        insertAdjacent('afterend'),
			first:        insertAdjacent('afterbegin'),
			last:         insertAdjacent('beforeend'),
			insertBefore: insertToAdjacent('beforebegin'),
			insertAfter:  insertToAdjacent('afterend'),
			insertFirst:  insertToAdjacent('afterbegin'),
			insertLast:   insertToAdjacent('beforeend'),

			prepend:      insertAdjacent('afterbegin'),
			append:       insertAdjacent('beforeend'),

			getAttr:      v => sel[0].getAttribute(v),
			offset:       () => sel[0].getBoundingClientRect(),
			sel:          () => sel,

			isFemtoJS: true
		}

		return O
	}

	$.fragment = () => $(document.createDocumentFragment())

	return $
})

if (typeof module === 'object' && module.exports) {
	module.exports = func()
} else if (typeof define === 'function' && define.amd) {
	const singleton = func()

	define('femtoJS', [], () => singleton)
} else {
	window.$ = func()
}

what is `s`?

I see a bunch of functions using the variable s, well I don't see it defined anywhere, what is s?? Does this mean every instance of the object shares the exact same state?

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.