Last active
August 29, 2015 14:03
-
-
Save tedkulp/bba32bbcdeb73d617739 to your computer and use it in GitHub Desktop.
Code for "A few reasons to use Underscore.js (or Lo-Dash)"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function somethingImporatant() { | |
return _.tap(this.getObject(), function(obj) { | |
obj.val = "Value"; | |
doSomething(obj); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function doAThing(boolArg) { | |
if (!_.isUndefined(boolArg) && !boolArg) { | |
// This would fire only if boolArg was sent something, and that something was falsy | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function somethingImporatant() { | |
var tmp = this.getObject(); | |
tmp.val = "Value"; | |
doSomething(val); | |
return tmp; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function doAThing(boolArg) { | |
if (!boolArg) { | |
// This would fire if boolArg was never sent or was sent with "false" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_.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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_.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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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