Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

import some basic functions of mine

  • Loading branch information...
commit c81e985627970b06513042c67eaba451097329d4 1 parent 4067525
@ratbeard authored
View
4 test/flexargs.js
@@ -1,8 +1,6 @@
$(document).ready(function() {
// msg as last arg is annoying
- function it (msg, actual, expected) {
- same(actual, expected, msg);
- }
+ var it = function (msg, actual, expected) { same(actual, expected, msg); };
var dogs = [
{name: 'bubbles', age: 5},
View
78 test/scraps.js
@@ -1,15 +1,77 @@
$(document).ready(function() {
+ var it = function (msg, actual, expected) { same(actual, expected, msg); };
+ var dogs = [
+ {name: 'bubbles', age: 5},
+ {name: 'lily', age: 5},
+ {name: 'ginger', age: 9}
+ ];
+
module("Scraps ");
- test("arrays: first", function() {
- equals(_.first([1,2,3]), 1, 'can pull out the first element of an array');
- equals(_([1, 2, 3]).first(), 1, 'can perform OO-style "first()"');
- equals(_.first([1,2,3], 2).join(', '), '1, 2', 'can pass an index to first');
- var result = (function(){ return _.first(arguments); })(4, 3, 2, 1);
- equals(result, 4, 'works on an arguments object.');
- result = _.map([[1,2,3],[1,2,3]], _.first);
- equals(result.join(','), '1,1', 'works well with _.map');
+ test("memo", function() {
+ it('is reduce without a return',
+ _.memo(dogs, [], function (memo, dog) {
+ memo.push(dog);
+ }),
+ _.reduce(dogs, [], function (memo, dog) {
+ memo.push(dog);
+ return memo;
+ })
+ );
+ it('makes the common use cases for reduce simpler',
+ _.memo(dogs, {}, function (memo, dog) {
+ memo[dog.name] = dog.age;
+ }),
+ _.reduce(dogs, {}, function (memo, dog) {
+ memo[dog.name] = dog.age;
+ return memo;
+ })
+ );
+ });
+
+
+ test('objects: get', function () {
+ var obj = {name: 'sprout'}
+ same('sprout', _.get(obj, 'name'), 'can get a property')
+ same(undefined, _.get(obj, 'is'), 'returns undefined for unknown property')
+ same('cat', _.get(obj, 'is', 'cat'), 'can give a default value to return if unknown property')
+ same('sprout', _.get(obj, 'name', 'bubbles'), 'default value not used if property exists')
+ same(undefined, _.get(undefined, 'name'), 'calling on undefined does not throw error')
+ same(undefined, _.get(null, 'name'), 'calling on null does not throw error')
+ same('ritz', _.get(undefined, 'name', 'ritz'), 'useful for default value on undefined')
+ same('ritz', _.get(null, 'name', 'ritz'), 'useful for default value on null')
+ same(null, _.get({a: null}, 'a', 'def'), 'will return explicit null on a property')
+ });
+
+ test('objects: set', function () {
+ var result, obj = {};
+ it('can set a value',
+ _.set(obj, 'age', 1),
+ {age: 1}
+ );
+ it('chains nicely',
+ _.set(obj, 'str', 22).age,
+ 1
+ );
+ it('called on undefined creates a new object',
+ _.set(undefined, 'age', 1),
+ {age: 1}
+ );
+
+ function tedious(k, v) {
+ var tmp = {};
+ tmp[k] = v;
+ return tmp;
+ }
+ it('is useful to make an object with dynamic keys',
+ _.set({}, 'a', 1),
+ tedious('a',1)
+ );
+ });
+
+ test('objects: prop', function () {
+
});
});
View
2  test/test.html
@@ -10,8 +10,10 @@
<script src="../underscore.js"></script>
<!-- code -->
<script src="../underscore.flexargs.js"></script>
+ <script src="../underscore.scraps.js"></script>
<!-- tests -->
<script src="flexargs.js"></script>
+ <script src="scraps.js"></script>
</head>
<body>
<h1 id="qunit-header">Underscore Test Suite</h1>
View
38 underscore.scraps.js
@@ -0,0 +1,38 @@
+// version of reduce that doesn't require caller to return the memo
+// each iteration. Especially handy since the common things you do
+// in reduce take two lines (adding an item to am object or array)
+_.memo = function (obj, memo, iterator, context) {
+ _.each(obj, function (value, index, list) {
+ iterator(memo, value, index, list);
+ }, context);
+ return memo;
+};
+
+// a safe get, with optional default value thats more rigorious than: ||
+_.get = function (obj, key, defaultValue) {
+ return (obj && key in obj) ? obj[key] : defaultValue;
+};
+
+// a setting a property chainable. create an obj for you if null / undefined
+_.set = function (obj, key, value) {
+ if (obj == null) obj = {};
+ obj[key] = value;
+ return obj;
+};
+
+// make delete chainable
+_.del = function (obj, key) {
+ if (obj) delete obj[key];
+ return obj;
+}
+
+// get/set in one function, like jQuery
+_.prop = function (obj, key, value) {
+ return _[arguments.length === 3 ? 'set' : 'get'](obj, key, value);
+};
+
+// make [].push chainable
+_.push = function (array, value) {
+ array.push(value);
+ return array;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.