Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
no more handle() method
no need to reinstantiate elements on search, just "push" context
`new $` now returns an empty elements instance
list now lives in elements
  • Loading branch information
kamicane committed Nov 2, 2012
1 parent 646e0d0 commit 16993d1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 50 deletions.
54 changes: 22 additions & 32 deletions lib/elements.js
Expand Up @@ -2,7 +2,8 @@
elements
*/"use strict"

var prime = require("prime/prime")
var prime = require("prime/prime"),
array = require("prime/es5/array").prototype

// uniqueID

Expand All @@ -13,22 +14,21 @@ var uniqueID = function(n){

var instances = {}

// `search` is the selector engine
// `sort` is the elements sorter

var search, sort

// the exposed prime
// elements prime

var $ = prime({constructor: function $(n, context){

if (n == null) return null
if (n instanceof elements) return n

if (typeof n === "string") return $.prototype.search ? $(context || document).search(n) : null
if (n == null) return (this && this.constructor === $) ? new elements : null
if (n.constructor === elements) return n

var self = new elements, uid

if (typeof n === "string"){
if (!self.search) return null
self[self.length++] = context || document
return self.search(n)
}

if (n.nodeType || n === global){

self[self.length++] = n
Expand All @@ -39,6 +39,7 @@ var $ = prime({constructor: function $(n, context){
// including another instance of elements from another interface.

var uniques = {}

for (var i = 0, l = n.length; i < l; i++){ // perform elements flattening
var nodes = $(n[i], context)
if (nodes && nodes.length) for (var j = 0, k = nodes.length; j < k; j++){
Expand Down Expand Up @@ -68,9 +69,6 @@ var $ = prime({constructor: function $(n, context){

}})

// the resulting prime
// this also makes it hard to override these prototypes

var elements = prime({

inherits: $,
Expand All @@ -79,28 +77,20 @@ var elements = prime({
this.length = 0
},

handle: function handle(method){
var buffer = [], length = this.length, res

if (length === 1){
res = method.call(this, this[0], 0, buffer)
if (res != null && res !== false && res !== true) buffer.push(res)
} else for (var i = 0; i < length; i++){
var node = this[i]
res = method.call(this, node, i, buffer)

if (res === false || res === true) break
if (res != null) buffer.push(res)
}
return buffer
},

unlink: function(){
return this.handle(function(node, i){
return this.map(function(node, i){
delete instances[uniqueID(node)]
return node
})
}
},

// straight es5 prototypes (or emulated methods)

forEach: array.forEach,
map: array.map,
filter: array.filter,
every: array.every,
some: array.some

})

Expand Down
18 changes: 0 additions & 18 deletions lib/list.js

This file was deleted.

0 comments on commit 16993d1

Please sign in to comment.