Skip to content

Commit

Permalink
namespace is now a string method
Browse files Browse the repository at this point in the history
  • Loading branch information
kangax authored and kangax committed May 23, 2008
1 parent 8abdc3a commit 9af0da0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 17 deletions.
17 changes: 0 additions & 17 deletions array.extensions.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,3 @@
/**
* Array#namespace(parrent=window) -> Array
* - parent(Object): top level object to start injection from
*
* Creates a nested chain of objects, based on the value of array items
*
* ['foo', 'bar', 'baz'].namespace();
* typeof foo.bar.baz; // => 'object'
* ['util', 'DOM', 'dimensions'].namespace(Prototype);
* typeof Prototype.util.DOM.dimensions; // => 'object'
**/
Array.prototype.namespace = function(parent) {
this.inject(parent || window, function(object, property) {
return object[property] = object[property] || { };
})
};

/**
* [1,2,3].sum(); 6
* ['5','','8', 10].sum(); 23 <= type-safe
Expand Down
16 changes: 16 additions & 0 deletions string.extensions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Array#namespace(parrent=window) -> Array
* - parent(Object): top level object to start injection from
*
* Creates a nested chain of objects, based on the value of array items
*
* 'Prototype.foo.bar'.namespace();
* typeof Prototype.foo.bar; // => 'object'
* 'util.DOM.dimensions'.namespace(Prototype);
* typeof Prototype.util.DOM.dimensions; // => 'object'
**/
String.prototype.namespace = function(parent) {
return this.split('.').inject(parent || window, function(object, property) {
return object[property] = object[property] || { };
})
};
29 changes: 29 additions & 0 deletions test/array.extensions.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">

<title>helpers.js test file</title>

<link rel="stylesheet" href="../lib/test.css" type="text/css" />

<script src="../lib/prototype.js" type="text/javascript"></script>
<script src="../lib/unittest.js" type="text/javascript"></script>
<script src="../array.extensions.js" type="text/javascript"></script>

</head>
<body>
<div id="testlog"></div>

<script type="text/javascript">
new Test.Unit.Runner({
testArrayPrototypeSum: function() {
this.assertIdentical(6, [1,2,3].sum());
this.assertIdentical(10, ['1', 2, '3', 4, '', '', '', null, 0].sum());
}
})
</script>

</body>
</html>

0 comments on commit 9af0da0

Please sign in to comment.