Skip to content

Commit

Permalink
fix: work on filters
Browse files Browse the repository at this point in the history
  • Loading branch information
learosema committed Nov 4, 2024
1 parent db748c4 commit 84edc90
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/builtin-filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export function sort(array) {
* @param {Iterable} array
* @param {number} amount
*/
export function last(amount = 1) {
export function last(array, amount = 1) {
return Array.from(array).reverse().slice(0, amount);
}

Expand Down
25 changes: 23 additions & 2 deletions tests/builtin-filters.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
import { describe, test } from 'node:test'
import assert from 'node:assert/strict'

import { json } from '../src/builtin-filters.js'
import { json, limit, last, async, each } from '../src/builtin-filters.js'

describe('built-in filters', () => {
test('json', () => {
assert.equal(json({name: 'Lea'}), '{"name":"Lea"}');
});


test('limit', () => {
const array = [1, 2, 3, 4, 5];
assert.deepEqual(limit(array, 3), [1,2,3]);
});

test('last', () => {
const array = [1, 2, 3, 4, 5];
assert.deepEqual(last(array), [5]);
});

test('each', () => {
const array = [1, 2, 3];
const li = (item) => `<li>${item}</li>`
assert.deepEqual(each(array, li),
'<li>1</li><li>2</li><li>3</li>'
);
});

test('async', async () => {
const promised = Promise.resolve(42);
assert.equal(await async(promised), 42);
});

});

0 comments on commit 84edc90

Please sign in to comment.