Permalink
Browse files

Added `expose`.

Allows the definition of functions that do not require an instance of and extender
  • Loading branch information...
doug-martin committed Jan 13, 2013
1 parent e063621 commit 847a26045bb9ffd7f490c97fe12f07406f9da211
Showing with 144 additions and 39 deletions.
  1. +36 −0 README.md
  2. +24 −0 docs/index.html
  3. +1 −1 extender-min.js
  4. +56 −7 extender.js
  5. +27 −31 test/extender.test.js
View
@@ -255,6 +255,33 @@ extended.pluck("a").value(); // ["a", "b", "c"]);
**Note** `myExtender` and `myExtender2` will **NOT** be altered.
+**`extender.expose(methods)`**
+
+The `expose` method allows you to add methods to your extender that are not wrapped or automatically chained by exposing them on the extender directly.
+
+```
+var isMethods = {
+ isFunction: is.function,
+ isNumber: is.number,
+ isString: is.string,
+ isDate: is.date,
+ isArray: is.array,
+ isBoolean: is.boolean,
+ isUndefined: is.undefined,
+ isDefined: is.defined,
+ isUndefinedOrNull: is.undefinedOrNull,
+ isNull: is.null,
+ isArguments: is.arguments,
+ isInstanceOf: is.instanceOf,
+ isRegExp: is.regExp
+};
+
+var myExtender = extender.define(isMethods).expose(isMethods);
+
+myExtender.isArray([]); //true
+myExtender([]).isArray([]).value(); //true
+
+```
**Using `instanceof`**
@@ -270,3 +297,12 @@ str instanceof myExtender; //true
## Examples
To see more examples click [here](https://github.com/doug-martin/extender/tree/master/examples)
+
+
+
+
+
+
+
+
+
View
@@ -401,9 +401,33 @@ <h2>Usage</h2>
extended.pluck(&quot;a&quot;).value(); // [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;]);</code></pre>
<p><strong>Note</strong> <code>myExtender</code> and <code>myExtender2</code> will <strong>NOT</strong> be altered.
+</p>
+<p><strong><code>extender.expose(methods)</code></strong>
+</p>
+<p>The <code>expose</code> method allows you to add methods to your extender that are not wrapped or automatically chained by exposing them on the extender directly.
</p>
+<pre class='prettyprint linenums lang-js'><code>var isMethods = {
+ isFunction: is.function,
+ isNumber: is.number,
+ isString: is.string,
+ isDate: is.date,
+ isArray: is.array,
+ isBoolean: is.boolean,
+ isUndefined: is.undefined,
+ isDefined: is.defined,
+ isUndefinedOrNull: is.undefinedOrNull,
+ isNull: is.null,
+ isArguments: is.arguments,
+ isInstanceOf: is.instanceOf,
+ isRegExp: is.regExp
+};
+
+var myExtender = extender.define(isMethods).expose(isMethods);
+
+myExtender.isArray([]); //true
+myExtender([]).isArray([]).value(); //true</code></pre>
<p><strong>Using <code>instanceof</code></strong>
</p>
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -261,6 +261,33 @@
*
* **Note** `myExtender` and `myExtender2` will **NOT** be altered.
*
+ * **`extender.expose(methods)`**
+ *
+ * The `expose` method allows you to add methods to your extender that are not wrapped or automatically chained by exposing them on the extender directly.
+ *
+ * ```
+ * var isMethods = {
+ * isFunction: is.function,
+ * isNumber: is.number,
+ * isString: is.string,
+ * isDate: is.date,
+ * isArray: is.array,
+ * isBoolean: is.boolean,
+ * isUndefined: is.undefined,
+ * isDefined: is.defined,
+ * isUndefinedOrNull: is.undefinedOrNull,
+ * isNull: is.null,
+ * isArguments: is.arguments,
+ * isInstanceOf: is.instanceOf,
+ * isRegExp: is.regExp
+ * };
+ *
+ * var myExtender = extender.define(isMethods).expose(isMethods);
+ *
+ * myExtender.isArray([]); //true
+ * myExtender([]).isArray([]).value(); //true
+ *
+ * ```
*
*
* **Using `instanceof`**
@@ -411,26 +438,48 @@
}
function define(tester, decorate) {
- if (!decorate) {
- decorate = tester;
- tester = always;
+ if (arguments.length) {
+ if (typeof tester === "object") {
+ decorate = tester;
+ tester = always;
+ }
+ decorate = decorate || {};
+ var proto = {};
+ decorateProto(proto, decorate);
+ defined.push([tester, proto]);
}
- decorate = decorate || {};
- var proto = {};
- decorateProto(proto, decorate);
- defined.push([tester, proto]);
return _extender;
}
function extend(supr) {
if (supr && supr.hasOwnProperty("__defined__")) {
defined = defined.concat(supr["__defined__"]);
}
+ for (var i in supr) {
+ if (supr.hasOwnProperty(i) && i !== "define" && i !== "extend" && i !== "expose") {
+ _extender[i] = supr[i];
+ }
+ }
return _extender;
}
_extender.define = define;
_extender.extend = extend;
+ _extender.expose = function expose() {
+ var methods;
+ for (var i = 0, l = arguments.length; i < l; i++) {
+ methods = arguments[i];
+ if (typeof methods === "object") {
+ for (var j in methods) {
+ if (methods.hasOwnProperty(j) && j !== "define" && j !== "extend" && j !== "expose") {
+ _extender[i] = methods[i];
+ }
+ }
+ merge(_extender, arguments[i]);
+ }
+ }
+ return _extender;
+ };
_extender["__defined__"] = defined;
View
@@ -7,22 +7,23 @@ var it = require("it"),
it.describe("extender",function (it) {
+ var isMethods = {
+ isFunction: is.function,
+ isNumber: is.number,
+ isString: is.string,
+ isDate: is.date,
+ isArray: is.array,
+ isBoolean: is.boolean,
+ isUndefined: is.undefined,
+ isDefined: is.defined,
+ isUndefinedOrNull: is.undefinedOrNull,
+ isNull: is.null,
+ isArguments: is.arguments,
+ isInstanceOf: is.instanceOf,
+ isRegExp: is.regExp
+ };
var myExtender = extender
- .define({
- isFunction: is.function,
- isNumber: is.number,
- isString: is.string,
- isDate: is.date,
- isArray: is.array,
- isBoolean: is.boolean,
- isUndefined: is.undefined,
- isDefined: is.defined,
- isUndefinedOrNull: is.undefinedOrNull,
- isNull: is.null,
- isArguments: is.arguments,
- isInstanceOf: is.instanceOf,
- isRegExp: is.regExp
- })
+ .define(isMethods)
.define(is.string, {
multiply: function (str, times) {
var ret = str;
@@ -83,7 +84,8 @@ it.describe("extender",function (it) {
}
}
- });
+ })
+ .expose(isMethods);
it.should("add eq, neq", function () {
var str = myExtender("hello");
@@ -125,6 +127,12 @@ it.describe("extender",function (it) {
]).pluckPlain("a"), ["a", "b", "c"]);
});
+ it.should("add static methods to extenders if defined", function () {
+ assert.isTrue(myExtender.isBoolean(true));
+ assert.isTrue(myExtender.isString("hello"));
+ assert.isTrue(myExtender.isArray([]));
+ });
+
it.should("keep extenders in their own scope", function () {
var myExtender = extender
.define({
@@ -154,21 +162,8 @@ it.describe("extender",function (it) {
it.should("allow extending extenders", function () {
var myExtender = extender
- .define({
- isFunction: is.function,
- isNumber: is.number,
- isString: is.string,
- isDate: is.date,
- isArray: is.array,
- isBoolean: is.boolean,
- isUndefined: is.undefined,
- isDefined: is.defined,
- isUndefinedOrNull: is.undefinedOrNull,
- isNull: is.null,
- isArguments: is.arguments,
- isInstanceOf: is.instanceOf,
- isRegExp: is.regExp
- });
+ .define(isMethods)
+ .expose(isMethods);
var myExtender2 = extender.define(is.array, {
pluck: function (arr, m) {
var ret = [];
@@ -196,6 +191,7 @@ it.describe("extender",function (it) {
{a: "c"}
]);
assert.isTrue(extended.isArray().value());
+ assert.isTrue(composed.isArray([]));
assert.deepEqual(extended.pluck("a").value(), ["a", "b", "c"]);
//it should not alter the compsing extenders

0 comments on commit 847a260

Please sign in to comment.