Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added `expose`.

Allows the definition of functions that do not require an instance of and extender
  • Loading branch information...
commit 847a26045bb9ffd7f490c97fe12f07406f9da211 1 parent e063621
@doug-martin authored
View
36 README.md
@@ -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
24 docs/index.html
@@ -401,9 +401,33 @@
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
2  extender-min.js
@@ -1,3 +1,3 @@
/*! extender - v0.0.3 - 2013-01-12
* Copyright (c) 2013 Doug Martin (blog.dougamartin.com); Licensed MIT */
-(function(){function e(e){function i(i){function u(e,r,i){if("function"!=typeof i)throw new TypeError("when extending type you must provide a function");var s;r==="constructor"?s=function(){this._super(arguments),i.apply(this,arguments)}:s=function(){var r=t.call(arguments);r.unshift(this._value);var s=i.apply(this,r);return s!==n?l(s):this},e[r]=s}function a(e,n,r){if("function"!=typeof r)throw new TypeError("when extending type you must provide a function");var i;n==="constructor"?i=function(){this._super(arguments),r.apply(this,arguments)}:i=function(){var n=t.call(arguments);return n.unshift(this._value),r.apply(this,n)},e[n]=i}function f(e,t,n){for(var r in t)t.hasOwnProperty(r)&&(r!=="getters"&&r!=="setters"?r==="noWrap"?f(e,t[r],!0):n?a(e,r,t[r]):u(e,r,t[r]):e[r]=t[r])}function l(e){var t=e,n,i;if(!(e instanceof s)){var u={},a=u.instance={};for(n=0,i=o.length;n<i;n++){var f=o[n];f[0](e)&&r(a,f[1])}t=new(s.extend(u))(e)}return t}function c(){return!0}function h(e,t){t||(t=e,e=c),t=t||{};var n={};return f(n,t),o.push([e,n]),l}function p(e){return e&&e.hasOwnProperty("__defined__")&&(o=o.concat(e.__defined__)),l}i=i||[];var s=e({instance:{constructor:function(e){this._value=e},value:function(){return this._value},eq:function(t){return l(this._value===t)},neq:function(t){return l(this._value!==t)},print:function(){return console.log(this._value),this}}}),o=[];return l.define=h,l.extend=p,l.__defined__=o,l}var t=Array.prototype.slice,n,r=function(){function t(e,t){var n,r;for(n in t)if(t.hasOwnProperty(n)){r=t[n];if(!(n in e)||e[n]!==r)e[n]=r}return e}return function(n){n||(n={});for(var r=1,i=arguments.length;r<i;r++)t(n,arguments[r]);return n}}();return{define:function(){return i().define.apply(i,arguments)},extend:function(e){return i().define().extend(e)}}}"undefined"!=typeof exports?"undefined"!=typeof module&&module.exports&&(module.exports=e(require("declare.js"))):"function"==typeof define?define(["require"],function(t){return e(t("declare.js"))}):this.extender=e(this.declare)}).call(this);
+(function(){function e(e){function i(i){function u(e,r,i){if("function"!=typeof i)throw new TypeError("when extending type you must provide a function");var s;r==="constructor"?s=function(){this._super(arguments),i.apply(this,arguments)}:s=function(){var r=t.call(arguments);r.unshift(this._value);var s=i.apply(this,r);return s!==n?l(s):this},e[r]=s}function a(e,n,r){if("function"!=typeof r)throw new TypeError("when extending type you must provide a function");var i;n==="constructor"?i=function(){this._super(arguments),r.apply(this,arguments)}:i=function(){var n=t.call(arguments);return n.unshift(this._value),r.apply(this,n)},e[n]=i}function f(e,t,n){for(var r in t)t.hasOwnProperty(r)&&(r!=="getters"&&r!=="setters"?r==="noWrap"?f(e,t[r],!0):n?a(e,r,t[r]):u(e,r,t[r]):e[r]=t[r])}function l(e){var t=e,n,i;if(!(e instanceof s)){var u={},a=u.instance={};for(n=0,i=o.length;n<i;n++){var f=o[n];f[0](e)&&r(a,f[1])}t=new(s.extend(u))(e)}return t}function c(){return!0}function h(e,t){if(arguments.length){typeof e=="object"&&(t=e,e=c),t=t||{};var n={};f(n,t),o.push([e,n])}return l}function p(e){e&&e.hasOwnProperty("__defined__")&&(o=o.concat(e.__defined__));for(var t in e)e.hasOwnProperty(t)&&t!=="define"&&t!=="extend"&&t!=="expose"&&(l[t]=e[t]);return l}i=i||[];var s=e({instance:{constructor:function(e){this._value=e},value:function(){return this._value},eq:function(t){return l(this._value===t)},neq:function(t){return l(this._value!==t)},print:function(){return console.log(this._value),this}}}),o=[];return l.define=h,l.extend=p,l.expose=function(){var t;for(var n=0,i=arguments.length;n<i;n++){t=arguments[n];if(typeof t=="object"){for(var s in t)t.hasOwnProperty(s)&&s!=="define"&&s!=="extend"&&s!=="expose"&&(l[n]=t[n]);r(l,arguments[n])}}return l},l.__defined__=o,l}var t=Array.prototype.slice,n,r=function(){function t(e,t){var n,r;for(n in t)if(t.hasOwnProperty(n)){r=t[n];if(!(n in e)||e[n]!==r)e[n]=r}return e}return function(n){n||(n={});for(var r=1,i=arguments.length;r<i;r++)t(n,arguments[r]);return n}}();return{define:function(){return i().define.apply(i,arguments)},extend:function(e){return i().define().extend(e)}}}"undefined"!=typeof exports?"undefined"!=typeof module&&module.exports&&(module.exports=e(require("declare.js"))):"function"==typeof define?define(["require"],function(t){return e(t("declare.js"))}):this.extender=e(this.declare)}).call(this);
View
63 extender.js
@@ -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,14 +438,16 @@
}
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;
}
@@ -426,11 +455,31 @@
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
58 test/extender.test.js
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.