vladocar / femtojs Goto Github PK
View Code? Open in Web Editor NEWfemtoJS - Really small JavaScript (ES6) library for DOM manipulation.
Home Page: https://vladocar.github.io/femtoJS/
License: MIT License
femtoJS - Really small JavaScript (ES6) library for DOM manipulation.
Home Page: https://vladocar.github.io/femtoJS/
License: MIT License
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()
}
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?
Currently this library pollutes the window
object with global variables. A few suggestions:
$
instance instead of window, and return $
If you're going to use es6 syntax, you should be using const
instead of var
.
I highly recommend you set up eslint as well, with a common config like standardjs or airbnb
Hey,
Could it be possible, that the version I download from npm is not up to date compared to this repository? Especially the module export part seems to be missing in the file I get from npm:
https://github.com/vladocar/femtoJS/blob/master/src/femtoJS.js#L103
Could you update the version on npm?
Hi.
Do you have some instructions for contributing to the code?
Hi.
There is no way, using the framework, to remove listeners registered with on()
method.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.