Skip to content

Instantly share code, notes, and snippets.

@tedkulp
Last active August 29, 2015 14:03
Show Gist options
  • Save tedkulp/bba32bbcdeb73d617739 to your computer and use it in GitHub Desktop.
Save tedkulp/bba32bbcdeb73d617739 to your computer and use it in GitHub Desktop.
Code for "A few reasons to use Underscore.js (or Lo-Dash)"
function capAndJoinWords(aryOfWords) {
return _.chain(aryOfWords)
.map(function(item) { return item + '123'; })
.map(function(item) { return item.toUpperCase(); })
.reverse()
.value()
.join(' ');
}
capAndJoinWords(['array', 'this', 'in', 'are', 'words', 'test']); // "TEST123 WORDS123 ARE123 IN123 THIS123 ARRAY123"
function somethingImporatant() {
return _.tap(this.getObject(), function(obj) {
obj.val = "Value";
doSomething(obj);
});
}
function doAThing(boolArg) {
if (!_.isUndefined(boolArg) && !boolArg) {
// This would fire only if boolArg was sent something, and that something was falsy
}
}
function capAndJoinWords(aryOfWords) {
var result = [];
if (aryOfWords.length) {
for (i = aryOfWords.length - 1; i >= 0; i--) {
result.push((aryOfWords[i] + '123').toUpperCase());
}
}
return result.join(' ');
}
capAndJoinWords(['array', 'this', 'in', 'are', 'words', 'test']); // "TEST123 WORDS123 ARE123 IN123 THIS123 ARRAY123"
function somethingImporatant() {
var tmp = this.getObject();
tmp.val = "Value";
doSomething(val);
return tmp;
}
function doAThing(boolArg) {
if (!boolArg) {
// This would fire if boolArg was never sent or was sent with "false"
}
}
_.mixin({
dumbify: function(items) {
return _.chain(items)
.map(function(item) { return item + '123'; })
.map(function(item) { return item.toUpperCase(); })
.reverse()
.value();
}
});
function capAndJoinWordsDirectly(aryOfWords) {
return _.dumbify(aryOfWords).join(' ');
}
capAndJoinWordsDirectly(['array', 'this', 'in', 'are', 'words', 'test']); // "TEST123 WORDS123 ARE123 IN123 THIS123 ARRAY123"
_.mixin({
dumbify: function(items) {
return _.chain(items)
.map(function(item) { return item + '123'; })
.map(function(item) { return item.toUpperCase(); })
.reverse()
.value();
}
});
function capAndGetLastWord(aryOfWords) {
return _.chain(aryOfWords)
.dumbify()
.first()
.value();
}
capAndGetLastWord(['array', 'this', 'in', 'are', 'words', 'test']); // "TEST123"
function doAthing(obj, params) {
if (params && params.something) {
// Do something here
}
if (params && params.somethingElse && params.somethingElse === 'some_value') {
// Do another thing
}
// Repeat every time you need to look at one params value
}
doAThing(123, {
something: 'new',
})
function doAthing(obj, params) {
params || (params = {});
_.defaults(params, {
something: 'thing',
somethingElse: 'else',
oneMore: 'this'
})
console.log(params) // { something: 'new', somethingElse: 'else', oneMore: 'this' }
}
doAThing(123, {
something: 'new',
})
function doAthing(obj, params) {
params || (params = {});
if (!params.something)
params.something = 'thing';
if (!params.somethingElse)
params.somethingElse = 'else';
if (!params.oneMore)
params.oneMore = 'this';
console.log(params) // { something: 'new', somethingElse: 'else', oneMore: 'this' }
}
doAThing(123, {
something: 'new',
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment