patternjavascriptMinor
Atomic elements selector engine
Viewed 0 times
selectorengineelementsatomic
Problem
I've written a DOM element selector engine that I'm really quite happy with, and I'd love to hear some opinions on it from my fellow JavaScripters :D
It's called Atomic, and I've got a repository for it over at GitHub - and I've also included the code below...
Check it out (if you're in the mood) and let me know what you think. I'd appreciate any and all feedback!
```
/* Atomic Elements Selector Engine v0.1 (Beta)
Copyright (c) 2012 Craig Pierce
http://www.atomicjs.com */
/* THIS SOFTWARE HAS BEEN RELEASED IN "AS IS" CONDITION, WITHOUT ANY
WARRANTY, AND IS AVAILABLE UNDER THE AS YOU WISH PUBLIC LICENSE. */
/* As You Wish Public License
Copyright (c) 2012 Craig Pierce
http://www.underctrl.com/as-you-wish/
Use and/or distribute exact copies of this license as you wish.
Use and/or distribute exact copies of any works covered by this
license as you wish.
Modify any works covered by this license as you wish, so long
as you:
0. Retain for attribution all applicable copyrights and/or
authorship notices.
1. Use and/or distribute all resulting works under at least
the same licenses as the originating work. */
;(function buildAtomicEngine(global, undefined){
"use strict";
var version = "0.1",
dom = global.document,
domReady = false,
readyTimer = setInterval(checkDomReady, 50),
readyCallbacks = [],
atomicCache = {},
cacheableRegex = /^(#document|body|html)$/i,
readyStateRegex = /^(loaded|interactive|complete)$/i,
commaSplitRegex = /\s,\s(?=(?:[^\)]|\([^\)]\))$)/,
selectorReplaceRegex = new RegExp(
"=([^'\"]+?)\\]|" +
"\\[(.+?!=.+?)\\]|" +
":not\\((.+?,.+?)\\)|" +
"^([>~\\s\\+])|" +
"([>~\\s\\+])$|" +
":(even|odd|selected|text|password|checkbox|radio|button|submit|reset|image|file|hidden)"
, "gi");
global.atomic =
It's called Atomic, and I've got a repository for it over at GitHub - and I've also included the code below...
Check it out (if you're in the mood) and let me know what you think. I'd appreciate any and all feedback!
```
/* Atomic Elements Selector Engine v0.1 (Beta)
Copyright (c) 2012 Craig Pierce
http://www.atomicjs.com */
/* THIS SOFTWARE HAS BEEN RELEASED IN "AS IS" CONDITION, WITHOUT ANY
WARRANTY, AND IS AVAILABLE UNDER THE AS YOU WISH PUBLIC LICENSE. */
/* As You Wish Public License
Copyright (c) 2012 Craig Pierce
http://www.underctrl.com/as-you-wish/
Use and/or distribute exact copies of this license as you wish.
Use and/or distribute exact copies of any works covered by this
license as you wish.
Modify any works covered by this license as you wish, so long
as you:
0. Retain for attribution all applicable copyrights and/or
authorship notices.
1. Use and/or distribute all resulting works under at least
the same licenses as the originating work. */
;(function buildAtomicEngine(global, undefined){
"use strict";
var version = "0.1",
dom = global.document,
domReady = false,
readyTimer = setInterval(checkDomReady, 50),
readyCallbacks = [],
atomicCache = {},
cacheableRegex = /^(#document|body|html)$/i,
readyStateRegex = /^(loaded|interactive|complete)$/i,
commaSplitRegex = /\s,\s(?=(?:[^\)]|\([^\)]\))$)/,
selectorReplaceRegex = new RegExp(
"=([^'\"]+?)\\]|" +
"\\[(.+?!=.+?)\\]|" +
":not\\((.+?,.+?)\\)|" +
"^([>~\\s\\+])|" +
"([>~\\s\\+])$|" +
":(even|odd|selected|text|password|checkbox|radio|button|submit|reset|image|file|hidden)"
, "gi");
global.atomic =
Solution
Some things I noticed:
(EDIT: accidentally put
- You don't need a semicolon after almost any case you use it after a
}(see https://stackoverflow.com/questions/2717949/javascript-when-should-i-use-a-semicolon-after-curly-braces ) - remove it after the}character in ALL cases exceptatomicCache = {};
- I personally prefer spaces between braces and items (e.g. use
} else {instead of}else{) - and many other people do too, I hope...
options = (options || {});can be simplified tooptions |= {};
lenx = readyCallbacks.length;has incorrect tab spacing before it (typo)
- It might be more clear to name the parameters
capture1,capture2, etc something more clear - or write some comments on the if/then lines explaining each case
- You might want to replace the
if (isCacheable)items withif (useCache)to avoid populating your cache when it's not going to be used
(EDIT: accidentally put
?= instead of |= for #3)Context
StackExchange Code Review Q#8979, answer score: 2
Revisions (0)
No revisions yet.