Skip to content

Commit 5a0ce1f

Browse files
committed
add map, filter and text
1 parent ae8afce commit 5a0ce1f

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ To solve the concept of the __context__ in calls to _jQuery_'s function `$`, __n
121121
- `get(index)`, that retrieves the object at position `index` in the collection.
122122
- `each(callback)`, that calls `callback` for each object in the collection.
123123
- callback is a function `callback(index, element)` that receives the index of the object in the collection and the object itself. The function is bound to the collection (so that `this` means the collection).
124+
- `map(callback)`, that calls `callback` for each object in the collection and returns the results in an array.
125+
- callback is a function `callback(index, element)` that receives the index of the object in the collection and the object itself. The function is bound to the collection (so that `this` means the collection).
126+
- `filter(callback)`, that filters the objects in the collection by calling `callback` for each object in the collection.
127+
- callback is a function `callback(index, element)` that receives the index of the object in the collection and the object itself. The function is bound to the collection (so that `this` means the collection). The function must return `true` to keep the object in the collection.
128+
- `text(text)`, that sets the text of the first object in the collection, or retrieves the text of the first object in the collection.
124129

125130
These functions are enabled to the collections resulting of calls to `_$`.
126131

dist/nojquery.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,31 @@ SOFTWARE.
330330
}
331331
return this;
332332
};
333+
$.map = function (callback) {
334+
let result = [];
335+
callback = callback.bind(this);
336+
for (let i = 0; i < this.length; i++) {
337+
result.push(callback(i, this[i]));
338+
}
339+
return result;
340+
};
341+
$.filter = function (callback) {
342+
let result = [];
343+
callback = callback.bind(this);
344+
for (let i = 0; i < this.length; i++) {
345+
if (callback(i, this[i])) {
346+
result.push(this[i]);
347+
}
348+
}
349+
return $(result);
350+
};
351+
$.text = function (text) {
352+
if (text === undefined) {
353+
return this[0].innerText;
354+
}
355+
this.forEach(x => x.innerText = text);
356+
return this;
357+
};
333358
$._$ = function (...elements) {
334359
return $.bind(this)(...elements);
335360
};

dist/nojquery.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/nojquery.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,56 @@
548548
return this;
549549
}
550550

551+
/**
552+
* Function that calls a function for each element in the collection and returns an array with the
553+
* results of the calls to the function. The function will receive two parameters: callback(index, element)
554+
* where <index> is the index of the element in the collection and <element> is the element itself. The function
555+
* is bound to the collection, so that this[index] will be the element.
556+
* @param {*} callback: the function to call for each element in the collection
557+
* @returns an array with the results of the calls to the function
558+
*/
559+
$.map = function(callback) {
560+
let result = [];
561+
callback = callback.bind(this);
562+
for (let i = 0; i < this.length; i++) {
563+
result.push(callback(i, this[i]));
564+
}
565+
return result;
566+
}
567+
568+
/**
569+
* Function that filters the elements in the collection that return "true"-ish when calling the callback function.
570+
* The callback function will receive two parameters: callback(index, element) where <index> is the index of the
571+
* element in the collection and <element> is the element itself. The function is bound to the collection, so that
572+
* this[index] will be the element.
573+
* The callback function should return true if the element passes the filter, false otherwise.
574+
* @param {*} callback: the function to filter the elements in the collection
575+
* @returns a new collection with the elements that pass the filter
576+
*/
577+
$.filter = function(callback) {
578+
let result = [];
579+
callback = callback.bind(this);
580+
for (let i = 0; i < this.length; i++) {
581+
if (callback(i, this[i])) {
582+
result.push(this[i]);
583+
}
584+
}
585+
return $(result);
586+
}
587+
588+
/**
589+
* Function that retrieves the text of the first element in the collection, or sets the text of all the elements
590+
* @param {*} text: the text to set for the elements
591+
* @returns the text of the first element in the collection or the collection of objects
592+
*/
593+
$.text = function(text) {
594+
if (text === undefined) {
595+
return this[0].innerText;
596+
}
597+
this.forEach((x) => x.innerText = text);
598+
return this;
599+
}
600+
551601
/**
552602
* The $ function scoped to the objects that are part of a previous $ object; this is the same
553603
* function $(...elements), except for the fact that the search for elements using selectors

0 commit comments

Comments
 (0)