Simplify API calls and array manipulation. Replace .map(), .filter(), .find(), .reduce() with clean $ syntax. Lightweight, zero-config, beginner-friendly.
npm install dollarjs-util
Simplify API calls and array manipulation in JavaScript.
Replace .map(), .filter(), .find(), .reduce() with clean $ syntax.
---
``bash`
npm install dollarjs-util
`javascript`
const $ = require('dollarjs-util');
---
| Task | Traditional Way | With dollarjs-util |
|------|-----------------|-------------------|
| Extract property | users.map(u => u.name) | $(users).name |users.filter(u => u.active === true)
| Filter array | | $.filter(users, 'active', true) |users.find(u => u.id === 5)
| Find item | | $.find(users, 'id', 5) |arr.reduce((s,x) => s + x.price, 0)
| Sum values | | $.sum(arr, 'price') |$.groupBy(arr, 'category')
| Group items | Complex reduce logic | |
---
`javascript`
const posts = await $('https://jsonplaceholder.typicode.com/posts');
// Output: [{ userId: 1, id: 1, title: '...', body: '...' }, ...]
`javascript`
const newPost = await $('https://api.example.com/posts', {
method: 'POST',
data: { title: 'My Post', body: 'Content here' }
});
// Output: { id: 101, title: 'My Post', body: 'Content here' }
`javascript`
const updated = await $('https://api.example.com/posts/1', {
method: 'PUT',
data: { title: 'Updated Title' }
});
`javascript`
await $('https://api.example.com/posts/1', { method: 'DELETE' });
`javascript`
const data = await $('https://api.example.com/search', {
headers: { 'Authorization': 'Bearer token123' },
params: { q: 'javascript', limit: 10 },
timeout: 5000
});
`javascript`
try {
const data = await $('https://api.example.com/data');
} catch (error) {
console.log(error.message); // Error message
console.log(error.status); // HTTP status code
console.log(error.data); // Response body
}
---
Extract properties from arrays without .map():
`javascript
const users = [
{ name: 'John', age: 25, city: 'NYC' },
{ name: 'Jane', age: 30, city: 'LA' },
{ name: 'Bob', age: 35, city: 'Chicago' }
];
$(users).name; // ['John', 'Jane', 'Bob']
$(users).age; // [25, 30, 35]
$(users).city; // ['NYC', 'LA', 'Chicago']
`
`javascript`
const doubled = $([1, 2, 3], x => x * 2);
// Output: [2, 4, 6]
---
`javascript
const users = [
{ name: 'John', active: true },
{ name: 'Jane', active: false },
{ name: 'Bob', active: true }
];
$.filter(users, 'active', true);
// Output: [{ name: 'John', active: true }, { name: 'Bob', active: true }]
$.filter(users, u => u.name.length > 3);
// Output: [{ name: 'John', ... }, { name: 'Jane', ... }]
`
`javascript
const products = [
{ id: 1, name: 'Laptop', price: 999 },
{ id: 2, name: 'Phone', price: 699 }
];
$.find(products, 'id', 1);
// Output: { id: 1, name: 'Laptop', price: 999 }
$.find(products, p => p.price > 900);
// Output: { id: 1, name: 'Laptop', price: 999 }
`
`javascript
const users = [
{ name: 'John', verified: true },
{ name: 'Jane', verified: false }
];
$.some(users, 'verified', true); // true
$.every(users, 'verified', true); // false
`
`javascript
const items = ['a', 'b', 'c', 'd', 'e'];
$.first(items); // 'a'
$.first(items, 3); // ['a', 'b', 'c']
$.last(items); // 'e'
$.last(items, 2); // ['d', 'e']
`
`javascript
const users = [
{ id: 1, name: 'John', password: 'secret' }
];
$.pick(users, ['name']);
// Output: [{ name: 'John' }]
$.omit(users, ['password']);
// Output: [{ id: 1, name: 'John' }]
`
`javascript
const orders = [
{ item: 'Book', price: 20 },
{ item: 'Pen', price: 5 },
{ item: 'Notebook', price: 15 }
];
$.sum(orders, 'price'); // 40
$.avg(orders, 'price'); // 13.33
$.sum([10, 20, 30]); // 60
$.avg([10, 20, 30]); // 20
`
`javascript
$.min([5, 2, 8, 1]); // 1
$.max([5, 2, 8, 1]); // 8
const products = [
{ name: 'A', price: 100 },
{ name: 'B', price: 50 }
];
$.min(products, 'price'); // { name: 'B', price: 50 }
$.max(products, 'price'); // { name: 'A', price: 100 }
`
`javascript
const users = [
{ name: 'John', active: true },
{ name: 'Jane', active: false },
{ name: 'Bob', active: true }
];
$.count(users); // 3
$.count(users, 'active', true); // 2
`
`javascript
$.unique([1, 2, 2, 3, 3, 3]); // [1, 2, 3]
const users = [
{ id: 1, city: 'NYC' },
{ id: 2, city: 'LA' },
{ id: 3, city: 'NYC' }
];
$.unique(users, 'city');
// Output: [{ id: 1, city: 'NYC' }, { id: 2, city: 'LA' }]
`
`javascript
const employees = [
{ name: 'John', dept: 'Engineering' },
{ name: 'Jane', dept: 'Marketing' },
{ name: 'Bob', dept: 'Engineering' }
];
$.groupBy(employees, 'dept');
// Output: {
// Engineering: [{ name: 'John', ... }, { name: 'Bob', ... }],
// Marketing: [{ name: 'Jane', ... }]
// }
`
`javascript
const products = [
{ name: 'Laptop', price: 999 },
{ name: 'Phone', price: 699 },
{ name: 'Tablet', price: 499 }
];
$.sortBy(products, 'price');
// Output: [Tablet, Phone, Laptop]
$.sortBy(products, 'price', 'desc');
// Output: [Laptop, Phone, Tablet]
`
`javascript`
$.chunk([1, 2, 3, 4, 5], 2);
// Output: [[1, 2], [3, 4], [5]]
`javascript
$.flatten([[1, 2], [3, 4]]);
// Output: [1, 2, 3, 4]
$.flatten([[1, [2, [3]]]], 2);
// Output: [1, 2, 3]
`
---
`javascript`
async function getDashboardStats() {
const orders = await $('https://api.example.com/orders');
return {
totalOrders: $.count(orders),
completedOrders: $.count(orders, 'status', 'completed'),
totalRevenue: $.sum(orders, 'amount'),
avgOrderValue: $.avg(orders, 'amount'),
ordersByStatus: $.groupBy(orders, 'status')
};
}
`javascript
const users = await $('https://api.example.com/users');
const activeEmails = $($.filter(users, 'active', true)).email;
const byCity = $.groupBy(users, 'city');
const topUser = $.max(users, 'score');
const safeData = $.omit(users, ['password', 'token']);
`
---
| Syntax | Returns | Description |
|--------|---------|-------------|
| $(url) | Promise | GET request |$(url, options)
| | Promise | HTTP request with options |$(array)
| | Proxy | Magic property access |$(array, fn)
| | Array | Map with function |
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| method | string | 'GET' | HTTP method |data
| | any | - | Request body |headers
| | object | {} | Request headers |params
| | object | - | Query parameters |timeout
| | number | 30000 | Timeout in ms |
| Method | Description |
|--------|-------------|
| $.filter(arr, key, value) | Filter by property |$.find(arr, key, value)
| | Find single item |$.some(arr, key, value)
| | Check if any match |$.every(arr, key, value)
| | Check if all match |$.first(arr, n?)
| | Get first n items |$.last(arr, n?)
| | Get last n items |$.pick(arr, keys)
| | Keep specified properties |$.omit(arr, keys)
| | Remove specified properties |$.sum(arr, key?)
| | Sum values |$.avg(arr, key?)
| | Average values |$.min(arr, key?)
| | Get minimum |$.max(arr, key?)
| | Get maximum |$.count(arr, key?, value?)
| | Count items |$.unique(arr, key?)
| | Remove duplicates |$.groupBy(arr, key)
| | Group by property |$.sortBy(arr, key, order?)
| | Sort by property |$.chunk(arr, size)
| | Split into chunks |$.flatten(arr, depth?)` | Flatten arrays |
|
---
MIT License - Newton School of Technology
Created by Adarsh Priydarshi