Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/deraen/random-lodash-mixins
Some functions Lodash is missing but might be useful when using e.g. AngularJS + Socket.io
https://github.com/deraen/random-lodash-mixins
Last synced: 2 days ago
JSON representation
Some functions Lodash is missing but might be useful when using e.g. AngularJS + Socket.io
- Host: GitHub
- URL: https://github.com/deraen/random-lodash-mixins
- Owner: Deraen
- License: mit
- Created: 2013-07-11T14:56:31.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2015-05-27T15:46:25.000Z (over 9 years ago)
- Last Synced: 2024-09-23T15:17:31.386Z (10 days ago)
- Language: JavaScript
- Homepage:
- Size: 234 KB
- Stars: 6
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Random functions [![Build Status](https://travis-ci.org/Deraen/random-lodash-mixins.svg)](https://travis-ci.org/Deraen/random-lodash-mixins)
Some functions Lo-Dash is missing but I have found useful.
Documentation contains some usage examples.
In addition to simple examples there are some particulary
good use cases using AngularJS + Socket.io.## Array manipulation
I removed these functions as Lo-Dash already provides [_.remove](http://lodash.com/docs#remove)
and append and prepend can be done with native JS and Lo-Dash.Below are examples to use Array.unshift and push.
### prepend
```
var a = [1, 2];
var arrayRef = [];
var unshift = arrayRef.unshift;// Single element
a.unshift(-1); // a = [-1, 1, 2];// Callback
socket.on('users:new', _.bind(_.call, $scope.users.unshift, $scope.users)); // or
socket.on('users:new', _.bind(_.call, unshift, $scope.users));// Multiple elements
unshift.apply(a, [-3, -2]); // a = [-3, -2, -1, 1, 2];// Callback
socket.on('users:list', _.bind(_.apply, $scope.users.unshift, $scope.users)); // or
socket.on('users:list', _.bind(_.apply, unshift, $scope.users));
```### append
```
var push = arrayRef.push;// Use $scope.users.push / .push / etc.
```## Object manipulation
### _.set(object, key, value)
Set property in target object to given value
```
var a = {a: 1, b: 2};
_.set(a, 'a', 3); // a = {a: 3, b: 2};
``````
socket.on('state', function (data) {
$scope.state = data;
});
// or
socket.on('state', _.partial(_.set, $scope, 'state'));
```### _.get(object, key)
Alias for [_.result](http://lodash.com/docs#result).### _.flattenObj(object[, separator])
Returns a new object.Default separator is '.'
```
var a = {a: {b: {c: {d: 1}}}, e: {f: {g: 2}, c: 3}};
_.flattenObj(a); // => {'a.b.c.d': 1, 'e.f.g': 2, 'e.c': 3};
```### _.unflattenObj(object[, separator])
Returns a new object.Default separator is '.'
```
var a = {'a.b.c.d': 1, 'e.f.g': 2, 'e.c': 3};
_.unflattenObj(a); // => {a: {b: {c: {d: 1}}}, e: {f: {g: 2}, c: 3}}
```## Misc
### _.construct(Constructor, value)
If value is defined use given constructor to create a new Object else return undefined.
```
new Date(undefined) // = Invalid Date
// or
_.construct(Date, undefined) // = undefined
```### _.clean
Returns a deep copy of the object with empty or undefined properties removed.
```
var q = {user: user._id};
if (request.params.from) q.from = {$lt: new Date(request.params.from)};
// or
var q = _.clean({
user: user._id,
from: {$lt: _.construct(Date, request.params.from)} // request.params.from = undefined
}); // q = {user: 'foo'}
```Though in this case the first in fact looks better...