-
Notifications
You must be signed in to change notification settings - Fork 20
/
valentine.min.js
6 lines (6 loc) · 6.37 KB
/
valentine.min.js
1
2
3
4
5
6
/*!
* Valentine: JavaScript's functional Sister
* (c) Dustin Diaz 2014
* https://github.com/ded/valentine License MIT
*/
(function(e,t,n){typeof module!="undefined"?module.exports=n():typeof define=="function"?define(n):t[e]=t.v=n()})("valentine",this,function(){function u(e){this.values=e,this.index=0}function a(e,t){return new l(e,t)}function f(e,t){for(var n in t)e[n]=t[n]}function l(e,t){this.val=e,this._scope=t||n,this._chained=0}var e=[],t=Object.prototype.hasOwnProperty,n=null,r=e.slice,i={each:function(t,n,r){e.forEach.call(t,n,r)},map:function(t,n,r){return e.map.call(t,n,r)},some:function(e,t,n){return e.some(t,n)},every:function(e,t,n){return e.every(t,n)},filter:function(e,t,n){return e.filter(t,n)},indexOf:function(e,t,n){return e.indexOf(t,isFinite(n)?n:0)},lastIndexOf:function(e,t,n){return e.lastIndexOf(t,isFinite(n)?n:e.length)},reduce:function(t,n,r,i){return e.reduce.call(t,a.bind(i,n),r)},reduceRight:function(t,n,r,i){return e.reduceRight.call(t,a.bind(i,n),r)},find:function(e,t,n){var r;return i.some(e,function(e,i,s){if(t.call(n,e,i,s))return r=e,!0}),r},reject:function(e,t,n){var r=[];for(var i=0,s=0,o=e.length;i<o;i++)if(i in e){if(t.call(n,e[i],i,e))continue;r[s++]=e[i]}return r},size:function(e){return o.toArray(e).length},compact:function(e){return i.filter(e,function(e){return!!e})},flatten:function(e){return i.reduce(e,function(e,t){return s.arr(t)?e.concat(i.flatten(t)):(e[e.length]=t,e)},[])},uniq:function(e,t){if(e===null)return[];var n=[],r=[];for(var s=0,o=e.length;s<o;s++){var u=e[s];t&&(u=t(u,s,e)),i.inArray(r,u)||(r.push(u),n.push(e[s]))}return n},merge:function(e,t){var n=e.length,r=0,i;if(isFinite(t.length))for(i=t.length;r<i;r++)e[n++]=t[r];else while(t[r]!==undefined)e[n++]=t[r++];return e.length=n,e},inArray:function(e,t){return!!~i.indexOf(e,t)},memo:function(e,n){var r={};return n||(n=function(e){return e}),function(){var i=n.apply(this,arguments);return t.call(r,i)?r[i]:r[i]=e.apply(this,arguments)}}},s={fun:function(e){return typeof e=="function"},str:function(e){return typeof e=="string"},ele:function(e){return!!e&&!!e.nodeType&&e.nodeType==1},arr:function(e){return e instanceof Array},arrLike:function(e){return e&&e.length&&isFinite(e.length)},num:function(e){return typeof e=="number"},bool:function(e){return e===!0||e===!1},args:function(e){return!!e&&!!t.call(e,"callee")},emp:function(e){var t=0;return s.arr(e)?e.length===0:s.obj(e)?function(){for(var n in e){t++;break}return t===0}():e===""},dat:function(e){return!!(e&&e.getTimezoneOffset&&e.setUTCFullYear)},reg:function(e){return!(!(e&&e.test&&e.exec)||!e.ignoreCase&&e.ignoreCase!==!1)},nan:function(e){return e!==e},nil:function(e){return e===n},und:function(e){return typeof e=="undefined"},def:function(e){return typeof e!="undefined"},obj:function(e){return e instanceof Object&&!s.fun(e)&&!s.arr(e)}};s.empty=s.emp,s.date=s.dat,s.regexp=s.reg,s.element=s.ele,s.array=s.arr,s.string=s.str,s.undef=s.und,s.func=s.fun;var o={each:function(n,r,o){s.arrLike(n)?i.each(n,r,o):function(){for(var e in n)t.call(n,e)&&r.call(o,e,n[e],n)}()},map:function(n,r,o){var u=[],a=0;return s.arrLike(n)?i.map(n,r,o):!function(){for(var e in n)t.call(n,e)&&(u[a++]=r.call(o,e,n[e],n))}()&&u},some:function(n,r,o){if(s.arrLike(n))return i.some(n,r,o);for(var u in n)if(t.call(n,u)&&r.call(o,u,n[u],n))return!0;return!1},every:function(n,r,o){if(s.arrLike(n))return i.every(n,r,o);for(var u in n)if(!t.call(n,u)||!r.call(o,u,n[u],n))return!1;return!0},filter:function(n,r,o){var u={},a;if(s.arrLike(n))return i.filter(n,r,o);for(a in n)t.call(n,a)&&r.call(o,a,n[a],n)&&(u[a]=n[a]);return u},pluck:function(t,n){return s.arrLike(t)?i.map(t,function(e){return e[n]}):o.map(t,function(e,t){return t[n]})},toArray:function(t){return t?s.arr(t)?t:t.toArray?t.toArray():s.args(t)?r.call(t):i.map(t,function(e){return e}):[]},first:function(t){return t[0]},last:function(t){return t[t.length-1]},keys:Object.keys,values:function(e){return o.map(e,function(e,t){return t})},extend:function(){var t,r,i,u,a,f=arguments[0],l=1,c=arguments.length;for(;l<c;l++)if((t=arguments[l])!==n)for(r in t){i=f[r],u=t[r];if(f===u)continue;u&&s.obj(u)?(a=i&&s.obj(i)?i:{},f[r]=o.extend(a,u)):u!==undefined&&(f[r]=u)}return f},trim:function(e){return e.trim()},bind:function(e,t){var n=arguments.length>2?r.call(arguments,2):null;return function(){return t.apply(e,n?n.concat(r.call(arguments)):arguments)}},curry:function(t){if(arguments.length==1)return t;var n=r.call(arguments,1);return function(){return t.apply(null,n.concat(r.call(arguments)))}},parallel:function(t,r){var u=o.toArray(arguments),a=0,f=[],l=[];if(s.arr(t)&&t.length===0||s.fun(t)&&u.length===1)throw new TypeError("Empty parallel array");s.arr(t)||(r=u.pop(),t=u),i.each(t,function(e,s){e(function(){var e=o.toArray(arguments),u=e.shift();if(u)return r(u);f[s]=e,t.length==++a&&(f.unshift(n),i.each(f,function(e){l=l.concat(e)}),r.apply(n,l))})})},waterfall:function(t,r){var i=o.toArray(arguments);if(s.arr(t)&&t.length===0||s.fun(t)&&i.length===1)throw new TypeError("Empty waterfall array");s.arr(t)||(r=i.pop(),t=i),function u(){var e=o.toArray(arguments);e.length||e.push(null),e.push(u);var i=e.shift();!i&&t.length?t.shift().apply(n,e):(e.pop(),e.unshift(i),r.apply(n,e))}(n)},series:function(e,t){o.waterfall(e.map(function(e){return function(t){e(function(e){t(e)})}}),t)},queue:function(t){return new u(s.arrLike(t)?t:o.toArray(arguments))},debounce:function(t,n,r){function s(){function o(){i=null,n.apply(s,e)}var e=arguments,s=r||this;clearTimeout(i),i=setTimeout(o,t)}var i;return s.cancel=function(){clearTimeout(i),i=null},s},throttle:function(t,n,r,i){var s,o=i;return function(){var u=r||this,a=arguments;if(i){n.apply(u,a),i=!1;return}s||(s=setTimeout(function(){n.apply(u,a),s=null,i=o},t))}},throttleDebounce:function(e,t,n,r){function a(){i=arguments,s=r||this,clearTimeout(o),o=setTimeout(function(){clearTimeout(u),u=null,n.apply(s,i)},t),u||(u=setTimeout(function(){clearTimeout(o),u=null,n.apply(s,i)},e))}var i,s,o,u;return a.cancel=function(){clearTimeout(o),clearTimeout(u),u=null},a}};return u.prototype.next=function(){return this.index<this.values.length&&this.values[this.index++](),this},f(a,i),f(a,o),a.is=s,a.v=a,a.each(a.extend({},i,o),function(e,t){l.prototype[e]=function(){var e=a.toArray(arguments);e.unshift(this.val);var n=t.apply(this._scope,e);return this.val=n,this._chained?this:n}}),f(l.prototype,{chain:function(){return this._chained=1,this},value:function(){return this.val}}),a})